Python 中的日誌庫 logzero

安裝

pip install -U logzero

使用

from logzero import logger

# These log messages are sent to the console
logger.debug("hello %s", "world")
logger.info("info")
logger.warning("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)

若是文件名是demo.py,輸出將會是下圖git

輸入圖片說明

輸出還有顏色,文件+行號。比logging默認的輸出不知道強了多少條街道。github

還有一些更高級的用法this

日誌切割

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")

設置全局日誌Level

import logzero
import logging
from logzero import logger

logzero.loglevel(logging.INFO)
logger.debug("hello") # 不顯示
logger.info("info") # 顯示

定製日誌實例

from logzero import setup_logger
logger1 = setup_logger(name="mylogger1", logfile="/tmp/test-logger1.log", level=logging.INFO)
logger2 = setup_logger(name="mylogger2", logfile="/tmp/test-logger2.log", level=logging.INFO)

# By default, logging
logger1.info("info for logger 1")
logger2.info("info for logger 2")

更多

還有更多的內容等着去發現。debug

相關文章
相關標籤/搜索