Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure django logging for production under gunicorn server?

I have this logging configuration set up:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(asctime)s %(levelname)s [%(name)s:%(lineno)s] %(module)s %(process)d %(thread)d %(message)s'
        }
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'gunicorn.errors': {
            'level': 'INFO',
            'handlers': ['console'],
            'propagate': True,
        },
    }
}

It seems to have no effect at all. When the flag DEBUG is set to True then I can see some errors in the console. When however it's set to False, I cannot. So how to log the errors to the console despite DEBUG flag being set one way or the other?

like image 974
Marek M. Avatar asked Feb 08 '17 20:02

Marek M.


People also ask

How do I enable logging in Django?

By default, the LOGGING setting is merged with Django's default logging configuration using the following scheme. If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the dictConfig default if the key is missing) then all loggers from the default configuration will be disabled.

How do I get Gunicorn logs?

In version 19.0, Gunicorn doesn't log by default in the console. To watch the logs in the console you need to use the option --log-file=- . In version 19.2, Gunicorn logs to the console by default again.


2 Answers

In django 1.9 the default logging configuration has been changed:

Changes to the default logging configuration¶

To make it easier to write custom logging configurations, Django’s default logging configuration no longer defines ‘django.request’ and ‘django.security’ loggers. Instead, it defines a single ‘django’ logger, filtered at the INFO level, with two handlers:

‘console’: filtered at the INFO level and only active if DEBUG=True. ‘mail_admins’: filtered at the ERROR level and only active if DEBUG=False. If you aren’t overriding Django’s default logging, you should see minimal changes in behavior, but you might see some new logging to the runserver console, for example.

If you are overriding Django’s default logging, you should check to see how your configuration merges with the new defaults.

So what you need to do is simply override the default config, add this to your loggers:

'django':{
    'level': 'INFO',
    'handlers': ['console'],
    'propagate': True,
}  

Logging to console wont depend on DEBUG now.

Hope this helps!

like image 134
HassenPy Avatar answered Nov 15 '22 03:11

HassenPy


Try adding --capture-output and --enable-stdio-inheritance with gunicorn

/home/ubuntu/inside-env/bin/gunicorn --access-logfile access.log --error-logfile error.log --capture-output --enable-stdio-inheritance --workers 3 --bind unix:/home/ubuntu/path-to-project/webapp.sock project.wsgi:application

and then in your views,

import logging 
logging.basicConfig(level='DEBUG')
logging.info('...something...')
like image 39
SuperNova Avatar answered Nov 15 '22 02:11

SuperNova