芹菜具有用于不同任务的不同日志文件

时间:2018-11-02 12:07:45

标签: django celery

我正在使用celery计划我的任务,但日志被写入单个文件celery_worker.log 我需要的是每个任务都有单独的日志文件。我添加了一个记录器功能,但是似乎不起作用。 所以我的记录功能是

def logging_service(logger_name, file_name):
    """
    Set the logging levels and the log file for each cron/task
    :param logger_name: cron/task name
    :param file_name: file to which logs are to be written
    :return: the logger which will be used to log
    """
    # set up logging to file - see previous section for more details
    logging.basicConfig(level=logging.DEBUG,
                        format='[%(asctime)s] [%(levelname)s] %(message)s [(%(filename)s : %(lineno)d)]',
                        datefmt="%Y-%m-%d %H:%M:%S",
                        filename='./logs/{}'.format(file_name))
    # define a Handler which writes INFO messages or higher to the sys.stderr

    console = logging.StreamHandler()
    console.setLevel(logging.INFO)


    # set a format which is simpler for console use
    formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s [(%(filename)s : %(lineno)d)]')
    # tell the handler to use this format
    console.setFormatter(formatter)
    # add the handler to the root logger
    logging.getLogger(logger_name).addHandler(console)
    return logging.getLogger(logger_name)

我的芹菜任务是定期的,

@periodic_task(run_every=(crontab(minute=0, hour=2)), name="task1_run", ignore_result=True)
def task1_run():
    some_func()

因此在此some_func中,我打电话给了logging_service,但它不起作用。 在supervisor.conf文件中,我也没有为celery worker添加任何特定的日志文件 预先感谢

0 个答案:

没有答案