如何从structlog中隐藏Celery任务id



我在Django应用程序中使用了Structlog和Celery,它们都运行得很好,但我希望在工作者执行任务时防止task_id出现在日志中。我该怎么做?

原因是task_id是一个键:值为36个字符长,因此它使日志输出难以读取。

2020-07-23 14:20:00.052156 [info     ] Update started                   [data.tasks] task_id=b79c9b3b-ae4b-41c6-951a-72b4f19fa2ac
2020-07-23 14:20:01.659316 [info     ] Update complete                  [data.models] exchange=aaa new=0 task_id=b79c9b3b-ae4b-41c6-951a-72b4f19fa2ac time=0.42 update=0
2020-07-23 14:20:01.936658 [info     ] Update complete                  [data.models] exchange=bbbbbb new=0 task_id=b79c9b3b-ae4b-41c6-951a-72b4f19fa2ac time=0.03 update=0
2020-07-23 14:20:02.451733 [info     ] Update complete                  [data.models] exchange=hhh new=0 task_id=b79c9b3b-ae4b-41c6-951a-72b4f19fa2ac time=0.28 update=0

这就是我为Celery设置structlog的方式:

structlog.configure(
    processors=[
        structlog.stdlib.filter_by_level,
        structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S.%f"),
        structlog.stdlib.add_logger_name,
        structlog.stdlib.add_log_level,
        structlog.stdlib.PositionalArgumentsFormatter(),
        structlog.processors.StackInfoRenderer(),
        structlog.processors.format_exc_info,
        structlog.processors.UnicodeDecoder(),
        structlog.processors.ExceptionPrettyPrinter(),
        structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
    ],
    context_class=structlog.threadlocal.wrap_dict(dict),
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=structlog.stdlib.BoundLogger,
    cache_logger_on_first_use=True,
)

@receiver(signals.modify_context_before_task_publish)
def receiver_modify_context_before_task_publish(sender, signal, context):
    keys_to_keep = {"request_id", "parent_task_id"}
    new_dict = {key_to_keep: context[key_to_keep] for key_to_keep in keys_to_keep if key_to_keep in context}
    context.clear()
    context.update(new_dict)

谢谢

日志行的格式由相关记录器的处理程序上设置的格式化程序控制。如果您在日志中看到了任务ID,则意味着您正在访问芹菜任务记录器(celery.utils.log.get_task_logger(。修改该格式化程序,您可以配置日志记录以从日志行中删除任务ID。下面是一个示例yaml字典配置,您可以与logging.dictConfig一起使用它来控制芹菜任务记录器的格式:

loggers:
  celery.task:
    level: INFO
    propagate: false
    handlers:
      - brief
handlers:
  brief:
    class: logging.StreamHandler
    formatter: brief
    level: DEBUG
    stream: ext://sys.stdout
formatters:
  brief:
    (): logging.Formatter
    format: '{log_color}[{name}] {message}'
    style: '{'

相关内容

  • 没有找到相关文章

最新更新