通过json文档进行结构化日志记录的库
simple-json-logging的Python项目详细描述
Python中的结构化日志库
示例
启动的简单脚本:
fromsimple_json_loggingimportinit_json_loggerlogger=init_json_logger('my_json_logger')defmain():logger.warning('Test log message with arg1 %s and arg2 %s','abc',100,foo='yyy',bar=100500)try:raiseRuntimeError('just for test')exceptException:logger.exception('exception',foo='yyy')if__name__=='__main__':main()
此脚本将输出:
{"name": "my_json_logger", "levelname": "WARNING", "levelno": 30, "pathname": "example.py", "filename": "example.py", "module": "example", "stack_info": null, "lineno": 7, "funcName": "main", "created": 1564591269.778053, "msecs": 778.0530452728271, "relativeCreated": 4.003047943115234, "thread": 30272, "threadName": "MainThread", "processName": "MainProcess", "process": 19180, "data": {"foo": "yyy", "bar": 100500}, "message": "Test log message with arg1 abc and arg2 100"}
{"name": "my_json_logger", "levelname": "ERROR", "levelno": 40, "pathname": "example.py", "filename": "example.py", "module": "example", "stack_info": null, "lineno": 11, "funcName": "main", "created": 1564591269.778053, "msecs": 778.0530452728271, "relativeCreated": 4.003047943115234, "thread": 30272, "threadName": "MainThread", "processName": "MainProcess", "process": 19180, "data": {"foo": "yyy"}, "message": "exception", "exceptionClass": "RuntimeError", "exceptionMessage": "just for test"}
另一个具有自定义格式化程序:
fromsimple_json_loggingimportJsonFormatter,init_json_loggerformatter=JsonFormatter(json_dumps_args={'sort_keys':True,'indent':2})logger=init_json_logger('my_json_logger',formatter=formatter)defmain():logger.warning('Test log message with arg1 %s and arg2 %s','abc',100,foo='yyy',bar=100500)try:raiseRuntimeError('just for test')exceptException:logger.exception('exception',foo='yyy')if__name__=='__main__':main()
此脚本将输出:
{
"created": 1564591638.1781173,
"data": {
"bar": 100500,
"foo": "yyy"
},
"exc_text": null,
"filename": "example.py",
"funcName": "main",
"levelname": "WARNING",
"levelno": 30,
"lineno": 8,
"message": "Test log message with arg1 abc and arg2 100",
"messageFormatted": "Test log message with arg1 abc and arg2 100",
"module": "example",
"msecs": 178.1172752380371,
"name": "my_json_logger",
"pathname": "example.py",
"process": 26544,
"processName": "MainProcess",
"relativeCreated": 2.9993057250976562,
"stack_info": null,
"thread": 28208,
"threadName": "MainThread"
}
{
"created": 1564591638.1781173,
"data": {
"foo": "yyy"
},
"exc_text": "Traceback (most recent call last):\n File \"example.py\", line 10, in main\n raise RuntimeError('just for test')\nRuntimeError: just for test",
"exceptionClass": "RuntimeError",
"exceptionMessage": "just for test",
"filename": "example.py",
"funcName": "main",
"levelname": "ERROR",
"levelno": 40,
"lineno": 12,
"message": "exception",
"messageFormatted": "exception",
"module": "example",
"msecs": 178.1172752380371,
"name": "my_json_logger",
"pathname": "example.py",
"process": 26544,
"processName": "MainProcess",
"relativeCreated": 2.9993057250976562,
"stack_info": null,
"thread": 28208,
"threadName": "MainThread"
}
已知问题
没有在Python3.7之前的版本上测试过,请给我以前版本的反馈。