leo
leo

Reputation: 379

Django StreamHandler log not working in mod_wsgi setup

I'm trying to get all log messages to go to the standard apache error log, and I just can't get it working. If I write to a file it's fine, but the apache logs flat out don't work. Any advice? Here's my relevant config:

settings.py (snippet)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'filters': {
        'require_debug_false': {
           '()': 'django.utils.log.RequireDebugFalse'
        }
    },
   'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
        }
    },
    'handlers': {
        'development': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'standard',
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
    },
    'loggers': {
        '':{
            'handlers': ['development'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.db.backends': {
            'handlers': ['development'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        }
    }
}

virtual host config

<VirtualHost *:80>
    SetEnv DJANGO_DEBUG True

    ServerName virtualimpact.userhome.test-env.net

    LogLevel debug
    ErrorLog   "/home/developer/logs/developer-machine_error.log"
    CustomLog "/home/developer/logs/developer-machine_com_access.log" combined

    Alias /static/ /home/developer/htdocs/path/to/htdocs/static/

    <Directory /home/developer/htdocs/path/to/htdocs/static>
      Order deny,allow
      Allow from all
    </Directory>

    WSGIScriptAlias / /home/developer/htdocs/path/to/mod_wsgi/django.wsgi
</VirtualHost>

django.wsgi

import os, sys, site

workspace = os.path.abspath('%s/..' % os.path.dirname(__file__))
activate_this = os.path.abspath('%s/virtpy/bin/activate_this.py' % workspace)
execfile(activate_this, dict(__file__=activate_this))

sys.path.append(workspace)
sys.path.append('%s/htdocs' % workspace)

os.environ['DJANGO_SETTINGS_MODULE'] = 'htdocs.settings'
os.environ['PYTHON_EGG_CACHE'] = '%s/mod_wsgi/egg-cache' % workspace

import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()

def application(environ, start_response):
    os.environ['DJANGO_DEBUG'] = environ.get('DJANGO_DEBUG', 'False')
    return _application(environ, start_response)

views.py (snippet)

import logging
logger = logging.getLogger('')
...

class AbstractsListView(BaseListView):
    def get_context_data(self, **kwargs):
        # none of the below calls write to the error log
        logger.debug('debug it')
        logger.info('info it')
        import sys
        sys.stderr.write('asdfasdf')
        ...

If you need any more information please let me know, this thing has been bugging me for a while and I'm at wits end about it.

Thanks in advance.

edit: also, I'm using Python 2.6.6 and Django==1.4.3

edit: it's logging, but to the wrong file!

So I don't know why I didn't check before, but it turns out anything I write to stdout (or stderr) is going to the logs in /var/log/httpd/, and not the logs I've configured specifically for this VirtualHost. I'm wondering if this is more of a mod_wsgi question than Django, but I can't be sure. Are my mod_wsgi and vhost.conf file correct?

Thanks,

Upvotes: 0

Views: 1945

Answers (1)

leo
leo

Reputation: 379

Okay, it seems like this stems from the fact I'm running mod_wsgi under embedded mode and not as a daemon. Soon as I switched it over, my logs started to appear in the correct place. We'll see how it fares, but so far so good.

Upvotes: 1

Related Questions