import logging
from jsonformatter import JsonFormatter
log_file_path = "./logs/log_data.log"
data_format = {"levelname": "levelname",
"filename": "filename",
"module": "module",
"funcname": "funcName",
"asctime": "asctime",
"message": "message"}
formatter = JsonFormatter(data_format, datefmt="%Y-%m-%d %H:%M:%S", ensure_ascii=False,
mix_extra=True, mix_extra_position='tail')
json_handler = logging.FileHandler(filename=log_file_path)
json_handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(json_handler)
logger.setLevel(logging.INFO)
logger.info('New message', extra={"duration" : 1000,
"value1" : 56,
"value2" : 189,
"code" : 11})
{'levelname': 'INFO',
'filename': '2712754776.py',
'module': '2712754776',
'funcname': '<cell line: 1>',
'asctime': '2022-11-08 17:25:26',
'message': 'New message',
'duration': 1000,
'value1': 56,
'value2': 189,
'code': 11}