相关文章推荐
旅行中的荒野  ·  Python ...·  1 月前    · 
从容的碗  ·  txt文件如何设置每行为空行?_编程语言-C ...·  1 月前    · 
暴走的烤地瓜  ·  json.decoder.JSONDecod ...·  2 周前    · 
欢快的香烟  ·  将Mathematica生成的不规则形状数组 ...·  2 天前    · 
烦恼的水桶  ·  python用符号拼接DataFrame两列 ...·  2 天前    · 
呐喊的打火机  ·  dos常用命令 - 泡代码的小二黑 - 博客园·  8 月前    · 
近视的沙发  ·  react ...·  2 年前    · 
灰常酷的蘑菇  ·  java - kotlin如何获取泛型对象 ...·  2 年前    · 
腹黑的足球  ·  SpringBoot+MDC实现全链路调用日 ...·  2 年前    · 
Code  ›  Python logging 配置为JSON格式 | lixiansen's blog
python
https://lixiansen.xyz/2021/06/29/python-logging-%E9%85%8D%E7%BD%AE%E4%B8%BAjson%E6%A0%BC%E5%BC%8F/
内向的树叶
2 年前
加载中...
avatar
文章
27
标签
4
分类
15

首页
时间轴
标签
分类
清单
  • 音乐
  • 照片
  • 电影
友链
关于
lixiansen's blog
首页
时间轴
标签
分类
清单
  • 音乐
  • 照片
  • 电影
友链
关于

Python logging 配置为JSON格式

发表于 2021-06-29 | 更新于 2022-09-30 | python
| 阅读量:

前言

将日志json化输出

基本信息

模块format显示消息的内置格式

如果需要更改消息显示格式, 需要指定 format

1
2
3
4
5
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too')
1
2
3
DEBUG:This message should appear on the console
INFO:So should this
WARNING:And this, too

可参考: LogRecord属性

配置JSON输出

安装 python-json-logger

配置JSON输出,我们需要安装第三方的包

1
pip install python-json-logger

pip install python-json-logger

Pypi
https://pypi.python.org/pypi/python-json-logger

创建配置文件

logging.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[loggers]
keys=root,jsonmodule

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=simpleFormatter,json

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_jsonmodule]
level=DEBUG
handlers=fileHandler
qualname=jsonmodule

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=handlers.TimedRotatingFileHandler
level=INFO
formatter=json
args=("./app.log", 'd', 1, 15, 'utf-8')

[formatter_json]
class=pythonjsonlogger.jsonlogger.JsonFormatter
format=%(asctime)s %(threadName)s %(thread)d %(levelname)s %(lineno)d %(module)s %(funcName)s %(message)s

[formatter_simpleFormatter]
format=%(asctime)s %(funcName)s %(filename)s [line: %(lineno)d] %(levelname)s:%(message)s

默认前台输出,以及后台写文件. 日志文件已天分割, 并且保留15天.

1
2
3
4
5
6
import logging.config

logging.config.fileConfig('logging.conf')
logger = logging.getLogger('jsonmodule')

logger.error('dawdwad')
 
推荐文章
旅行中的荒野  ·  Python 如何解决最后一行最后一个输出有逗号问题_有问必答-CSDN问答
1 月前
从容的碗  ·  txt文件如何设置每行为空行?_编程语言-CSDN问答
1 月前
暴走的烤地瓜  ·  json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) - 找回失去的自我
2 周前
欢快的香烟  ·  将Mathematica生成的不规则形状数组导入Python的技术问询
2 天前
烦恼的水桶  ·  python用符号拼接DataFrame两列开发者社区
2 天前
呐喊的打火机  ·  dos常用命令 - 泡代码的小二黑 - 博客园
8 月前
近视的沙发  ·  react 移动端视频、音频、pdf预览_react音频视频预览-CSDN博客
2 年前
灰常酷的蘑菇  ·  java - kotlin如何获取泛型对象 - SegmentFault 思否
2 年前
腹黑的足球  ·  SpringBoot+MDC实现全链路调用日志跟踪 - 掘金
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号