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):

import logging.handlers
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

5 Comments

Add a Comment
  1. on 3.7.3 setup in docker, this gives me:

    ERROR: for netbox-housekeeping Container “24bcefa43391” is unhealthy.
    ERROR: Encountered errors while bringing up the project.
    root@netbox02:/pack/netbox-test# docker container logs -f 24bcefa43391
    ValueError: Unable to configure handler ‘file’
    [ Use DB_WAIT_DEBUG=1 in netbox.env to print full traceback for errors here ]
    ⏳ Waiting on DB… (0s / 30s)
    ValueError: Unable to configure handler ‘file’
    [ Use DB_WAIT_DEBUG=1 in netbox.env to print full traceback for errors here ]
    ⏳ Waiting on DB… (3s / 30s)
    ValueError: Unable to configure handler ‘file’
    [ Use DB_WAIT_DEBUG=1 in netbox.env to print full traceback for errors here ]
    ⏳ Waiting on DB… (6s / 30s)
    ValueError: Unable to configure handler ‘file’
    [ Use DB_WAIT_DEBUG=1 in netbox.env to print full traceback for errors here ]
    ⏳ Waiting on DB… (9s / 30s)
    ValueError: Unable to configure handler ‘file’
    [ Use DB_WAIT_DEBUG=1 in netbox.env to print full traceback for errors here ]
    ⏳ Waiting on DB… (12s / 30s)
    ValueError: Unable to configure handler ‘file’
    [ Use DB_WAIT_DEBUG=1 in netbox.env to print full traceback for errors here ]
    ⏳ Waiting on DB… (15s / 30s)
    ValueError: Unable to configure handler ‘file’

    1. I added the missing “import logging.handlers” statement in the post, please see if that helps.

      1. Normann P. Nielsen

        Thanks. Same problem, though.

        1. In that case I’d recommend asking in the netbox-docker community discussions: https://github.com/netbox-community/netbox-docker/discussions

          1. Normann P. Nielsen

            Ok, no problem 🙂 Thanks for the attempt.

Leave a Reply