1

I am running Ubuntu 14.04 on my notebook and I am using Django 1.6.4 (with virtualenv) together with Apache 2.4.7.

I set up a site and want to log from my views some debugging information to my logfile called homepage.log (the name of my app is homepage). When I run my site with Django's built-in server all messages are shown in the logfile but when running with Apache no message lands in the file. My project is located under /home/nick/Workspace/Web/kleyboldt_django/ (kleyboldt is the name of the guy I am writting for). There I have my wsgi file, my virtual environment called env and my actual project called kleyboldt-homepage. I would like to log in kleyboldt_homepage/homepage.log. My setup is:

/etc/apache2/sites-available/mks.conf

WSGIDaemonProcess mks.com python-path=/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage:/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages
WSGIProcessGroup mks.com

<VirtualHost *:80>
    ...
    <Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static>
        Require all granted
    </Directory>

    <Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/media>
        Require all granted
    </Directory>

    WSGIScriptAlias / /home/nick/Workspace/Web/kleyboldt_django/kleyboldt.wsgi

    ServerName mks.com

    <Directory /home/nick/Workspace/Web/kleyboldt_django/>
        Require all granted
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/>
        Require all granted
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/homepage.log
    LogLevel debug
</VirtualHost>

/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/kleyboldt_homepage/settings.py

...
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s:\t %(message)s',
            'datefmt': '%d/%B/%Y %H:%M:%S',
        },
        'simple': {
            'format': '%(levelname)s %(message)s',
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'django.utils.log.NullHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',


  'formatter': 'simple',
    },
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': 'homepage.log',
        'formatter': 'verbose',
    }
},
'loggers': {
    'kleyboldt_homepage': {
        'handlers': ['console', 'file'],
        'level': 'DEBUG',
    },
},

}

/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/homepage/views.py

import logging

logger = logging.getLogger('kleyboldt_homepage')

def home(request):
    logger.warning("test")
    return render(request, 'homepage/home.html', {})

When running Apache the warning never appears.

Update 1

I changed the logfile to /tmp/homepage.log but then Apache shows an error message telling me that a misconfiguration was discovered. Because I set the error log in my VirtualHost to my logfile for my homepage Apache dumped the error message in it.

/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/homepage.log

[Sat May 03 14:00:04.996997 2014] [authz_core:debug] [pid 16747] mod_authz_core.c(802): [client 127.0.0.1:47319] AH01626: authorization result of Require all granted: granted, referer: http://mks.com/
[Sat May 03 14:00:04.997084 2014] [authz_core:debug] [pid 16747] mod_authz_core.c(802): [client 127.0.0.1:47319] AH01626: authorization result of <RequireAny>: granted, referer: http://mks.com/
[Sat May 03 14:00:04.997272 2014] [authz_core:debug] [pid 16747] mod_authz_core.c(802): [client 127.0.0.1:47319] AH01626: authorization result of Require all granted: granted, referer: http://mks.com/
[Sat May 03 14:00:04.997295 2014] [authz_core:debug] [pid 16747] mod_authz_core.c(802): [client 127.0.0.1:47319] AH01626: authorization result of <RequireAny>: granted, referer: http://mks.com/
[Sat May 03 14:00:05.015130 2014] [:info] [pid 16746] [remote 127.0.0.1:36049] mod_wsgi (pid=16746, process='mks.com', application='mks.com|'): Loading WSGI script '/home/nick/Workspace/Web/kleyboldt_django/kleyboldt.wsgi'.
[Sat May 03 14:00:05.187017 2014] [:error] [pid 16746] [remote 127.0.0.1:36049] mod_wsgi (pid=16746): Exception occurred processing WSGI script '/home/nick/Workspace/Web/kleyboldt_django/kleyboldt.wsgi'.
[Sat May 03 14:00:05.187112 2014] [:error] [pid 16746] [remote 127.0.0.1:36049] Traceback (most recent call last):
[Sat May 03 14:00:05.187145 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]   File "/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Sat May 03 14:00:05.187282 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]     self.load_middleware()
[Sat May 03 14:00:05.187329 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]   File "/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 46, in load_middleware
[Sat May 03 14:00:05.187481 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Sat May 03 14:00:05.187542 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]   File "/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
[Sat May 03 14:00:05.187652 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]     self._setup(name)
[Sat May 03 14:00:05.187710 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]   File "/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/django/conf/__init__.py", line 50, in _setup
[Sat May 03 14:00:05.187746 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]     self._configure_logging()
[Sat May 03 14:00:05.187793 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]   File "/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/django/conf/__init__.py", line 80, in _configure_logging
[Sat May 03 14:00:05.187827 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]     logging_config_func(self.LOGGING)
[Sat May 03 14:00:05.187887 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]   File "/usr/lib/python2.7/logging/config.py", line 794, in dictConfig
[Sat May 03 14:00:05.188141 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]     dictConfigClass(config).configure()
[Sat May 03 14:00:05.188182 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]   File "/usr/lib/python2.7/logging/config.py", line 576, in configure
[Sat May 03 14:00:05.188218 2014] [:error] [pid 16746] [remote 127.0.0.1:36049]     '%r: %s' % (name, e))
[Sat May 03 14:00:05.188255 2014] [:error] [pid 16746] [remote 127.0.0.1:36049] ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '/tmp/homepage.log'

Obviously Apache does not have the permissions to write to this file. Therefore I changed the group of /tmp/homepage.log to www. Now writting debug messages to this file works. Now I changed the group of homepage.log in my project also to www. Unfortunately it does not work. The error log shows that Apache does not have permissions to write in my project folder. What should I change?

2 Answers 2

1

Try setting the logfilename to a full path for now. 'filename': 'homepage.log', try 'filename': '/tmp/homepage.log',

If that works, you can use os.path to set it relative to your app folder.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, I tried this. I changed homepage.log to /tmp/homepage.log but I had the same issue. With the built-in server everthing works. But after I reloaded the apache2 service I got an error message that the server discovered a misconfiguration.
Well, it is a tricky issue with file permissions. For Apache to be able to write to a file, it needs "execute" permissions on the folder the file is in. Try su'ing as the user apache to see if you can write to the file.
I changed all permissions to 777, but it still does not work. There nothing in the error log when I search for log or homepage. I see the results of loading the wsgi file but nothing more.
@Nick Well, you need to give proper permissions all the way up. /home/nick/Workspace/Web/kleyboldt_django/ so every parent folder has to be readable.
0

After some attempts I fixed the problem by changing the user of homepage.log to www-data and reloading Apache2. It could happen that you see an error message when you reload the server and then open the site immediatately.

2 Comments

The proper etiquette here is to mark it as answer if you were given the answer. Just saying...
Well, but I should tell the folks out there how I solved it right? But thank you for your remark. Unfortunately I have to accept an answer before the question is marked as answered and this is possible in two days according to the forum rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.