【发布时间】:2014-05-03 11:25:20
【问题描述】:
我在我的笔记本上运行 Ubuntu 14.04,我正在使用 Django 1.6.4(带有 virtualenv)和 Apache 2.4.7。
我建立了一个站点并希望从我的视图中将一些调试信息记录到我的名为homepage.log 的日志文件中(我的应用程序的名称是主页)。当我使用 Django 的内置服务器运行我的站点时,所有消息都显示在日志文件中,但是当使用 Apache 运行时,文件中没有消息。我的项目位于/home/nick/Workspace/Web/kleyboldt_django/ 下(kleyboldt 是我为之写作的人的名字)。那里有我的 wsgi 文件、名为 env 的虚拟环境和名为 kleyboldt-homepage 的实际项目。我想登录kleyboldt_homepage/homepage.log。我的设置是:
/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', {})
运行 Apache 时,警告永远不会出现。
更新 1
我将日志文件更改为/tmp/homepage.log,但随后 Apache 显示一条错误消息,告诉我发现了错误配置。因为我将 VirtualHost 中的错误日志设置为主页的日志文件,Apache 将错误消息转储在其中。
/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'
显然 Apache 没有写入该文件的权限。因此,我将/tmp/homepage.log 的组更改为www。现在将调试消息写入此文件即可。现在我将项目中的homepage.log 组也更改为www。不幸的是,它不起作用。错误日志显示 Apache 没有权限写入我的项目文件夹。我应该改变什么?
【问题讨论】:
标签: python django logging apache2 django-views