在 Celery 中使用 Python 标准日志记录
我必须在现有系统中实施 Celery。之前版本的系统已经使用Python标准日志记录。
我的代码类似于下面的代码。进程一和进程二是非 Celery 函数,它们到处记录日志。如果发生不良情况,我们将使用日志记录来跟踪数据丢失。
@task
def add(x,y):
process_one(x,y)
process_two(x,y)
我如何实现 Celery 并使用 Python 标准日志记录而不是 Celery 日志记录,这样我们的旧日志系统就不会丢失?
我尝试将 importlogging
从 Python 更改为:logger = add.get_logger()
并将 logger
传递给所有函数,但我不这样做不认为这是一个好的做法。我需要另一个解决方案。
更新:要在 Celery 日志记录中添加应用程序日志记录,您可以执行:
$ manage.py celeryd -v 2 -B -s celery -E -l debug --traceback \
--settings=settings --logfile=/(path to your log folder)/celeryd.log
使用 -l
(日志记录)作为 debug
,我们的应用程序/Python 日志记录是自动包含在我们的 Celery 日志记录中:无需执行 logger = add.get_logger() 。
I have to implement Celery in a pre-existing system. The previous version of the system already used Python standard logging.
My code is similar to the code below. Process one and process two are non-Celery functions, which are logging everywhere. We are using the logging to track data loss if something bad happens.
@task
def add(x,y):
process_one(x,y)
process_two(x,y)
How can I implement Celery and use the Python standard logging instead Celery logging, so our old logging system is not lost?
I have tried to change import logging
from Python to: logger = add.get_logger()
and pass the logger
to all functions, but I don't think it is a good practice. I need another solution.
Update: to add application logging in Celery logging, you can perform:
$ manage.py celeryd -v 2 -B -s celery -E -l debug --traceback \
--settings=settings --logfile=/(path to your log folder)/celeryd.log
With -l
(logging) as debug
, our application/Python logging is automatically included in our Celery logging: No need to perform logger = add.get_logger()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要这样的设置:
告诉我效果如何。
顺便说一句,它劫持根记录器的原因是因为一些写得不好
库设置日志记录,这是库不应该做的事情,导致用户体验不到 celeryd 的输出
工人:(
You probably want this setting:
Tell me how that works out.
Btw, the reason it hijacks the root logger is because some badly written
libraries sets up logging, something a library should never do, resulting in users experiencing no output from the celeryd
worker :(