EsseTi
EsseTi

Reputation: 4271

Django logs in apache vhost error file, how to fix

I've a django running with WSGI and it sends logs to the error file that i setup in the vhost of apache.

the logging configuration is the following

 'django': {
        'handlers': ['logfile'],
        'level': 'INFO',
        'propagate': True,
    },
    'django.requests': {
        'handlers': ['slack_error'],
        'level': 'ERROR',
        'propagate': False,
    },
    'django.requests': {
        'handlers': ['logfile'],
        'level': 'INFO',
        'propagate': True,
    },

and this

'version': 1,
 'disable_existing_loggers': True,
 'formatters': {
     'standard': {
         'format': "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)s] %(message)s",
         'datefmt': "%d/%b/%Y %H:%M:%S"
     },
     'onlymsg': {
         'format': '%(message)s'
     },
 },
 'handlers': {
     'logfile': {
         'level': 'INFO',
         'class': 'logging.handlers.RotatingFileHandler',
         'filename': "/var/log/web.log",
         'maxBytes': 5000000,
         'backupCount': 3,
         'formatter': 'standard',
     },
     'slack_chino_error': {
         'level': 'ERROR',
         'class': 'handler.AdminSlackHandler',
         'formatter': 'onlymsg',
         'slack_url': 'https://hooks.slack.com/...',
         'channel': '#error_notification',
         'username': SERVER_NAME
 },

apache vhost is the follwing

# LOG Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
CustomLog /var/log/web_access.log combined
ErrorLog /var/log/web_error.log
LogLevel warn

Now if i open the web_error.log file it's full of django logs

[Mon Dec 12 12:16:20.054977 2016] [wsgi:error] [pid 13255:tid 139989789542144] GET console.views.generic.generic_view
[Mon Dec 12 12:18:11.222981 2016] [wsgi:error] [pid 13255:tid 139991418357504] GET console.views.user_info.UserInfo

and I think the reason is beacuse of the [wsgi:error] which I don't know why it saying so, or for other reasons that I don't know. any help on how to remove these entires and fix everhting?

Upvotes: 2

Views: 1307

Answers (1)

Graham Dumpleton
Graham Dumpleton

Reputation: 58563

Anything output to stdout or stderr from the Python WSGI application will be logged to the Apache error log. This is the preferred way of doing logging under Apache. It is not a good idea to use your own separate log file with the logging module. This is because the fact that your application can be running multi process means things like the rotating file handler from the logging module may not work properly.

Why do you believe it is bad that the Apache error log file is used when doing that is what is regarded as best practice when using Apache/mod_wsgi?

FWIW, typical Django logging configuration when using mod_wsgi would be:

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

Upvotes: 2

Related Questions