通过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之前的版本上测试过,请给我以前版本的反馈。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java文件路径中的java UTF8字符   java如何可靠地写入OPC UA服务器?   for-my-if语句中的java循环不能产生我想要的结果   java我如何在1个XSL中加入2个XML   macos无法在“/Library/Java/JavaVirtualMachines/jdk15.0.1.jdk/Contents/Home/bin/apt”中找到可执行文件   Java代码简单数学   java如何避免selenium中的sendKeys方法清除之前填充的文本?   java如何将PlacesAPI自动完成小部件传递到自定义对话框?   带有文本块的java Intellij json片段问题   启动时Spring启动错误:java。木卫一。IOException:jsse。别名\u否\u键\u项   移动文件时发生java异常   http Java Web客户端远程连接