Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds "bootstrap logger" for logging before CLI options are recognized #1839

Merged
merged 2 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/unit/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,7 @@ def test_labels(caplog: _pytest.logging.LogCaptureFixture, root_logger: Logger)
root_logger.labels += ['bar']

_exercise_logger(caplog, root_logger, labels=['foo', 'bar'])


def test_bootstrap_logger(caplog: _pytest.logging.LogCaptureFixture) -> None:
_exercise_logger(caplog, Logger.get_boostrap_logger())
26 changes: 26 additions & 0 deletions tmt/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,29 @@ def fail(
'shift': shift
}
)

_bootstrap_logger: Optional['Logger'] = None

@classmethod
def get_boostrap_logger(cls) -> 'Logger':
thrix marked this conversation as resolved.
Show resolved Hide resolved
"""
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