config: Add logging sections in __init__() to allow to set their default values
This commit is contained in:
parent
4962b16099
commit
25cdf9d4dc
1 changed files with 58 additions and 55 deletions
113
mylib/config.py
113
mylib/config.py
|
@ -682,8 +682,66 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
self.config_file_env_variable = config_file_env_variable
|
self.config_file_env_variable = config_file_env_variable
|
||||||
self.default_config_dirpath = default_config_dirpath
|
self.default_config_dirpath = default_config_dirpath
|
||||||
self.default_config_filename = default_config_filename
|
self.default_config_filename = default_config_filename
|
||||||
|
self.add_logging_sections()
|
||||||
self._init_config_parser()
|
self._init_config_parser()
|
||||||
|
|
||||||
|
def add_logging_sections(self):
|
||||||
|
"""Add logging sections"""
|
||||||
|
console_section = self.add_section("console", comment="Console logging", order=998)
|
||||||
|
console_section.add_option(
|
||||||
|
BooleanOption,
|
||||||
|
"enabled",
|
||||||
|
default=False,
|
||||||
|
arg="--console",
|
||||||
|
short_arg="-C",
|
||||||
|
comment="Enable/disable console log",
|
||||||
|
)
|
||||||
|
console_section.add_option(
|
||||||
|
BooleanOption,
|
||||||
|
"force_stderr",
|
||||||
|
default=False,
|
||||||
|
arg="--console-stderr",
|
||||||
|
comment="Force console log on stderr",
|
||||||
|
)
|
||||||
|
console_section.add_option(
|
||||||
|
StringOption,
|
||||||
|
"log_format",
|
||||||
|
default=DEFAULT_CONSOLE_LOG_FORMAT,
|
||||||
|
arg="--console-log-format",
|
||||||
|
comment="Console log format",
|
||||||
|
)
|
||||||
|
console_section.add_option(
|
||||||
|
StringOption,
|
||||||
|
"log_level",
|
||||||
|
comment=(
|
||||||
|
"Console log level limit : by default, all logged messages (according to main log "
|
||||||
|
"level) will be logged to the console, but you can set a minimal level if you "
|
||||||
|
# logging.getLevelNamesMapping() not available in python 3.9
|
||||||
|
# pylint: disable=protected-access
|
||||||
|
f"want. Possible values: {', '.join(logging._nameToLevel)}."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
logfile_section = self.add_section("logfile", comment="Logging file", order=999)
|
||||||
|
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 "
|
||||||
|
# logging.getLevelNamesMapping() not available in python 3.9
|
||||||
|
# pylint: disable=protected-access
|
||||||
|
f"want. Possible values: {', '.join(logging._nameToLevel)}."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
def add_section(self, name, loaded_callback=None, **kwargs):
|
def add_section(self, name, loaded_callback=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Add section
|
Add section
|
||||||
|
@ -882,61 +940,6 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
"-v", "--verbose", action="store_true", help="Show verbose messages"
|
"-v", "--verbose", action="store_true", help="Show verbose messages"
|
||||||
)
|
)
|
||||||
|
|
||||||
console_section = self.add_section("console", comment="Console logging")
|
|
||||||
console_section.add_option(
|
|
||||||
BooleanOption,
|
|
||||||
"enabled",
|
|
||||||
default=False,
|
|
||||||
arg="--console",
|
|
||||||
short_arg="-C",
|
|
||||||
comment="Enable/disable console log",
|
|
||||||
)
|
|
||||||
console_section.add_option(
|
|
||||||
BooleanOption,
|
|
||||||
"force_stderr",
|
|
||||||
default=False,
|
|
||||||
arg="--console-stderr",
|
|
||||||
comment="Force console log on stderr",
|
|
||||||
)
|
|
||||||
console_section.add_option(
|
|
||||||
StringOption,
|
|
||||||
"log_format",
|
|
||||||
default=DEFAULT_CONSOLE_LOG_FORMAT,
|
|
||||||
arg="--console-log-format",
|
|
||||||
comment="Console log format",
|
|
||||||
)
|
|
||||||
console_section.add_option(
|
|
||||||
StringOption,
|
|
||||||
"log_level",
|
|
||||||
comment=(
|
|
||||||
"Console log level limit : by default, all logged messages (according to main log "
|
|
||||||
"level) will be logged to the console, but you can set a minimal level if you "
|
|
||||||
# logging.getLevelNamesMapping() not available in python 3.9
|
|
||||||
# pylint: disable=protected-access
|
|
||||||
f"want. Possible values: {', '.join(logging._nameToLevel)}."
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
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 "
|
|
||||||
# logging.getLevelNamesMapping() not available in python 3.9
|
|
||||||
# pylint: disable=protected-access
|
|
||||||
f"want. Possible values: {', '.join(logging._nameToLevel)}."
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.add_options_to_parser(self.options_parser)
|
self.add_options_to_parser(self.options_parser)
|
||||||
|
|
||||||
return self.options_parser
|
return self.options_parser
|
||||||
|
|
Loading…
Reference in a new issue