LogZero 稳健有效的日志工具开源项目

我要开发同款
匿名用户2017年09月13日
18阅读
开发技术Python
所属分类程序开发、日志工具(Logging)
授权协议MIT

作品详情

LogZero是一个稳健有效的Python2和Python3日志工具。

特性

可以很方便地打印到终端或者滚动日志

提供完整可配置的PythonLogger对象

输出格式漂亮,在终端中根据不同的日志分级显示不同的颜色。

Windows终端中也能区分颜色

能很好地处理编码问题,遇到特殊字符也不会崩溃

多个Logger可以输出到同一个日志文件

支持配置全局Logger和局部Logger

兼容Python2和3

只有一个代码文件

MIT许可证

灵感来源于Tornado框架

示例from logzero import loggerlogger.debug("hello")logger.info("info")logger.warn("warn")logger.error("error")# This is how you'd log an exceptiontry:    raise Exception("this is a demo exception")except Exception as e:    logger.exception(e)

添加日志滚动也很容易

import logzerofrom logzero import logger# Setup rotating logfile with 3 rotations, each with a maximum filesize of 1MB:logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1e6, backupCount=3)# Log messageslogger.info("This log message goes to the console and the logfile")

下面是一些例子说明如何使用日志文件、自定义格式以及设置最低日志等级。

import loggingimport logzerofrom logzero import logger# This log message goes to the consolelogger.debug("hello")# Set a minimum log levellogzero.loglevel(logging.INFO)# Set a logfile (all future log messages are also saved there)logzero.logfile("/tmp/logfile.log")# You can also set a different loglevel for the file handlerlogzero.logfile("/tmp/logfile.log", loglevel=logging.ERROR)# Set a rotating logfile (replaces the previous logfile handler)logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1000000, backupCount=3)# Disable logging to a filelogzero.logfile(None)# Set a custom formatterformatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s');logzero.formatter(formatter)# Log some variableslogger.info("var1: %s, var2: %s", var1, var2
查看全文
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论