简体   繁体   中英

Logging in Django and gunicorn

I'm running django application with gunicorn, and I can't see any log messages I'm wriging.

Here is the code that writes the logs:

logger = logging.getLogger(__name__)

def home_page(request):
    logger.warning('in home page')

(note: this code definitely runs, since this is a view that leads to the homepage)

This is my logs configuration from settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
        },
    },
    'root': {'level': 'INFO'},
}

I run gunicorn as daemon with the following arguments:

--access-logfile ../access.log --error-logfile --log-level debug ../error.log

Both access.log and error.log are created and filled with gunicorn messages, but I can't see the messages I write.

Thanks

I have solved my problem. Providing the details so it might help somebody with similar issue.

Decided not to mix up with gunicorn and django logs and to create separate log file for django.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
        'logfile': {
            'level':'DEBUG',
            'class':'logging.FileHandler',
            'filename': BASE_DIR + "/../logfile",
        },
    },
    'root': {
        'level': 'INFO',
        'handlers': ['console', 'logfile']
    },
}

With this configuration every message written with severity >= INFO will be written to file "logfile" located just outside of source directory.

I'd like to add a 2021 answer for this, as server logs are so helpful and I couldn't find the answer I was looking for without having to merge a couple of different examples. I'm using django==3.1 and gunicorn==20.0.4 in a Dockerized application that's hosted on Heroku. I just wanted my Django server logs to show up both in my local stdout when I ran docker-compose up, and in papertrail logs on Heroku. This is working for me.

# In Dockerfile, I created a location for the logs, 
# and point there when starting gunicorn
RUN mkdir /logs
CMD gunicorn app.wsgi:application --bind 0.0.0.0:$PORT --workers 3 --capture-output --access-logfile /logs/gunicorn-access.log

Then in the Django settings, I used this for the logging_dict:

import logging.config
# Clear prev config
LOGGING_CONFIG = None
logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'console': {
            'format': '%(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'console',
        },
    },
    'loggers': {
        'gunicorn': { # this was what I was missing, I kept using django and not seeing any server logs
            'level': 'INFO',
            'handlers': ['console'],
            'propagate': True,
        },
    },

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM