Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why all application logs are written to uwsgi log?

I'm using dJango+uWSGI for a web project. But I found that all my logs will be written to the uwsgi log!!

The situation is that: When I write a log entry using logger.xxx, the logger I configured in settings.py will receive the log entry, but the uwsgi.log will also have a log written to that file! And the most strange things is that, in some of my projects, my application logs will be written to logs files as I configured, and all daemon process logs are written to uwsgi.log; but the other projects' application logs will ALSO be written to uwsgi.log!

Here is my logging configuration:

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
    'verbose': {
        'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
    },
    'detail': {
        'format': '%(asctime)s %(levelname)s %(module)s %(message)s'
    },
    'message_only': {
        'format': '%(asctime)s %(message)s'
    },
    'simple': {
        'format': '%(levelname)s %(asctime)s %(message)s'
    },
},
'handlers': {
    'mail_admins': {
        'level': 'ERROR',
        'class': 'django.utils.log.AdminEmailHandler'
    },
    'file':{
        'level':'DEBUG',
        'class':'logging.handlers.TimedRotatingFileHandler',
        'formatter': 'simple',
        'filename':  os.path.join(LOG_BASE, 'web.log'),
        'when': 'D',
        'backupCount' : 3
    },
    'pref':{
        'level':'DEBUG',
        'class':'logging.handlers.RotatingFileHandler',
        'formatter': 'message_only',
        'filename': os.path.join(LOG_BASE, 'pref.log'),
        'maxBytes': 10 * 1024 * 1024, # 10MB
        'backupCount' : 5
    },
    'err':{
        'level':'ERROR',
        'class':'logging.handlers.TimedRotatingFileHandler',
        'formatter': 'detail',
        'filename': os.path.join(LOG_BASE, 'err.log'),
        'when': 'D',
        'backupCount' : 3
    },
},
'loggers': {
    'django.request': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': True,
    },
    'myproject' : {
        'handlers': ['file', 'err' ],
        'level': 'INFO',
    },
    'myproject+prefs' : {
        'handlers': ['pref'],
        'level': 'DEBUG',
    }
}
}

And my uwsgi.xml:

<uwsgi>
<socket>:8888</socket>
<env>DJANGO_SETTINGS_MODULE=myproject.settings</env>
<module>django.core.handlers.wsgi:WSGIHandler()</module>
<processes>4</processes>
<master />
<master-as-root />
<!-- request timeout -->
<harakiri>15</harakiri>
<post-buffering>32768</post-buffering>
<daemonize>/var/log/myproject/uwsgi.log</daemonize>
<listen>32768</listen>
<socket-timeout>4</socket-timeout>
<disable-logging />
</uwsgi>

And here is how I use the logging:

import logging
from time import time

logger = logging.getLogger('myproject')


logger.info('my log')
like image 565
Albert Zhong Avatar asked Dec 13 '11 10:12

Albert Zhong


People also ask

What is uWSGI log?

The most basic form of logging in uWSGI is writing requests, errors, and informational messages to stdout/stderr. This happens in the default configuration. The most basic form of log redirection is the --logto / --logto2 / --daemonize options which allow you to redirect logs to files.

What is log Django?

Logging Formatters Django leverages the potential of logging by using the Python logging module by default, which provides different ways to create customized loggers through handlers, formats, and levels. The logging module is capable of: Multithreading execution. Categorizing messages via different log levels.


1 Answers

You just mix Django logging system and uWSGI.

'formatters': {
    'simple': {
        'format': '%(levelname)s | %(message)s'
    },
...
    },
'handlers': {
    'console':{
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'simple'
    },
...
},
'loggers': {
    'django.request': {
        'handlers': ['console', ],
        'level': 'DEBUG',
        'propagate': True,
        },
    },

this add logging all requests to console and this logs handle uWSGI.

'class': 'logging.StreamHandler', this is key for save log to uWSGI logs.

And you Django logs saves to 'filename': os.path.join(LOG_BASE, 'err.log'),

like image 100
b1_ Avatar answered Sep 22 '22 16:09

b1_