From 7267588e74392208b3b8f580152630fee627a38c Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Mon, 8 Oct 2018 19:28:49 +0200 Subject: [PATCH] Logging can be configured now, just like in Nextbot --- bot.py | 29 +++++++++++++++++++---------- config.ini.example | 5 +++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/bot.py b/bot.py index b9068bb..3578d95 100644 --- a/bot.py +++ b/bot.py @@ -16,22 +16,31 @@ from telegram.ext.dispatcher import run_async import utils -# Logging -logging.basicConfig( - format="%(asctime)s - %(levelname)s: %(message)s", - datefmt="%d.%m.%Y %H:%M:%S", - level=logging.INFO -) -logger = logging.getLogger(__name__) - -# Bot configuration config = ConfigParser() try: config.read_file(open('config.ini')) except FileNotFoundError: - logger.critical('Config.ini nicht gefunden') + logging.critical('Config.ini nicht gefunden') sys.exit(1) +# Logging +try: + logging_conf = config["LOGGING"] + logging_level = logging_conf.get("level", "INFO") + logging_format = logging_conf.get("format", "%(asctime)s - %(levelname)s: %(message)s", raw=True) + if logging_level not in ["DEBUG", "INFO", "CRITICAL", "ERROR", "WARNING"]: + logging.warning("Logging Level invalid. Will be changed to WARNING") + logging.basicConfig(format=logging_format, level=logging.INFO, datefmt="%d.%m.%Y %H:%M:%S") + else: + logging.basicConfig(format=logging_format, + level=eval("logging.{0}".format(logging_level.upper())), + datefmt="%d.%m.%Y %H:%M:%S") +except KeyError: + logging.basicConfig(format="%(asctime)s - %(levelname)s: %(message)s", + level=logging.INFO, + datefmt="%d.%m.%Y %H:%M:%S") +logger = logging.getLogger(__name__) + # Bot token try: bot_token = config['DEFAULT']['token'] diff --git a/config.ini.example b/config.ini.example index 1bac842..4299b0c 100644 --- a/config.ini.example +++ b/config.ini.example @@ -1,6 +1,11 @@ [DEFAULT] token = 1337:1234567890abcdefgh +[LOGGING] +# Valid options are ERROR, INFO, DEBUG, CRITICAL, WARNING +#level = INFO +#format = %(asctime)s - %(levelname)s: %(message)s + [REDIS] #db = 0 #host = localhost