I am using the logging setup below with a django project (also using sentry/raven). The sentry/raven bit is working fine, but the file logging isn't. An empty logfile is created, but whenever I use logging.info('foo')
nothing comes up 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,
},
},
}
I ran into this same issue. It turned out to be a permissions problem. When I ran the development server for the first time after configuring logging, it created the file /var/log/django/request.log
owned by my local user (stretch) with mode 644.
When I started the "production" server (nginx/uwsgi), the service would run as the www-data user and was unable to open /var/log/django/request.log
for writing. Simply deleting the log file and restarting uwsgi was enough to get it going, but I'll have to come up with a more elegant long-term fix.
In order to make it work you have to add the 'file' handler to each logger, like this:
'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 loggers won't write anything to the specified file.
This is a Quick Answer.
More complete:
In Django logging setting follow this script:
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')