Python 'with' 和日志记录的额外参数

Python 'with' and extra arguments to logging

是否可以通过 with 函数向记录器添加额外信息?

我需要这样的东西

some_class.py
def some_function():
   with monitoring.context(1, 2):
       logger.info('message')

monitoring.py

@contextmanager
def context(id, id2):
   //any way how to adds ids to formatter
   yield

谢谢指教

def some_function():
   with monitoring.context(1, 2) as ctx_dict:
       logger.info('message', **ctx_dict)

def context(id, id2):
   yield {'some': id, 'another': id2}

当然,对于您的 logger.info,API 必须接受 someanother 关键字参数。