mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
Enable system logging for custom scripts
This commit is contained in:
parent
406b88777c
commit
36cbbac870
@ -6,4 +6,5 @@
|
|||||||
|---------------------------------|--------------------------------------------------------|
|
|---------------------------------|--------------------------------------------------------|
|
||||||
| `netbox.auth.*` | Authentication events |
|
| `netbox.auth.*` | Authentication events |
|
||||||
| `netbox.api.views.*` | Views which handle business logic for the REST API |
|
| `netbox.api.views.*` | Views which handle business logic for the REST API |
|
||||||
|
| `netbox.scripts.*` | Custom scripts (`module.name`) |
|
||||||
| `netbox.views.*` | Views which handle business logic for the web UI |
|
| `netbox.views.*` | Views which handle business logic for the web UI |
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import time
|
import time
|
||||||
@ -260,6 +261,7 @@ class BaseScript:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
# Initiate the log
|
# Initiate the log
|
||||||
|
self.logger = logging.getLogger(f"netbox.scripts.{self.module()}.{self.__class__.__name__}")
|
||||||
self.log = []
|
self.log = []
|
||||||
|
|
||||||
# Declare the placeholder for the current request
|
# Declare the placeholder for the current request
|
||||||
@ -307,18 +309,23 @@ class BaseScript:
|
|||||||
# Logging
|
# Logging
|
||||||
|
|
||||||
def log_debug(self, message):
|
def log_debug(self, message):
|
||||||
|
self.logger.log(logging.DEBUG, message)
|
||||||
self.log.append((LOG_DEFAULT, message))
|
self.log.append((LOG_DEFAULT, message))
|
||||||
|
|
||||||
def log_success(self, message):
|
def log_success(self, message):
|
||||||
|
self.logger.log(logging.INFO, message) # No syslog equivalent for SUCCESS
|
||||||
self.log.append((LOG_SUCCESS, message))
|
self.log.append((LOG_SUCCESS, message))
|
||||||
|
|
||||||
def log_info(self, message):
|
def log_info(self, message):
|
||||||
|
self.logger.log(logging.INFO, message)
|
||||||
self.log.append((LOG_INFO, message))
|
self.log.append((LOG_INFO, message))
|
||||||
|
|
||||||
def log_warning(self, message):
|
def log_warning(self, message):
|
||||||
|
self.logger.log(logging.WARNING, message)
|
||||||
self.log.append((LOG_WARNING, message))
|
self.log.append((LOG_WARNING, message))
|
||||||
|
|
||||||
def log_failure(self, message):
|
def log_failure(self, message):
|
||||||
|
self.logger.log(logging.ERROR, message)
|
||||||
self.log.append((LOG_FAILURE, message))
|
self.log.append((LOG_FAILURE, message))
|
||||||
|
|
||||||
# Convenience functions
|
# Convenience functions
|
||||||
@ -381,6 +388,10 @@ def run_script(script, data, request, commit=True):
|
|||||||
start_time = None
|
start_time = None
|
||||||
end_time = None
|
end_time = None
|
||||||
|
|
||||||
|
script_name = script.__class__.__name__
|
||||||
|
logger = logging.getLogger(f"netbox.scripts.{script.module()}.{script_name}")
|
||||||
|
logger.info(f"Running script (commit={commit})")
|
||||||
|
|
||||||
# Add files to form data
|
# Add files to form data
|
||||||
files = request.FILES
|
files = request.FILES
|
||||||
for field_name, fileobj in files.items():
|
for field_name, fileobj in files.items():
|
||||||
@ -403,6 +414,7 @@ def run_script(script, data, request, commit=True):
|
|||||||
script.log_failure(
|
script.log_failure(
|
||||||
"An exception occurred: `{}: {}`\n```\n{}\n```".format(type(e).__name__, e, stacktrace)
|
"An exception occurred: `{}: {}`\n```\n{}\n```".format(type(e).__name__, e, stacktrace)
|
||||||
)
|
)
|
||||||
|
logger.error(f"Exception raised during script execution: {e}")
|
||||||
commit = False
|
commit = False
|
||||||
finally:
|
finally:
|
||||||
if not commit:
|
if not commit:
|
||||||
@ -415,6 +427,7 @@ def run_script(script, data, request, commit=True):
|
|||||||
# Calculate execution time
|
# Calculate execution time
|
||||||
if end_time is not None:
|
if end_time is not None:
|
||||||
execution_time = end_time - start_time
|
execution_time = end_time - start_time
|
||||||
|
logger.info(f"Script completed in {execution_time:.4f} seconds")
|
||||||
else:
|
else:
|
||||||
execution_time = None
|
execution_time = None
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user