是否可以为芹菜任务和Django提供不同的日志格式?
我想要的是两种不同的日志格式。在芹菜日志中,我想要额外的task_id
和processName
参数。
下面是我的Django记录器设置,
class LoggerSettingsMixin(object):
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[%(asctime)s] [rid:%(request_id)s] [crid:%(client_request_id)s] [aid:%(actor_id)s] [%(levelname)s] [%(lineno)d:%(name)s] - %(message)s'
},
'celery': {
'format': '[%(asctime)s] [%(processName)s] [%(levelname)s] [%(lineno)d:%(name)s] - %(message)s'
},
},
'filters': {
'request_id': {
'()': 'request_id.logging.RequestIdFilter'
},
'client_request_id': {
'()': 'apps.commons.logging_filters.ClientRequestIdFilter'
},
'actor_id': {
'()': 'apps.commons.logging_filters.ActorFilter'
},
'task_id': {
'()': 'apps.commons.logging_filters.CeleryTaskIdFilter'
}
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'filters': ['request_id', 'actor_id', 'client_request_id', 'task_id'],
'formatter': 'default',
},
'celery': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'celery',
}
},
'loggers': {
'statsd': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
},
'celery': {
'handlers': ['celery'],
'level': 'INFO',
'propagate': False,
},
'celery.redirected': {
'handlers': ['celery'],
'level': 'INFO',
'propagate': False,
},
'': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
}
}
}
以下是我的信号,我试图覆盖芹菜根记录器,
@after_setup_logger.connnect
def setup_logger(**options):
logger = get_logger('')
logger.handlers = []
logger.addFilter(ActorFilter())
logger.addFilter(ClientRequestIdFilter())
logger.addFilter(CeleryTaskIdFilter())
logger.addFilter(RequestIdFilter())
formatter = logging.Formatter('[%(asctime)s] [%(processName)s][rid:%(request_id)s [crid:%(client_request_id)s] [aid:%(actor_id)s] [taskid:%(task_id)s] [%(levelname)s] [%(lineno)d:%(name)s] - %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
我也尝试过为celery.redirected设置记录器,但它仍然无法正常工作。
在设置中,CELERYD_HIJACK_ROOT_LOGGER = False
在尝试执行任务时,它会给出日志错误
[2017-07-26 21:09:17,377] [MainProcess] [INFO] [402:celery.worker.job] - Task apps.apis.reports.tasks.update_pipeline_reports[a6698044-a209-4fdf-8ab6-e6a1affb5efd] succeeded in 0.158420855005s: None
[2017-07-26 21:09:19,927] [PoolWorker-4] [WARNING] [282:celery.redirected] - Traceback (most recent call last):
[2017-07-26 21:09:19,927] [PoolWorker-4] [WARNING] [282:celery.redirected] - File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 861, in emit
[2017-07-26 21:09:19,927] [PoolWorker-4] [WARNING] [282:celery.redirected] - msg = self.format(record)
[2017-07-26 21:09:19,928] [PoolWorker-4] [WARNING] [282:celery.redirected] - File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 734, in format
[2017-07-26 21:09:19,928] [PoolWorker-4] [WARNING] [282:celery.redirected] - return fmt.format(record)
[2017-07-26 21:09:19,928] [PoolWorker-4] [WARNING] [282:celery.redirected] - File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 469, in format
[2017-07-26 21:09:19,929] [PoolWorker-4] [WARNING] [282:celery.redirected] - s = self._fmt % record.__dict__
[2017-07-26 21:09:19,929] [PoolWorker-4] [WARNING] [282:celery.redirected] - KeyError: 'request_id'
[2017-07-26 21:09:19,929] [PoolWorker-4] [WARNING] [282:celery.redirected] - Logged from file tasks.py, line 32
此外,当我没有尝试在after_setup_logger
信号中覆盖根记录器时,它会选择默认的Django格式并打印日志而不会出错。