Configuring Logging in NetBox

Creating the log file and setting the permissions:

$ sudo mkdir /var/log/netbox
$ sudo touch /var/log/netbox/netbox.log
$ sudo chown -R netbox.netbox /var/log/netbox
$

Configuring NetBox (in /opt/netbox/netbox/netbox/configuration.py):

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'normal': {
            'format': '%(asctime)s %(name)s %(levelname)s: %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': '/var/log/netbox/netbox.log',
            'formatter': 'normal',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'INFO',
        },
        'netbox': {
            'handlers': ['file'],
            'level': 'INFO',
        },
    },
}

You can set the level for the loggers as needed, in this setup the file handler accepts DEBUG and higher but the actual loggers (“django” and “netbox”) only log at INFO and above.

Note: If/when running NetBox’ manage.py commands manually, you may need to adjust the permissions of the log file temporarily (or disable logging temporarily by adding an extra LOGGING = {} after the existing configuration). Otherwise the Python process is not able to write to the log file and it will result in errors. Just remember to revert the change when done.

Example of logging output:

2022-09-08 12:54:29,048 django.request WARNING: Not Found: /favicon.ico
2022-09-08 12:54:43,724 netbox.api.views.ModelViewSet INFO: Updating IP address 172.19.11.59/24 (PK: 11367)
2022-09-08 12:54:43,783 netbox.webhooks_worker INFO: Sending POST request to http://127.0.0.1:8080/webhook/ (ipaddress updated)
2022-09-08 12:54:43,790 netbox.webhooks_worker INFO: Request succeeded; response status 200

Configuring log rotation (/etc/logrotate.d/netbox):

/var/log/netbox/netbox.log
{
        rotate 53
        weekly
        missingok
        notifempty
        compress
}

See also the NetBox documentation: https://docs.netbox.dev/en/stable/configuration/system/#logging

Leave a Reply