Django Logging with FileHandler not working

I am using the logging setting below with the django project (also using clock / raven). The watchdog / raven bit works fine, but there is no file registration. An empty log file is created, but whenever I use logging.info('foo') , nothing appears in the log file (i.e., it remains empty). Any suggestions?

 LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'root': { 'level': 'WARNING', 'handlers': ['sentry'], }, 'formatters': { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' }, }, 'handlers': { 'sentry': { 'level': 'ERROR', 'class': 'raven.contrib.django.handlers.SentryHandler', }, 'file': { 'level': 'INFO', 'class': 'logging.FileHandler', 'filename': '/var/log/django/breeding.log', }, 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'verbose' } }, 'loggers': { 'django.db.backends': { 'level': 'ERROR', 'handlers': ['console'], 'propagate': False, }, 'raven': { 'level': 'DEBUG', 'handlers': ['console'], 'propagate': False, }, 'sentry.errors': { 'level': 'DEBUG', 'handlers': ['console'], 'propagate': False, }, }, } 
+5
source share
3 answers

I ran into this problem. This turned out to be a permission issue. When I started the development server for the first time after setting up logging, it created the file /var/log/django/request.log , which belongs to my local user (stretching), with mode 644.

When I started the "production" server (nginx / uwsgi), the service will work as the www-data user and will not be able to open /var/log/django/request.log for writing. Just deleting the log file and restarting uwsgi was enough for this to happen, but I would have to come up with a more elegant long-term fix.

+2
source

To do this, you must add a "file" handler for each registrar, for example:

 'loggers': { 'django.db.backends': { 'level': 'ERROR', 'handlers': ['file','console'], 'propagate': False, }, 'raven': { 'level': 'DEBUG', 'handlers': ['file','console'], 'propagate': False, }, 'sentry.errors': { 'level': 'DEBUG', 'handlers': ['file','console'], 'propagate': False, }, }, 

Otherwise, the registrars do not write anything to the specified file.

+1
source

This is a quick answer .


More:

In your Django recording settings, follow this scenario:

 LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'verbose': { 'format': '%(levelname)3.3s %(asctime)22.22s [%(name)s:%(funcName)s] {%(process)d} %(message)s' } }, 'handlers': { 'sentry-warn': { 'level': 'WARNING', 'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler', }, 'sentry-info': { 'level': 'INFO', 'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler', }, 'sentry-error': { 'level': 'ERROR', 'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler', }, 'console': { 'level': 'WARNING', 'class': 'logging.StreamHandler', 'formatter': 'verbose' }, 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler', } }, 'loggers': { 'sentry': { 'level': 'DEBUG', 'handlers': ['sentry-warn', 'console', 'file', 'sentry-info', 'sentry-error'], 'propagate': False, }, 'sentry-warn': { 'level': 'DEBUG', 'handlers': ['sentry-warn', 'console', 'file'], 'propagate': False, }, 'sentry-error': { 'level': 'DEBUG', 'handlers': ['console', 'file', 'sentry-error'], 'propagate': False, }, 'sentry-info': { 'level': 'DEBUG', 'handlers': ['console', 'file', 'sentry-info'], 'propagate': False, }, 'django': { 'handlers': ['console', 'file', 'mail_admins'], 'propagate': False, }, }, 'root': { 'handlers': ['console', 'file', 'mail_admins'], 'level': 'INFO' }, } 

Testing:

 from logging import getLogger logger = getLogger('sentry') log_war = getLogger('sentry-warn') log_inf = getLogger('sentry-info') log_err = getLogger('sentry-error') logger.warning('warn') logger.info('info') logger.error('error') log_err.error('new error') log_war.warning('new warn') log_inf.info('new info') 
0
source

All Articles