Skip to content

Commit

Permalink
Add BuilltIn.Reset Log Level. Fixes #4930.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkaklarck committed Dec 19, 2023
1 parent ac50738 commit 0384c4f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
9 changes: 9 additions & 0 deletions atest/robot/standard_libraries/builtin/set_log_level.robot
Expand Up @@ -26,6 +26,15 @@ Set Log Level
Invalid Log Level Failure Is Catchable
Check Test Case ${TESTNAME}

Reset Log Level
${tc} = Check Test Case ${TESTNAME}
Check Log Message ${tc.kws[0].msgs[0]} Log level changed from INFO to DEBUG. DEBUG
Check Log Message ${tc.kws[1].msgs[0]} This is logged INFO
Check Log Message ${tc.kws[2].msgs[0]} This is logged DEBUG
Should Be Empty ${tc.kws[3].msgs}
Check Log Message ${tc.kws[4].msgs[0]} This is logged INFO
Should Be Empty ${tc.kws[5].msgs}

Log Level Goes To HTML
File Should Contain ${OUTDIR}${/}set_log_level_log.html KW Info to log
File Should Contain ${OUTDIR}${/}set_log_level_log.html KW Trace to log
Expand Down
11 changes: 10 additions & 1 deletion atest/testdata/standard_libraries/builtin/set_log_level.robot
Expand Up @@ -27,7 +27,16 @@ Set Log Level
[Teardown] Set Log Level INFO

Invalid Log Level Failure Is Catchable
Run Keyword And Expect Error Invalid log level 'INVALID'. Set Log Level INVALID
[Documentation] FAIL Invalid log level 'INVALID'.
Set Log Level INVALID

Reset Log Level
Set Log Level DEBUG
Log This is logged INFO
Log This is logged DEBUG
Reset Log Level
Log This is logged INFO
Log This is not logged DEBUG

Log Level Goes To HTML
Set Log Level Trace
Expand Down
23 changes: 19 additions & 4 deletions src/robot/libraries/BuiltIn.py
Expand Up @@ -3160,19 +3160,34 @@ def comment(self, *messages):
pass

def set_log_level(self, level):
"""Sets the log threshold to the specified level and returns the old level.
"""Sets the log threshold to the specified level.
Messages below the level will not logged. The default logging level is
INFO, but it can be overridden with the command line option ``--loglevel``.
The available levels: TRACE, DEBUG, INFO (default), WARN, ERROR and NONE
INFO, but it can be overridden with the ``--loglevel`` command line option.
The available levels are TRACE, DEBUG, INFO (default), WARN, ERROR and NONE
(no logging).
The old level is returned and can be used for setting the level back
later. An alternative way to reset the level is using the dedicated
`Reset Log Level` keyword.
"""
old = self._context.output.set_log_level(level)
self._namespace.variables.set_global('${LOG_LEVEL}', level.upper())
self.log(f'Log level changed from {old} to {level.upper()}.', level='DEBUG')
return old

def reset_log_level(self):
"""Resets the log level to the original value.
The original log level is set from the command line with the ``--loglevel``
option and is INFO by default. The active log level can be changed using
the `Set Log Level` keyword.
New in Robot Framework 7.0.
"""
level = self._context.output.initial_log_level
return self.set_log_level(level)

def reload_library(self, name_or_instance):
"""Rechecks what keywords the specified library provides.
Expand Down
4 changes: 4 additions & 0 deletions src/robot/output/output.py
Expand Up @@ -34,6 +34,10 @@ def __init__(self, settings):
self._register_loggers(DebugFile(settings.debug_file))
self._settings = settings

@property
def initial_log_level(self):
return self._settings.log_level

def _register_loggers(self, debug_file):
LOGGER.register_xml_logger(self._xml_logger)
LOGGER.register_listeners(self.listeners or None, self.library_listeners)
Expand Down

0 comments on commit 0384c4f

Please sign in to comment.