简体   繁体   English

在皇帝模式下在uWSGI下运行金字塔应用程序时无法记录日志

[英]Logging not working when running pyramid app under uWSGI in emperor mode

First, a little background. 首先,有一点背景。 I'm running an app under uWSGI in emperor mode. 我在uWSGI下以皇帝模式运行一个应用程序。 The uWSGI command line: uWSGI命令行:

uwsgi --master --emperor /etc/uwsgi --die-on-term --uid uwsgi --gid uwsgi

The INI file for my app is in /etc/uwsgi and it is successfully found when uWSGI starts. 我的应用程序的INI文件位于/ etc / uwsgi中,并在uWSGI启动时成功找到该文件。 My app's uwsgi and logging configuration sections: 我的应用程序的uwsgi和日志记录配置部分:

[uwsgi]
socket = /tmp/uwsgi.sock
master = true
processes = 8
threads = 4
harakiri = 60
harakiri-verbose = true
limit-post = 52428800
post-buffering = 8192
listen = 256
max-requests = 1000
buffer-size = 32768
no-orphans = true
logto = /var/log/uwsgi/my_app.log
log-slow = 1000
virtualenv = /usr/local/python/my_app
paste = config:%p

[loggers]
keys = root, my_app

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console

[logger_my_app]
level = DEBUG
handlers =
qualname = my_app

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

Nginx is configured to proxy the site, and does so successfully. Nginx被配置为代理站点,并且成功完成了代理。 The app starts and runs fine. 该应用程序启动并运行正常。 I have no problem getting to it from a web browser. 我从网络浏览器上获取它没有问题。 The problem starts when I try to log something from my app like so: 当我尝试从应用程序中记录某些内容时,问题就开始了:

import logging
log = logging.getLogger(__name__)
log.info('hello world')

In my log, instead of "hello world", I see the following: 在我的日志中,看到的不是“ hello world”,而是:

No handlers could be found for logger "my_app.my_module" 找不到记录程序“ my_app.my_module”的处理程序

It appears that uWSGI is not picking up the logging configuration from my INI file. 看来uWSGI并未从我的INI文件中获取日志记录配置。 I've seen people suggest using the "--ini-paste-logged my.ini" option on the uWSGI command line, but this won't work in my case since I'm using emperor mode. 我见过有人建议在uWSGI命令行上使用“ --ini-paste-logged my.ini”选项,但是由于我使用的是皇帝模式,因此这在我的情况下不起作用。

Others have suggested calling pyramid.paster.setup_logging from within the app, but this seems like a non-ideal solution. 其他人建议从应用程序中调用pyramid.paster.setup_logging,但这似乎是非理想的解决方案。 Firstly, during development, I run the app locally with pserve which calls setup_logging automatically, so I'd have to call it conditionally depending on which environment the app is running in. Secondly, setup_logging requires the path to the config file as an argument, and I need to be able to use different configs for staging and production environments, so it seems I'd have to do something like add a setting in the config file that specifies the path to itself. 首先,在开发过程中,我使用pserve在本地运行应用程序,该服务会自动调用setup_logging,因此必须根据应用程序所处的环境有条件地对其进行调用。其次,setup_logging需要将配置文件的路径作为参数,而且我需要能够在登台和生产环境中使用不同的配置,因此看来我必须要做一些类似的事情,例如在配置文件中添加一个设置,以指定其自身的路径。 A convoluted solution at best. 最好是一个复杂的解决方案。

Any idea how I can get uWSGI to pick up my logging configuration without jumping through a bunch of hoops? 知道如何让uWSGI来获取我的日志记录配置,而不必经历一堆麻烦吗?

Figured it out and might as well leave it here for posterity since there seems to be precious little content on the web about emperor mode. 弄清楚了它,不妨将其留在这里供后代使用,因为网络上关于皇帝模式的内容似乎很少。 I added "paste-logger = %p" to the uwsgi section of my INI file, and logging appears to work now. 我在INI文件的uwsgi部分添加了“ paste-logger =%p”,并且日志记录现在可以正常工作。 Full uwsgi section for context: 有关上下文的完整uwsgi部分:

[uwsgi]
socket = /tmp/uwsgi.sock
master = true
processes = 8
threads = 4
harakiri = 60
harakiri-verbose = true
limit-post = 52428800
post-buffering = 8192
listen = 256
max-requests = 1000
buffer-size = 32768
no-orphans = true
logto = /var/log/uwsgi/my_app.log
log-slow = 1000
virtualenv = /usr/local/python/my_app
paste = config:%p
paste-logger = %p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM