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