From a3ee46a32ae9757c88cf963a813d1b59c07b80a2 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 9 Jul 2025 10:36:18 -0400 Subject: [PATCH] Update documentation --- docs/configuration/system.md | 1 + docs/plugins/development/background-jobs.md | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/docs/configuration/system.md b/docs/configuration/system.md index 20143276c..fda1ca7ed 100644 --- a/docs/configuration/system.md +++ b/docs/configuration/system.md @@ -158,6 +158,7 @@ LOGGING = { * `netbox..` - Generic form for model-specific log messages * `netbox.auth.*` - Authentication events * `netbox.api.views.*` - Views which handle business logic for the REST API +* `netbox.jobs.*` - Background jobs * `netbox.reports.*` - Report execution (`module.name`) * `netbox.scripts.*` - Custom script execution (`module.name`) * `netbox.views.*` - Views which handle business logic for the web UI diff --git a/docs/plugins/development/background-jobs.md b/docs/plugins/development/background-jobs.md index 9be52c3ca..376df6dd2 100644 --- a/docs/plugins/development/background-jobs.md +++ b/docs/plugins/development/background-jobs.md @@ -38,6 +38,27 @@ You can schedule the background job from within your code (e.g. from a model's ` This is the human-friendly names of your background job. If omitted, the class name will be used. +### Logging + +!!! info "This feature was introduced in NetBox v4.4." + +A Python logger is instantiated by the runner for each job. It can be utilized within a job's `run()` method as needed: + +```python +def run(self, *args, **kwargs): + obj = MyModel.objects.get(pk=kwargs.get('pk')) + self.logger.info("Retrieved object {obj}") +``` + +Four of the standard Python logging levels are supported: + +* `debug()` +* `info()` +* `warning()` +* `error()` + +Log entries recorded using the runner's logger will be saved in the job's log in the database in addition to being processed by other [system logging handlers](../../configuration/system.md#logging). + ### Scheduled Jobs As described above, jobs can be scheduled for immediate execution or at any later time using the `enqueue()` method. However, for management purposes, the `enqueue_once()` method allows a job to be scheduled exactly once avoiding duplicates. If a job is already scheduled for a particular instance, a second one won't be scheduled, respecting thread safety. An example use case would be to schedule a periodic task that is bound to an instance in general, but not to any event of that instance (such as updates). The parameters of the `enqueue_once()` method are identical to those of `enqueue()`.