From b92a814577b671a97ef1d20deab926fca7a12e9f Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Fri, 27 Oct 2023 13:36:32 +0200 Subject: [PATCH] config: Add logfile feature --- mylib/config.py | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/mylib/config.py b/mylib/config.py index 232e859..94c3293 100644 --- a/mylib/config.py +++ b/mylib/config.py @@ -24,7 +24,9 @@ log = logging.getLogger(__name__) # Constants DEFAULT_ENCODING = "utf-8" DEFAULT_CONFIG_DIRPATH = os.path.expanduser("./") -DEFAULT_CONSOLE_LOG_FORMAT = "%(asctime)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s" +DEFAULT_LOG_FORMAT = "%(asctime)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s" +DEFAULT_CONSOLE_LOG_FORMAT = DEFAULT_LOG_FORMAT +DEFAULT_FILELOG_FORMAT = DEFAULT_LOG_FORMAT class ConfigException(BaseException): @@ -824,8 +826,8 @@ class Config: # pylint: disable=too-many-instance-attributes "-v", "--verbose", action="store_true", help="Show verbose messages" ) - section = self.add_section("console", comment="Console logging") - section.add_option( + console_section = self.add_section("console", comment="Console logging") + console_section.add_option( BooleanOption, "enabled", default=False, @@ -833,14 +835,14 @@ class Config: # pylint: disable=too-many-instance-attributes short_arg="-C", comment="Enable/disable console log", ) - section.add_option( + console_section.add_option( BooleanOption, "force_stderr", default=False, arg="--console-stderr", comment="Force console log on stderr", ) - section.add_option( + console_section.add_option( StringOption, "log_format", default=DEFAULT_CONSOLE_LOG_FORMAT, @@ -848,6 +850,24 @@ class Config: # pylint: disable=too-many-instance-attributes comment="Console log format", ) + logfile_section = self.add_section("logfile", comment="Logging file") + logfile_section.add_option(StringOption, "path", comment="File log path") + logfile_section.add_option( + StringOption, + "format", + default=DEFAULT_FILELOG_FORMAT, + comment="File log format", + ) + logfile_section.add_option( + StringOption, + "level", + comment=( + "File log level limit : by default, all logged messages (according to main log " + "level) will be logged to the log file, but you can set a minimal level if you " + f"want. Possible values: {', '.join(logging.getLevelNamesMapping())}." + ), + ) + self.add_options_to_parser(self.options_parser) return self.options_parser @@ -945,6 +965,24 @@ class Config: # pylint: disable=too-many-instance-attributes logging.getLogger().addHandler(stdout_console_handler) logging.getLogger().addHandler(stderr_console_handler) + if self.get("logfile", "path"): + logfile_handler = logging.FileHandler(self.get("logfile", "path")) + logfile_level = ( + logging.getLevelNamesMapping().get(self.get("logfile", "level")) + if self.get("logfile", "level") + else logging.DEBUG + ) + if logfile_level is None: + log.fatal("Invalid log file level specified (%s)", self.get("logfile", "level")) + sys.exit(1) + logfile_handler.setLevel(logfile_level) + + if self.get("logfile", "format"): + logfile_formater = logging.Formatter(self.get("logfile", "format")) + logfile_handler.setFormatter(logfile_formater) + + logging.getLogger().addHandler(logfile_handler) + if execute_callback: self._loaded()