Просмотр исходного кода

Enable system logging for custom scripts

Jeremy Stretch 6 лет назад
Родитель
Сommit
36cbbac870
2 измененных файлов с 14 добавлено и 0 удалено
  1. 1 0
      docs/configuration/logging.md
  2. 13 0
      netbox/extras/scripts.py

+ 1 - 0
docs/configuration/logging.md

@@ -6,4 +6,5 @@
 |---------------------------------|--------------------------------------------------------|
 | `netbox.auth.*`                 | Authentication events                                  |
 | `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       |

+ 13 - 0
netbox/extras/scripts.py

@@ -1,5 +1,6 @@
 import inspect
 import json
+import logging
 import os
 import pkgutil
 import time
@@ -260,6 +261,7 @@ class BaseScript:
     def __init__(self):
 
         # Initiate the log
+        self.logger = logging.getLogger(f"netbox.scripts.{self.module()}.{self.__class__.__name__}")
         self.log = []
 
         # Declare the placeholder for the current request
@@ -307,18 +309,23 @@ class BaseScript:
     # Logging
 
     def log_debug(self, message):
+        self.logger.log(logging.DEBUG, message)
         self.log.append((LOG_DEFAULT, message))
 
     def log_success(self, message):
+        self.logger.log(logging.INFO, message)  # No syslog equivalent for SUCCESS
         self.log.append((LOG_SUCCESS, message))
 
     def log_info(self, message):
+        self.logger.log(logging.INFO, message)
         self.log.append((LOG_INFO, message))
 
     def log_warning(self, message):
+        self.logger.log(logging.WARNING, message)
         self.log.append((LOG_WARNING, message))
 
     def log_failure(self, message):
+        self.logger.log(logging.ERROR, message)
         self.log.append((LOG_FAILURE, message))
 
     # Convenience functions
@@ -381,6 +388,10 @@ def run_script(script, data, request, commit=True):
     start_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
     files = request.FILES
     for field_name, fileobj in files.items():
@@ -403,6 +414,7 @@ def run_script(script, data, request, commit=True):
         script.log_failure(
             "An exception occurred: `{}: {}`\n```\n{}\n```".format(type(e).__name__, e, stacktrace)
         )
+        logger.error(f"Exception raised during script execution: {e}")
         commit = False
     finally:
         if not commit:
@@ -415,6 +427,7 @@ def run_script(script, data, request, commit=True):
     # Calculate execution time
     if end_time is not None:
         execution_time = end_time - start_time
+        logger.info(f"Script completed in {execution_time:.4f} seconds")
     else:
         execution_time = None