【问题标题】:Logging message in Django running through Apache在通过 Apache 运行的 Django 中记录消息
【发布时间】: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


    【解决方案1】:

    现在尝试将日志文件名设置为完整路径。 'filename': 'homepage.log', 试试 'filename': '/tmp/homepage.log',

    如果可行,您可以使用 os.path 将其设置为相对于您的应用文件夹。

    【讨论】:

    • 好的,我试过了。我将homepage.log 更改为/tmp/homepage.log,但我遇到了同样的问题。使用内置服务器一切正常。但是在我重新加载 apache2 服务后,我收到一条错误消息,表明服务器发现了错误配置。
    • 嗯,文件权限是个棘手的问题。为了让 Apache 能够写入文件,它需要对文件所在的文件夹具有“执行”权限。尝试以 apache 用户身份 su'ing 以查看是否可以写入文件。
    • 我把所有权限都改成777了,还是不行。当我搜索 loghomepage 时,错误日志中没有任何内容。我看到了加载 wsgi 文件的结果,但仅此而已。
    • @Nick 好吧,您需要一直提供适当的权限。 /home/nick/Workspace/Web/kleyboldt_django/ 所以每个父文件夹必须是可读的。
    【解决方案2】:

    经过一些尝试,我通过将 homepage.log 的用户更改为 www-data 并重新加载 Apache2 来解决问题。当您重新加载服务器然后立即打开站点时,您可能会看到一条错误消息。

    【讨论】:

    • 这里正确的礼仪是,如果你得到了答案,就把它标记为答案。只是说...
    • 好吧,但是我应该告诉那里的人们我是如何解决它的,对吗?但是谢谢你的评论。不幸的是,我必须在问题被标记为已回答之前接受答案,根据论坛规则,这可以在两天内完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多