Skip to content

Commit

Permalink
Closes #16307: Enable calling log_* methods on Script without a log m…
Browse files Browse the repository at this point in the history
…essage
  • Loading branch information
jeremystretch committed Jun 24, 2024
1 parent 00d9a86 commit 40027b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
12 changes: 7 additions & 5 deletions docs/customization/custom-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

* `log_debug(message, obj=None)`
* `log_success(message, obj=None)`
* `log_info(message, obj=None)`
* `log_warning(message, obj=None)`
* `log_failure(message, obj=None)`
* `log_debug(message=None, obj=None)`
* `log_success(message=None, obj=None)`
* `log_info(message=None, obj=None)`
* `log_warning(message=None, 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.

Expand All @@ -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.)

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
This functionality was ported from [legacy reports](./reports.md) in NetBox v4.0.

Expand Down
30 changes: 16 additions & 14 deletions netbox/extras/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,21 @@ def _log(self, message, obj=None, level=LogLevelChoices.LOG_DEFAULT):
# A test method is currently active, so log the message using legacy Report logging
if self._current_test:

# TODO: Use a dataclass for test method logs
self.tests[self._current_test]['log'].append((
timezone.now().isoformat(),
level,
str(obj) if obj else None,
obj.get_absolute_url() if hasattr(obj, 'get_absolute_url') else None,
str(message),
))

# 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
self.tests[self._current_test]['log'].append((
timezone.now().isoformat(),
level,
str(obj) if obj else None,
obj.get_absolute_url() if hasattr(obj, 'get_absolute_url') else None,
str(message),
))

elif message:

# Record to the script's log
Expand All @@ -509,19 +511,19 @@ def _log(self, message, obj=None, level=LogLevelChoices.LOG_DEFAULT):
message = f"{obj}: {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)

def log_success(self, message, obj=None):
def log_success(self, message=None, obj=None):
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)

def log_warning(self, message, obj=None):
def log_warning(self, message=None, obj=None):
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.failed = True

Expand Down

0 comments on commit 40027b4

Please sign in to comment.