From cdda78eab09e46f82ef6c156d645c570f408c3eb Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Sun, 31 Mar 2024 03:17:18 +0900 Subject: [PATCH] Add option to disable color Introduce an option to disable color keys which are set according to log levels, so that the log sent to stdout/stderr is more machine readable. Closes-Bug: #2038917 Change-Id: I7966d4f4977b267f620946de4a5509f53b043652 --- oslo_log/_options.py | 5 +++++ oslo_log/log.py | 12 ++++++++---- releasenotes/notes/log_color-531437cab4477bcb.yaml | 5 +++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/log_color-531437cab4477bcb.yaml diff --git a/oslo_log/_options.py b/oslo_log/_options.py index 34af4abc..8c728e2c 100644 --- a/oslo_log/_options.py +++ b/oslo_log/_options.py @@ -114,6 +114,11 @@ generic_log_opts = [ deprecated_for_removal=True, deprecated_reason='Windows support is no longer maintained.', help='Log output to Windows Event Log.'), + cfg.BoolOpt('log_color', + default=False, + help='(Optional) Set the \'color\' key according to log ' + 'levels. This option takes effect only when logging ' + 'to stderr or stdout is used. ' + _IGNORE_MESSAGE), cfg.IntOpt('log_rotate_interval', default=1, help='The amount of time before the log files are rotated. ' diff --git a/oslo_log/log.py b/oslo_log/log.py index 72526c2c..9de69261 100644 --- a/oslo_log/log.py +++ b/oslo_log/log.py @@ -401,7 +401,10 @@ def _setup_logging_from_conf(conf, project, version): log_root.addHandler(filelog) if conf.use_stderr: - streamlog = handlers.ColorHandler() + if conf.log_color: + streamlog = handlers.ColorHandler() + else: + streamlog = logging.StreamHandler() log_root.addHandler(streamlog) if conf.use_journal: @@ -421,9 +424,10 @@ def _setup_logging_from_conf(conf, project, version): # if None of the above are True, then fall back to standard out if not logpath and not conf.use_stderr and not conf.use_journal: - # pass sys.stdout as a positional argument - # python2.6 calls the argument strm, in 2.7 it's stream - streamlog = handlers.ColorHandler(sys.stdout) + if conf.log_color: + streamlog = handlers.ColorHandler(sys.stdout) + else: + streamlog = logging.StreamHandler(sys.stdout) log_root.addHandler(streamlog) if conf.publish_errors: diff --git a/releasenotes/notes/log_color-531437cab4477bcb.yaml b/releasenotes/notes/log_color-531437cab4477bcb.yaml new file mode 100644 index 00000000..6fefc9e6 --- /dev/null +++ b/releasenotes/notes/log_color-531437cab4477bcb.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + The new ``log_color`` option has been added. When this option is set to + ``False``, color keys are not set.