config: split console logging between stdout & stderr base on level
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Benjamin Renard 2023-01-06 18:18:55 +01:00
parent 55782df47c
commit eb183b0d3b
2 changed files with 36 additions and 4 deletions

View file

@ -879,11 +879,20 @@ class Config: # pylint: disable=too-many-instance-attributes
logging.getLogger().setLevel(logging.INFO) logging.getLogger().setLevel(logging.INFO)
if self.get('console', 'enabled'): if self.get('console', 'enabled'):
console_handler = logging.StreamHandler(sys.stdout) stdout_console_handler = logging.StreamHandler(sys.stdout)
stdout_console_handler.addFilter(StdoutInfoFilter())
stdout_console_handler.setLevel(logging.DEBUG)
stderr_console_handler = logging.StreamHandler(sys.stderr)
stderr_console_handler.setLevel(logging.WARNING)
if self.get('console', 'log_format'): if self.get('console', 'log_format'):
console_formater = logging.Formatter(self.get('console', 'log_format')) console_formater = logging.Formatter(self.get('console', 'log_format'))
console_handler.setFormatter(console_formater) stdout_console_handler.setFormatter(console_formater)
logging.getLogger().addHandler(console_handler) stderr_console_handler.setFormatter(console_formater)
logging.getLogger().addHandler(stdout_console_handler)
logging.getLogger().addHandler(stderr_console_handler)
if execute_callback: if execute_callback:
self._loaded() self._loaded()
@ -1127,3 +1136,10 @@ class ConfigSectionAsDictWrapper:
def __delitem__(self, key): def __delitem__(self, key):
raise Exception('Deleting a configuration option is not supported') raise Exception('Deleting a configuration option is not supported')
# pylint: disable=too-few-public-methods
class StdoutInfoFilter(logging.Filter):
""" Logging filter to keep messages only >= logging.INFO """
def filter(self, record):
return record.levelno in (logging.DEBUG, logging.INFO)

View file

@ -1,10 +1,12 @@
# pylint: disable=redefined-outer-name,missing-function-docstring,protected-access,global-statement # pylint: disable=redefined-outer-name,missing-function-docstring,protected-access,global-statement
""" Tests on config lib """ """ Tests on config lib """
import logging
from mylib.config import Config, ConfigSection from mylib.config import Config, ConfigSection
from mylib.config import StringOption from mylib.config import StringOption
runned = dict() runned = {}
def test_config_init_default_args(): def test_config_init_default_args():
@ -208,3 +210,17 @@ def test_get_default():
config.parse_arguments_options(argv=[], create=False) config.parse_arguments_options(argv=[], create=False)
assert config.get(section_name, opt_name) == opt_default_value assert config.get(section_name, opt_name) == opt_default_value
def test_logging_splited_stdout_stderr(capsys):
config = Config('Test app')
config.parse_arguments_options(argv=['-C', '-v'], create=False)
info_msg = "[info]"
err_msg = "[error]"
logging.getLogger().info(info_msg)
logging.getLogger().error(err_msg)
captured = capsys.readouterr()
assert info_msg in captured.out
assert info_msg not in captured.err
assert err_msg in captured.err
assert err_msg not in captured.out