Django Celery 日志最佳实践 [英] Django Celery Logging Best Practice
问题描述
我正在尝试让 Celery 日志记录与 Django
一起工作.我在 settings.py
中进行了日志设置以进入控制台(这很好用,因为我在 Heroku
上托管).在每个模块的顶部,我有:
I'm trying to get Celery logging working with Django
. I have logging set-up in settings.py
to go to console (that works fine as I'm hosting on Heroku
). At the top of each module, I have:
import logging
logger = logging.getLogger(__name__)
在我的tasks.py中,我有:
And in my tasks.py, I have:
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
这适用于记录来自任务的调用,我得到如下输出:
That works fine for logging calls from a task and I get output like this:
2012-11-13T18:05:38+00:00 app[worker.1]: [2012-11-13 18:05:38,527: INFO/PoolWorker-2] Syc feed is starting
但是如果该任务然后调用另一个模块中的方法,例如一个 queryset
方法,我得到重复的日志条目,例如
But if that task then calls a method in another module, e.g. a queryset
method, I get duplicate log entries, e.g.
2012-11-13T18:00:51+00:00 app[worker.1]: [INFO] utils.generic_importers.ftp_processor process(): File xxx.csv already imported. Not downloaded
2012-11-13T18:00:51+00:00 app[worker.1]: [2012-11-13 18:00:51,736: INFO/PoolWorker-6] File xxx.csv already imported. Not downloaded
我觉得我可以用
CELERY_HIJACK_ROOT_LOGGER = False
仅使用 Django
日志记录但是当我尝试它时这不起作用,即使我确实让它工作了,我也会丢失 "PoolWorker-6"代码> 我想要的位.(顺便说一句,我不知道如何让任务名称显示在 Celery 的日志条目中,如 文档 似乎表明它应该).
to just use the Django
logging but this didn't work when I tried it and even if I did get it to work, I would lose the "PoolWorker-6"
bit which I do want. (Incidentally, I can't figure out how to get the task name to display in the log entry from Celery, as the docs seems to indicate that it should).
我怀疑我在这里遗漏了一些简单的东西.
I suspect I'm missing something simple here.
推荐答案
当您的记录器在另一个模块"的开头初始化时,它会链接到另一个记录器.哪个处理你的消息.它可以是根记录器,或者通常我在 Django 项目中看到 - 记录器名称为 ''
.
When your logger initialized in the beginning of "another module" it links to another logger. Which handle your messages. It can be root logger, or usually I see in Django projects - logger with name ''
.
这里最好的方法是覆盖你的日志配置:
Best way here, is overriding your logging config:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s',
'datefmt': '%y %b %d, %H:%M:%S',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'celery': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'celery.log',
'formatter': 'simple',
'maxBytes': 1024 * 1024 * 100, # 100 mb
},
},
'loggers': {
'celery': {
'handlers': ['celery', 'console'],
'level': 'DEBUG',
},
}
}
from logging.config import dictConfig
dictConfig(LOGGING)
在这种情况下,我想它应该像你想象的那样工作.
In this case I suppose it should work as you assume.
附言dictConfig 在 Python2.7+ 中添加.
P.S. dictConfig added in Python2.7+.
这篇关于Django Celery 日志最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!