Search code examples
pythondjangosyslog

Django: Unable to configure handler 'syslog'


I make docker image from django project, and it started to create error that I never had before. The error is seems like related to logging, specifically syslog. At first, I thought that missing /dev/log in docker image creates an error. So I added line to docker file to create /dev/ folder and log file. However, The error did not disappear.

This is error logs.

 File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
autoreload.raise_last_exception()
File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
six.reraise(*_exception)
File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/usr/local/lib/python2.7/site-packages/django/utils/log.py", line 75, in configure_logging
logging_config_func(logging_settings)
File "/usr/local/lib/python2.7/logging/config.py", line 794, in dictConfig
dictConfigClass(config).configure()
File "/usr/local/lib/python2.7/logging/config.py", line 576, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'syslog': [Errno 2] No such file or directory

This is logging related settings.

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
    'mail_admins': {
        'level': 'ERROR',
        'class': 'django.utils.log.AdminEmailHandler'
    },
    'syslog': {
        'level': 'DEBUG',
        'class': 'logging.handlers.SysLogHandler',
        'facility': 'user',
        'address': '/dev/log',
    },
    'null': {
        'level': 'DEBUG',
        'class': 'logging.NullHandler',
    },
    'stream': {
        'level': 'INFO',
        'class': 'logging.StreamHandler'
    },
},
'loggers': {
    'root': {
        'handlers': ['syslog', 'mail_admins'],
        'level': 'ERROR',
    },
    'SocialAuth': {
        'handlers': ['mail_admins'],
        'level': 'WARN',
    },
    'django': {
        'handlers': ['syslog', 'mail_admins'],
        'level': 'ERROR',
    },
 }

I created folder and path /dev/log, but seems like that is not the problem.


Solution

  • The first problem is that there is no /dev/log in your filesystem (/dev is usually mounted as a tmpfs, not as a drive-backed FS).

    And the next issue is that that the SysLogHandler (logging.handlers.SysLogHandler) requires the address to be a UNIX domain socket, not anything else. So, you need to create a UNIX socket /dev/log and pass on the address in Django SysLogHandler.


    You can create a UNIX domain sokcet via nc (netcat), if your version supports the -U option:

    nc -lU /dev/log
    

    Or you can create the socket create directly in Python mentioning the address family as AF_UNIX:

    import sokcet
    
    sock = socket.socket(socket.AF_UNIX)
    sock.bind('/dev/log')
    

    And any of the above can go as a command in Dockerfile/docker-compose.yml while creating your container.