使用简单的python日志包装器来使用最佳实践、减少代码和添加额外字段
logplus的Python项目详细描述
对数加
简单的python日志包装器可以使用最佳实践,减少代码并添加额外的字段。
Log Plus概述:
- 在每个日志条目中提供永久和ephimeral上下文字段
- 提供一种简单的方法,以一致地记录最佳实践
- 在每个应用程序中避免繁琐的锅炉板代码
- 使用简单的yaml文件配置python日志记录
- 使用简单干净
- 自由软件:麻省理工学院许可证
- 文档:https://logplus.readthedocs.io。
- python>;=3.5
功能
灵感来自Python logging best practices with JSON steirods。
- 使用标准的python日志记录
- 使用yaml日志记录配置文件
- 以json格式登录
- 添加永久和短暂上下文字段
快速启动
安装LogPlus
pipinstalllogplus
导入LogPlus包
importlogplus
使用日志文件位置和永久上下文字段定义记录器。永久上下文字段是静态的,将添加到所有日志条目中。例如,您可能希望记录提供给脚本的参数以及实例标识符(uuid)
log=logplus.setup('test.log',arg1=arg1,uuid=uuid)
添加日志项。日志条目将包含前面的参数以及值为count的字段“count”。
log.info("Application message",count=count)
下面是一个简单的应用程序示例test.py
importlogplusarg1="argument1"uuid="23RS2F"log=logplus.setup('test.log',arg1=arg1,uuid=uuid)count=1log.info("Application message: with count",count=count)log.info("Application message: no count")
这导致在“test.log”中出现以下日志条目:
{"asctime": "2018-08-22 16:51:28,794", "filename": "test.py", "lineno": 7, "levelname": "INFO", "message": "Application message: with count", "arg1": "argument1", "uuid": "23RS2F", "count": 1} {"asctime": "2018-08-22 16:51:28,795", "filename": "test.py", "lineno": 8, "levelname": "INFO", "message": "Application message: no count", "arg1": "argument1", "uuid": "23RS2F"}
使用量
Log Plus需要一个简单的配置命令来设置日志文件位置、可选的日志配置文件以及要添加到所有日志项的任何上下文字段:
logplus.setup(log_file, [config_file], [static_args]...)
- 日志文件:要写入的日志文件的路径
- config_文件:yaml格式的可选日志配置文件
- 静态参数:要包含在所有日志项中的键值参数
请注意,如果未提供配置文件,它将使用以下配置:
--- version: 1 disable_existing_loggers: False formatters: simple: format: "%(asctime)s - %(levelname)s - %(message)s" json: format: "%(asctime)s %(filename)s %(lineno)s %(levelname)s %(message)s" class: pythonjsonlogger.jsonlogger.JsonFormatter handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdout file_handler: class: logging.handlers.RotatingFileHandler formatter: json maxBytes: 10485760 # 10MB backupCount: 20 encoding: utf8 root: level: INFO handlers: [console, file_handler] ...
学分
这个包是用Cookiecutter和audreyr/cookiecutter-pypackage项目模板创建的。
历史记录
0.1.0(2018-08-20)
- pypi上的第一个版本。