LogZero 特性示例 介绍
LogZero 是一个稳健有效的 Python 2 和 Python 3 日志工具。
特性
-
@H_404_7@
可以很方便地打印到终端或者滚动日志
@H_404_7@提供完整可配置的Python Logger对象
@H_404_7@ @H_404_7@Windows终端中也能区分颜色
@H_404_7@能很好地处理编码问题,遇到特殊字符也不会崩溃
@H_404_7@ @H_404_7@支持配置全局Logger和局部Logger
@H_404_7@兼容Python2和3
@H_404_7@ @H_404_7@MIT许可证
@H_404_7@灵感来源于Tornado框架
示例
from logzero import logger logger.debug("hello") logger.info("info") logger.warn("warn") logger.error("error") # This is how you'd log an exception try: raise Exception("this is a demo exception") except Exception as e: logger.exception(e)
添加日志滚动也很容易
import logzero from 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 messages logger.info("This log message goes to the console and the logfile")
下面是一些例子说明如何使用日志文件、自定义格式以及设置最低日志等级。
import logging import logzero from logzero import logger # This log message goes to the console logger.debug("hello") # Set a minimum log level logzero.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 handler logzero.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 file logzero.logfile(None) # Set a custom formatter formatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s'); logzero.formatter(formatter) # Log some variables logger.info("var1: %s, var2: %s", var1, var2
LogZero 特性示例 官网
https://github.com/metachris/logzero
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。