使用简单的python日志包装器来使用最佳实践、减少代码和添加额外字段

logplus的Python项目详细描述


对数加

https://img.shields.io/pypi/v/logplus.svghttps://img.shields.io/travis/jmrichardson/logplus.svgDocumentation Status

简单的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]
    ...

学分

这个包是用Cookiecutteraudreyr/cookiecutter-pypackage项目模板创建的。

历史记录

0.1.0(2018-08-20)

  • pypi上的第一个版本。

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

推荐PyPI第三方库


热门话题
java ArrayOutOfBoundsException与org。日食swt。图样图像数据。布利特   java为什么Hazelcast CacheLoader类需要对所有客户端可见?   如何在java中迭代xml   java概要文件不是工件名称的一部分   java将多个Spring MVC应用程序映射到根Servlet   java SetContentView参数显示错误   java Cucumber类扩展步骤定义和挂钩   java如何动态添加JFace向导页面   amazon web services使用AWS Java SDK通过AWS API网关从S3下载文本文件   Android上的java软键盘   如何在Java中像在C#中一样创建泛型扩展方法?   使用JPA的java多列类型