【问题标题】:Celery / Django - How to make existing function into a task and handle existing loggingCelery / Django - 如何将现有功能变为任务并处理现有日志记录
【发布时间】:2016-02-24 13:35:49
【问题描述】:

这是我的场景。我的 Django 项目中有一些类似的功能:

import logging

logger = logging.getLogger('farm')

def _move_horse(horse_id, attachment=None):
    if attachment:
        logger.info(...)
        #other_stuff
    else:
        logger.info(...)
        other_func() # contains more logging

def move_horse_to_field(horse_id):
    _move_horse(horse_id)

def move_horse_to_field_w_attachment(horse_id, attachment):
    _move_horse(horse_id, attachment)

那我想把这个做成后台任务:

@shared_task(bind=True)
def move_horse_to_field(self, horse_id):
    _move_horse(horse_id)

但是如何让被调用函数的日志记录在从后台任务调用时和正常情况下都能正常工作?

【问题讨论】:

  • 您目前有哪些日志记录配置? p;请更详细地描述它如何工作/不工作
  • 它设置为写入指定文件,并通过电子邮件发送错误级别的日志消息。
  • 请完整回答我的问题。

标签: python django celery django-celery


【解决方案1】:

经过一段时间的工具和尝试不同的事情,它似乎就像设置这个选项一样简单:

CELERYD_HIJACK_ROOT_LOGGER = 假

一旦我这样做了,芹菜工人似乎尊重我所有现有的日志设置。他们写入与 Django 应用程序相同的文件,甚至在我记录错误级别消息(我拥有的日志设置)时发送电子邮件在我的情况下,这正是我想要的。

而且我似乎根本不必为 get_task_logger(name) 操心。

【讨论】:

    猜你喜欢
    • 2020-07-15
    • 2013-09-30
    • 2018-09-22
    • 2020-08-04
    • 2014-07-17
    • 1970-01-01
    • 2019-01-04
    • 2020-12-04
    • 2015-12-25
    相关资源
    最近更新 更多