Source code for ccat_data_transfer.config.log_config
import logging
# Celery adds task name and task id to the formatter. Some code might be called
# from Celery and some might be called from other functions to make allow
# normal usage of the logging module in independent code but get the additional
# task_id and task_name information the augmented TaskFormatter below can be
# used Reference
# https://www.distributedpython.com/2018/11/06/celery-task-logger-format/
[docs]
class MyTaskFormatter(logging.Formatter):
""" Task Formatter to make logging play nicely with Celery
"""
[docs]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
try:
from celery._state import get_current_task
self.get_current_task = get_current_task
except ImportError:
self.get_current_task = lambda: None
[docs]
def format(self, record):
task = self.get_current_task()
if task and task.request:
record.__dict__.update(task_id=task.request.id,
task_name=task.name,
task_separator="/",
task_start="[",
task_end="]"
)
else:
record.__dict__.setdefault('task_name', '')
record.__dict__.setdefault('task_id', '')
record.__dict__.setdefault('task_separator', '')
record.__dict__.setdefault('task_start', '')
record.__dict__.setdefault('task_end', '')
return super().format(record)
LOGGING_FORMAT = ('%(asctime)s %(task_start)s%(task_id)s%(task_separator)s'
'%(task_name)s%(task_end)s %(name)s '
'%(levelname)s %(message)s')
DATE_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
LOGGING_CONFIG_NORMAL = {
'version': 1,
'formatters': {
'standard': {
'()': MyTaskFormatter,
'format': LOGGING_FORMAT,
'datefmt': DATE_FORMAT
},
},
'handlers': {
'stream': {'class': 'logging.StreamHandler',
'formatter': 'standard',
'level': 'DEBUG'
},
},
'loggers': {
'karmada': {
'handlers': ['stream'],
'level': 'DEBUG',
'propagate': False
},
'werkzeug': {
'handlers': ['stream'],
'level': 'DEBUG',
},
# Kept as a reference to explain how to configure the root logger
# '': {
# 'handlers': ['stream', "file"],
# 'level': 'DEBUG',
# },
}
}