Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gunicorn Django and logging info to a file

I'm trying to setup my logging settings to send logging.info('any message') to a file through stdout.

This is my gunicorn_django script:

$ gunicorn_django -w $NUM_WORKERS --user=$USER --group=$GROUP --log-level=info --log-file=$LOGFILE &>>$LOGFILE

These are my logging settings:

import sys
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'filters': ['require_debug_false'],
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
            'formatter': 'simple',
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'app.location': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': False,
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

When I do:

import logging
logging.getLogger('app.location')
logging.info('any message')

It is not logged to the gunicorn_django $LOGFILE. Just print >> sys.stderr messages seem to be showed in the $LOGFILE

How can I log info messages (through stdout) to a file

like image 514
Jose S Avatar asked May 08 '13 17:05

Jose S


1 Answers

Your logger is just getting garbage collected as soon as you create it. You need to keep a reference to it as a logger, and then use that logger, like so:

import logging


logger = logging.getLogger('app.location')
logger.info('any message')
like image 89
orokusaki Avatar answered Sep 28 '22 22:09

orokusaki