简体   繁体   English

Django Celery运行时更改日志文件

[英]Django Celery change log file on run time

======================== LAST UPDATE ========================= ========================最后更新======================== =
I found Celery Signal worker_process_init.connect(some_function) 我发现芹菜信号worker_process_init.connect(some_function)
which allow me to run any function I want before the worker child start running, and in my case I can change the logs handlers as I want... 这可以让我在工作者子程序开始运行之前运行我想要的任何功能,就我而言,我可以根据需要更改日志处理程序...

handler = logging.FileHandler(('my%s.log')%current_process().index, 'a')
logger.addHandler(handler)

(another signal that I found- setup_log was not efficient in my case.) (我发现的另一个信号-setup_log在我的情况下效率不高。)
++++++++++++++++++++++++++ UPDATE ++++++++++++++++++++++++++ ++++++++++++++++++++++++++更新+++++++++++++++++++++++ +++
I moved the handlet out of my settings.py since i understood it loaded only once, so now it is defined in task.py: 我将handlet从我的settings.py中移出,因为我知道它仅加载一次,所以现在在task.py中对其进行了定义:

def custom_logger(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    handler = logging.FileHandler(('my%s.log')%current_process().index, 'a')
    logger.addHandler(handler)
return logger

and task for example : 和任务,例如:

@tenant_celery.task(base=HandlersImplTenantTask)
@concurrent_task(task_timeout=1)
def add():
    task_id = add.request.id
    l = custom_logger(task_id)
    l.info("task name is -   ADD")
    l.info("my worker id is: %s" % current_process().index)
return 5+7

not the most aesthetic solution, but it does work- changing log files on run time.. 不是最美观的解决方案,但它确实可以在运行时更改日志文件。

============================================================== ================================================== ============

I'm running Celery task over Django, 我正在Django上运行Celery任务,

I want every celery worker to write to a different log file, 我希望每个芹菜工人都写入不同的日志文件,

but to the same one every time--> 但每次都同一个->

for example: worker 1 -->myLog1.log ,worker 2 -->myLog2.log 例如:worker 1-> myLog1.log,worker 2-> myLog2.log

this is how my settings.py looks: 这是我的settings.py的外观:

logfile = '%s/%s' % (logdir, APP_NAME + "-" + process_name + '.log')
CELERY_WORKER_LOGFILE = '%s/%s' % (logdir, 'celery_worker.log')

and task.py: 和task.py:

@tenant_celery.task(base=HandlersImplTenantTask)
def get_worker_id():
    logdir = '%s/%s' % (os.curdir, 'log')
    settings.CELERY_WORKER_LOGFILE = '%s/%s-%s.log' % (logdir, 'celery_worker', current_process().index)
    print settings.CELERY_WORKER_LOGFILE
    # print the new log file
    logger.info("HELLO FROM TASK %s", current_process().index)
    #  write to the wrong logfile.

and the same for the second task I have. 与第二项任务相同。

but although it print a different "LOFFILE" every task, 但是,尽管它为每个任务打印了一个不同的“ LOFFILE”,

it keeps writing to the same log (!!) as its appear in settings.py . 它会继续写入与其在settings.py中出现的日志相同的日志(!!)。

my run command is : 我的运行命令是:

celery worker -c 2 --broker=amqp://1000:1000@localhost:5672//1000

is there any way to (realy) change the log file during run time?? 有什么方法可以(真正)在运行时更改日志文件?

(I could not understand if Django log signals can help me) (我不明白Django日志信号是否可以帮助我)

I also found THIS answer but it does not work for me.. 我也找到了这个答案,但对我不起作用。

thanks 谢谢

You know that when running the worker you can specify the logfile via argument like this: 您知道,在运行worker时,可以通过如下参数指定日志文件:

celery -A proj worker -l info -f paht/to/yourfile.log

More on the official doc 有关官方文档的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM