-1

Don't the different logging levels in Python (v3.11.4) logging work for a file and for telegrams?

So the logs of the WARNING level and above go both to the Telegram and to the file, and the logs of the INFO level only go to the file.

There is a script:

import logging
import tg_logger
import os
from dotenv import load_dotenv
from pathlib import Path
from logging.handlers import RotatingFileHandler

dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
    load_dotenv(dotenv_path)
token = os.getenv("TELEGRAM_BOT_TOKEN")
users = [os.getenv("TELEGRAM_CHAT_ID")]
script_path = Path(__file__).resolve()
script_name = os.path.splitext(os.path.basename(script_path))[0]
log_dir = script_path.parent
log_file = Path(str(log_dir) + "/" + script_name + ".log")
logger = logging.getLogger(script_name)
tg_logger.setup(logger, token=token, users=users, tg_format="<b><u>%(name)s</u> : %(levelname)s</b>\n\n<code>%(message)s</code>")
file_handler = RotatingFileHandler(log_file, maxBytes=1024*1024, backupCount=10)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(file_handler)
logger.setLevel(logging.WARNING)

logger.info(f"Hello from tg_logger by {script_name}")
logger.warning(f"Test WARNING {script_name}")
#logger.error(f

"Test ERROR {script_name}")

1 Answer 1

0

The answer was suggested to me by a good person on qna.habr.com And as always, everything turned out to be very simple and elegant.

import logging
import tg_logger
import os
from dotenv import load_dotenv
from pathlib import Path
from logging.handlers import RotatingFileHandler
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
    load_dotenv(dotenv_path)
token = os.getenv("TELEGRAM_BOT_TOKEN")
users = [os.getenv("TELEGRAM_CHAT_ID")]

script_path = Path(__file__).resolve()
script_name = os.path.splitext(os.path.basename(script_path))[0]
log_dir = script_path.parent
log_file = Path(str(log_dir) + "/" + script_name + ".log")
logger = logging.getLogger(script_name)
logger.setLevel(logging.INFO)
file_handler = RotatingFileHandler(log_file, maxBytes=1024*1024, backupCount=10)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(file_handler)
t_handler = tg_logger.setup(logger, token=token, users=users, tg_format="<b><u>%(name)s</u> : %(levelname)s</b>\n\n<code>%(message)s</code>")
t_handler.setLevel(logging.WARNING)
logger.addHandler(t_handler)
logger.info(f"Hello from tg_logger by {script_name}")
logger.warning(f"Test WARNING {script_name}")
#logger.error(f"Test ERROR {script_name}")
Sign up to request clarification or add additional context in comments.

1 Comment

consider adding comments to the code and providing some details around the answer. code only answers does not help the community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.