[英]How to properly configure Celery logging functionality with Django?
I use Django and Celery to schedule a task but I have an issue with the logger because it doesn't propagate properly.我使用 Django 和 Celery 来安排任务,但记录器有问题,因为它不能正确传播。 As you can see in the code below I have configured the Python
logging
module and the get_task_logger
Celery module.正如您在下面的代码中看到的,我已经配置了 Python
logging
模块和get_task_logger
Celery 模块。
import logging
from celery import Celery
from celery.utils.log import get_task_logger
# Configure logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
# Create Celery application and Celery logger
app = Celery('capital')
logger = get_task_logger(__name__)
@app.task()
def candle_updated(d, f):
logging.warning('This is a log')
logger.info('This is another log')
return d+f
I use django-celery-beat
extension to setup periodic task from the Django admin.我使用
django-celery-beat
扩展从 Django 管理员设置定期任务。 This module stores the schedule in the Django database, and presents a convenient admin interface to manage periodic tasks at runtime.该模块将计划存储在 Django 数据库中,并提供方便的管理界面来管理运行时的周期性任务。
As recommended in the documentation I start the worker and the scheduler that way:按照文档中的建议,我以这种方式启动工作程序和调度程序:
$ celery -A capital beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
celery beat v4.4.0 (cliffs) is starting.
__ - ... __ - _
LocalTime -> 2020-04-02 22:33:32
Configuration ->
. broker -> redis://localhost:6379//
. loader -> celery.loaders.app.AppLoader
. scheduler -> django_celery_beat.schedulers.DatabaseScheduler
. logfile -> [stderr]@%INFO
. maxinterval -> 5.00 seconds (5s)
[2020-04-02 22:33:32,630: INFO/MainProcess] beat: Starting...
[2020-04-02 22:33:32,631: INFO/MainProcess] Writing entries...
[2020-04-02 22:33:32,710: INFO/MainProcess] Scheduler: Sending due task Candles update (marketsdata.tasks.candle_updated)
[2020-04-02 22:33:32,729: INFO/MainProcess] Writing entries...
[2020-04-02 22:33:38,726: INFO/MainProcess] Scheduler: Sending due task Candles update (marketsdata.tasks.candle_updated)
[2020-04-02 22:33:44,751: INFO/MainProcess] Scheduler: Sending due task Candles update (marketsdata.tasks.candle_updated)
Everything seems to run fine.一切似乎都运行良好。 There is output in the console every 6 seconds (frequency of the periodic task) so it seems the task is executed in the background but I can't check it.
每 6 秒(周期性任务的频率)在控制台中有 output(周期性任务的频率),所以看起来任务是在后台执行的,但我无法检查。 And the problem I have is that the file example.log is empty, what could be the reason?
我遇到的问题是文件 example.log 是空的,可能是什么原因?
Did you start a worker node as well?您是否也启动了工作节点? beat is just the scheduler, You have to run a worker as well
beat 只是调度程序,您还必须运行一个工人
celery -A capital worker -l info
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.