Skip to content

Commit

Permalink
Adds "bootstrap logger" for logging before CLI options are recognized
Browse files Browse the repository at this point in the history
Aims at parts of tmt lifetime where logging is needed, but CLI options
like --debug are unknown yet. This would be code that's executed in
import time, the most visible one is plugin explocation & importing, but
there may be other. At this moment, `fmf.utils.Logging` is used for this
purpose, but that's tmt's logging "sneaking" away from what `tmt.log`
offers and implements. And that's not good, as it may mean slightly
different logging behavior than the one exhibited during the rest of
tmt run.
  • Loading branch information
happz committed Feb 22, 2023
1 parent 1dc5bf8 commit db52518
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tmt/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,29 @@ def fail(
'shift': shift
}
)

_bootstrap_logger: Optional['Logger'] = None

@classmethod
def get_boostrap_logger(cls) -> 'Logger':
"""
Create a logger designed for tmt startup time.
.. warning::
This logger has a **very** limited use case span, i.e. before tmt can
digest its command-line options and create a proper logger. This happens
inside :py:funs:`tmt.cli.main` function, but there are some actions taken
by tmt code before this function is called by Click, actions that need
to emit logging messages. Using it anywhere outside of this brief time
in tmt's runtime should be ruled out.
"""

if cls._bootstrap_logger is None:
# Stay away of our future main logger
actual_logger = Logger._normalize_logger(logging.getLogger('_tmt_bootstrap'))

cls._bootstrap_logger = Logger.create(actual_logger=actual_logger)
cls._bootstrap_logger.add_console_handler()

return cls._bootstrap_logger

0 comments on commit db52518

Please sign in to comment.