Fix logging in the baremetal CLI

* Ask openstacksdk to log to stderr instead of stdout, otherwise its
  warnings break parsing the output.
* Respect the --verbose flag (same as --debug but without tracebacks).
* Do not propagate log records that are handled by either openstacksdk's
  or ours logging code.

Change-Id: Ia191ab32f0d4f8f50cb266332d4f4bc96619cb58
This commit is contained in:
Dmitry Tantsur 2022-05-23 16:51:18 +02:00
parent bf48ad2394
commit bc753e22b4
2 changed files with 32 additions and 9 deletions

View File

@ -170,21 +170,35 @@ class App(app.App):
) )
return parser return parser
# This is the openstacksdk default value
_STREAM_FORMAT = '%(asctime)s %(levelname)s: %(name)s %(message)s'
def _configure_ironic_logging(self): def _configure_ironic_logging(self):
openstack.enable_logging(debug=self.options.debug) debug_enabled = self.options.debug or self.options.verbose_level > 1
# NOTE(dtantsur): I wish logging.basicConfig worked.. but it does not.
openstack.enable_logging(debug=debug_enabled, stream=sys.stderr,
format_template=self._STREAM_FORMAT,
# No fancy formatting if debug is off
format_stream=debug_enabled)
# NOTE(dtantsur): prevent openstack logging from appearing again in the
# cliff handlers
for name in ('openstack', 'keystoneauth'):
logger = logging.getLogger(name)
logger.propagate = False
# NOTE(dtantsur): I wish logging.basicConfig worked, but at this point
# cliff has already started configuring logging.
for name in ('ironicclient', 'ironic_inspector_client'): for name in ('ironicclient', 'ironic_inspector_client'):
logger = logging.getLogger(name) logger = logging.getLogger(name)
logger.setLevel( logger.setLevel(
logging.DEBUG if self.options.debug else logging.WARNING) logging.DEBUG if debug_enabled else logging.WARNING)
# warnings are already configured by something else, only configure # warnings are already configured by cliff, only configure
# debug logging for ironic. # debug logging for ironic.
if not logger.handlers and self.options.debug: if not logger.handlers and debug_enabled:
handler = logging.StreamHandler() handler = logging.StreamHandler(stream=sys.stderr)
handler.setFormatter(logging.Formatter( handler.setFormatter(logging.Formatter(self._STREAM_FORMAT))
# This is the openstacksdk default value
'%(asctime)s %(levelname)s: %(name)s %(message)s'))
logger.addHandler(handler) logger.addHandler(handler)
logger.propagate = False
def initialize_app(self, argv): def initialize_app(self, argv):
super(App, self).initialize_app(argv) super(App, self).initialize_app(argv)

View File

@ -0,0 +1,9 @@
---
fixes:
- |
OpenStackSDK log messages are no longer sent to stdout and no longer break
parsing the output.
- |
The logging configuration now respects the ``--verbose`` flag.
- |
Some warnings are no longer duplicated.