我需要以 JSON 格式将Apache Airflow日志记录到标准输出。Airflow 似乎并没有开箱即用地投射出这种能力。我找到了几个能够完成这项任务的 python 模块,但我无法让实现工作。
目前,我正在应用一个类airflow/utils/logging.py
来修改记录器,如下所示:
from pythonjsonlogger import jsonlogger
class StackdriverJsonFormatter(jsonlogger.JsonFormatter, object):
def __init__(self, fmt="%(levelname) %(asctime) %(nanotime) %(severity) %(message)", style='%', *args, **kwargs):
jsonlogger.JsonFormatter.__init__(self, fmt=fmt, *args, **kwargs)
def process_log_record(self, log_record):
if log_record.get('level'):
log_record['severity'] = log_record['level']
del log_record['level']
else:
log_record['severity'] = log_record['levelname']
del log_record['levelname']
if log_record.get('asctime'):
log_record['timestamp'] = log_record['asctime']
del log_record['asctime']
now = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
log_record['nanotime'] = now
return super(StackdriverJsonFormatter, self).process_log_record(log_record)
我正在实现此代码,/airflow/settings.py
如下所示:
from airflow.utils import logging as logconf
def configure_logging(log_format=LOG_FORMAT):
handler = logconf.logging.StreamHandler(sys.stdout)
formatter = logconf.StackdriverJsonFormatter()
handler.setFormatter(formatter)
logging = logconf.logging.getLogger()
logging.addHandler(handler)
''' code below was original airflow source code
logging.root.handlers = []
logging.basicConfig(
format=log_format, stream=sys.stdout, level=LOGGING_LEVEL)
'''
我已经尝试了几种不同的变体,但无法让 python-json-logger 将日志转换为 JSON。也许我没有进入根记录器?我考虑过的另一个选择是将日志手动格式化为 JSON 字符串。也没有运气。感谢您提供任何替代的想法、提示或支持。
干杯!