Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 244
Pavel Ryvintsev Avatar asked Dec 29 '14 21:12

Pavel Ryvintsev


People also ask

How do you get the log in Gunicorn?

¶ 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.

Where are Gunicorn logs stored?

Gunicorn Logs This log file is located at /var/log/cloudify/rest/gunicorn-access.

Is Gunicorn needed for Django?

Running a local server of Django is not a recommended way in production because it's just a test server not a production ready server. So to run Django in production is to run with Gunicorn and use Nginx as a reverse proxy so it gives more security to our application.


2 Answers

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.

like image 91
Pavel Ryvintsev Avatar answered Oct 15 '22 12:10

Pavel Ryvintsev


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,
        },
    },

})
like image 43
Anthony Roberts Avatar answered Oct 15 '22 12:10

Anthony Roberts