Changeset View
Changeset View
Standalone View
Standalone View
swh/core/logger.py
Show All 24 Lines | def db_level_of_py_level(lvl): | ||||
""" | """ | ||||
return logging.getLevelName(lvl).lower() | return logging.getLevelName(lvl).lower() | ||||
def get_extra_data(record, task_args=True): | def get_extra_data(record, task_args=True): | ||||
"""Get the extra data to insert to the database from the logging record""" | """Get the extra data to insert to the database from the logging record""" | ||||
log_data = record.__dict__ | log_data = record.__dict__ | ||||
extra_data = {k[len(EXTRA_LOGDATA_PREFIX):]: v | extra_data = { | ||||
k[len(EXTRA_LOGDATA_PREFIX) :]: v | |||||
for k, v in log_data.items() | for k, v in log_data.items() | ||||
if k.startswith(EXTRA_LOGDATA_PREFIX)} | if k.startswith(EXTRA_LOGDATA_PREFIX) | ||||
} | |||||
args = log_data.get('args') | args = log_data.get('args') | ||||
if args: | if args: | ||||
extra_data['logging_args'] = args | extra_data['logging_args'] = args | ||||
# Retrieve Celery task info | # Retrieve Celery task info | ||||
if current_task and current_task.request: | if current_task and current_task.request: | ||||
extra_data['task'] = { | extra_data['task'] = {'id': current_task.request.id, 'name': current_task.name} | ||||
'id': current_task.request.id, | |||||
'name': current_task.name, | |||||
} | |||||
if task_args: | if task_args: | ||||
extra_data['task'].update({ | extra_data['task'].update( | ||||
{ | |||||
'kwargs': current_task.request.kwargs, | 'kwargs': current_task.request.kwargs, | ||||
'args': current_task.request.args, | 'args': current_task.request.args, | ||||
}) | } | ||||
) | |||||
return extra_data | return extra_data | ||||
def flatten( | def flatten(data: Any, separator: str = "_") -> Generator[Tuple[str, Any], None, None]: | ||||
data: Any, | |||||
separator: str = "_" | |||||
) -> Generator[Tuple[str, Any], None, None]: | |||||
"""Flatten the data dictionary into a flat structure""" | """Flatten the data dictionary into a flat structure""" | ||||
def inner_flatten( | def inner_flatten( | ||||
data: Any, prefix: List[str] | data: Any, prefix: List[str] | ||||
) -> Generator[Tuple[List[str], Any], None, None]: | ) -> Generator[Tuple[List[str], Any], None, None]: | ||||
if isinstance(data, dict): | if isinstance(data, dict): | ||||
if all(isinstance(key, str) for key in data): | if all(isinstance(key, str) for key in data): | ||||
for key, value in data.items(): | for key, value in data.items(): | ||||
Show All 29 Lines | def emit(self, record): | ||||
try: | try: | ||||
extra_data = flatten(get_extra_data(record, task_args=False)) | extra_data = flatten(get_extra_data(record, task_args=False)) | ||||
extra_data = { | extra_data = { | ||||
(EXTRA_LOGDATA_PREFIX + key).upper(): stringify(value) | (EXTRA_LOGDATA_PREFIX + key).upper(): stringify(value) | ||||
for key, value in extra_data | for key, value in extra_data | ||||
} | } | ||||
msg = self.format(record) | msg = self.format(record) | ||||
pri = self.mapPriority(record.levelno) | pri = self.mapPriority(record.levelno) | ||||
send(msg, | send( | ||||
msg, | |||||
PRIORITY=format(pri), | PRIORITY=format(pri), | ||||
LOGGER=record.name, | LOGGER=record.name, | ||||
THREAD_NAME=record.threadName, | THREAD_NAME=record.threadName, | ||||
CODE_FILE=record.pathname, | CODE_FILE=record.pathname, | ||||
CODE_LINE=record.lineno, | CODE_LINE=record.lineno, | ||||
CODE_FUNC=record.funcName, | CODE_FUNC=record.funcName, | ||||
**extra_data) | **extra_data | ||||
) | |||||
except Exception: | except Exception: | ||||
self.handleError(record) | self.handleError(record) |