Closes #16307: Enable calling log_* methods on Script without a log message

This commit is contained in:
Jeremy Stretch 2024-06-24 09:39:27 -04:00
parent dbcd89c8ed
commit 8b62e40874
2 changed files with 23 additions and 19 deletions

View File

@ -138,11 +138,11 @@ These two methods will load data in YAML or JSON format, respectively, from file
The Script object provides a set of convenient functions for recording messages at different severity levels: The Script object provides a set of convenient functions for recording messages at different severity levels:
* `log_debug(message, obj=None)` * `log_debug(message=None, obj=None)`
* `log_success(message, obj=None)` * `log_success(message=None, obj=None)`
* `log_info(message, obj=None)` * `log_info(message=None, obj=None)`
* `log_warning(message, obj=None)` * `log_warning(message=None, obj=None)`
* `log_failure(message, obj=None)` * `log_failure(message=None, obj=None)`
Log messages are returned to the user upon execution of the script. Markdown rendering is supported for log messages. A message may optionally be associated with a particular object by passing it as the second argument to the logging method. Log messages are returned to the user upon execution of the script. Markdown rendering is supported for log messages. A message may optionally be associated with a particular object by passing it as the second argument to the logging method.
@ -152,6 +152,8 @@ A script can define one or more test methods to report on certain conditions. Al
These methods are detected and run automatically when the script is executed, unless its `run()` method has been overridden. (When overriding `run()`, `run_tests()` can be called to run all test methods present in the script.) These methods are detected and run automatically when the script is executed, unless its `run()` method has been overridden. (When overriding `run()`, `run_tests()` can be called to run all test methods present in the script.)
Calling any of these logging methods without a message will increment the relevant counter, but will not generate an output line in the script's log.
!!! info !!! info
This functionality was ported from [legacy reports](./reports.md) in NetBox v4.0. This functionality was ported from [legacy reports](./reports.md) in NetBox v4.0.

View File

@ -480,6 +480,12 @@ class BaseScript:
# A test method is currently active, so log the message using legacy Report logging # A test method is currently active, so log the message using legacy Report logging
if self._current_test: if self._current_test:
# Increment the event counter for this level
if level in self.tests[self._current_test]:
self.tests[self._current_test][level] += 1
# Record message (if any) to the report log
if message:
# TODO: Use a dataclass for test method logs # TODO: Use a dataclass for test method logs
self.tests[self._current_test]['log'].append(( self.tests[self._current_test]['log'].append((
timezone.now().isoformat(), timezone.now().isoformat(),
@ -489,10 +495,6 @@ class BaseScript:
str(message), str(message),
)) ))
# Increment the event counter for this level
if level in self.tests[self._current_test]:
self.tests[self._current_test][level] += 1
elif message: elif message:
# Record to the script's log # Record to the script's log
@ -509,19 +511,19 @@ class BaseScript:
message = f"{obj}: {message}" message = f"{obj}: {message}"
self.logger.log(LogLevelChoices.SYSTEM_LEVELS[level], message) self.logger.log(LogLevelChoices.SYSTEM_LEVELS[level], message)
def log_debug(self, message, obj=None): def log_debug(self, message=None, obj=None):
self._log(message, obj, level=LogLevelChoices.LOG_DEBUG) self._log(message, obj, level=LogLevelChoices.LOG_DEBUG)
def log_success(self, message, obj=None): def log_success(self, message=None, obj=None):
self._log(message, obj, level=LogLevelChoices.LOG_SUCCESS) self._log(message, obj, level=LogLevelChoices.LOG_SUCCESS)
def log_info(self, message, obj=None): def log_info(self, message=None, obj=None):
self._log(message, obj, level=LogLevelChoices.LOG_INFO) self._log(message, obj, level=LogLevelChoices.LOG_INFO)
def log_warning(self, message, obj=None): def log_warning(self, message=None, obj=None):
self._log(message, obj, level=LogLevelChoices.LOG_WARNING) self._log(message, obj, level=LogLevelChoices.LOG_WARNING)
def log_failure(self, message, obj=None): def log_failure(self, message=None, obj=None):
self._log(message, obj, level=LogLevelChoices.LOG_FAILURE) self._log(message, obj, level=LogLevelChoices.LOG_FAILURE)
self.failed = True self.failed = True