From a59cca2162e99f342c6cc5043913ee7b88d30dd8 Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Tue, 17 Aug 2021 15:17:40 +0200 Subject: [PATCH 1/2] Allow disabling default logging handlers --- mmpy_bot/bot.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/mmpy_bot/bot.py b/mmpy_bot/bot.py index fcba7001..65cb85ec 100644 --- a/mmpy_bot/bot.py +++ b/mmpy_bot/bot.py @@ -23,24 +23,26 @@ def __init__( self, settings: Optional[Settings] = None, plugins: Optional[Sequence[Plugin]] = None, + enable_logging: bool = True, ): if plugins is None: plugins = [ExamplePlugin(), WebHookExample()] # Use default settings if none were specified. self.settings = settings or Settings() - logging.basicConfig( - **{ - "format": self.settings.LOG_FORMAT, - "datefmt": "%m/%d/%Y %H:%M:%S", - "level": logging.DEBUG if self.settings.DEBUG else logging.INFO, - "filename": self.settings.LOG_FILE, - "filemode": "w", - } - ) - # define and add a Handler which writes log messages to the sys.stdout - self.console = logging.StreamHandler(stream=sys.stdout) - self.console.setFormatter(logging.Formatter(self.settings.LOG_FORMAT)) - logging.getLogger("").addHandler(self.console) + if enable_logging: + logging.basicConfig( + **{ + "format": self.settings.LOG_FORMAT, + "datefmt": "%m/%d/%Y %H:%M:%S", + "level": logging.DEBUG if self.settings.DEBUG else logging.INFO, + "filename": self.settings.LOG_FILE, + "filemode": "w", + } + ) + # define and add a Handler which writes log messages to the sys.stdout + self.console = logging.StreamHandler(stream=sys.stdout) + self.console.setFormatter(logging.Formatter(self.settings.LOG_FORMAT)) + logging.getLogger("").addHandler(self.console) self.driver = Driver( { From 9412cf122e2d41c97c881e94de784c94c333cc22 Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Tue, 17 Aug 2021 15:42:32 +0200 Subject: [PATCH 2/2] Refactor logging registration out of __init__ --- mmpy_bot/bot.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/mmpy_bot/bot.py b/mmpy_bot/bot.py index 65cb85ec..1aeb012f 100644 --- a/mmpy_bot/bot.py +++ b/mmpy_bot/bot.py @@ -29,20 +29,11 @@ def __init__( plugins = [ExamplePlugin(), WebHookExample()] # Use default settings if none were specified. self.settings = settings or Settings() + if enable_logging: - logging.basicConfig( - **{ - "format": self.settings.LOG_FORMAT, - "datefmt": "%m/%d/%Y %H:%M:%S", - "level": logging.DEBUG if self.settings.DEBUG else logging.INFO, - "filename": self.settings.LOG_FILE, - "filemode": "w", - } - ) - # define and add a Handler which writes log messages to the sys.stdout - self.console = logging.StreamHandler(stream=sys.stdout) - self.console.setFormatter(logging.Formatter(self.settings.LOG_FORMAT)) - logging.getLogger("").addHandler(self.console) + self._register_logger() + else: + self.console = None self.driver = Driver( { @@ -67,6 +58,21 @@ def __init__( self.running = False + def _register_logger(self): + logging.basicConfig( + **{ + "format": self.settings.LOG_FORMAT, + "datefmt": "%m/%d/%Y %H:%M:%S", + "level": logging.DEBUG if self.settings.DEBUG else logging.INFO, + "filename": self.settings.LOG_FILE, + "filemode": "w", + } + ) + # define and add a Handler which writes log messages to the sys.stdout + self.console = logging.StreamHandler(stream=sys.stdout) + self.console.setFormatter(logging.Formatter(self.settings.LOG_FORMAT)) + logging.getLogger("").addHandler(self.console) + def _initialize_plugins(self, plugins: Sequence[Plugin]): for plugin in plugins: plugin.initialize(self.driver, self.settings)