config: Add logfile feature
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Benjamin Renard 2023-10-27 13:36:32 +02:00
parent 8a0a65465d
commit b92a814577
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -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()