Celery Worker log 中记录 task_id

import inspect
import logging
import threading
from logging import Logger as Logger, LogRecord

from celery import Celery
from celery.signals import task_prerun, task_postrun

local = threading.local()
local.task_id = ""


class MyLogger(Logger):
    def makeRecord(self, *args, **kwargs) -> LogRecord:
        rv = super().makeRecord(*args, **kwargs)
        rv.task_id = local.task_id
        return rv


logging.Logger.manager.loggerClass = MyLogger

app = Celery()
app.conf.update(
    CELERYD_LOG_FORMAT="[%(asctime)s: %(levelname)s/%(processName)s %(task_id)s %(filename)s:%(lineno)d ] %(message)s"
)


@task_prerun.connect
def set_task_id(task_id, task, *args, **kwargs):
    local.task_id = task_id


@task_postrun.connect
def rm_task_id(task_id, task, *args, **kwargs):
    local.task_id = ""


@app.task
def add(x, y):
    stack = inspect.stack()
    f_locals = stack[1].frame.f_locals
    print(f'{getattr(f_locals.get("req", object()), "id", "noid")}')
    return x + y


if __name__ == '__main__':
    app.worker_main()

 

posted @ 2020-11-18 11:03  两只老虎111  阅读(581)  评论(2编辑  收藏  举报