日志級别(5個等級),從低到高分别是:
DEBUG
INFO
WARNING
ERROR
CRITICAL
日志輸出:
控制台日志
檔案日志
logging 中的幾個概念:
Logger:日志記錄器,是應用程式中可以直接使用的接口。
Handler:日志處理器,用以表明将日志儲存到什麼地方以及儲存多久。
Formatter:格式化,用以配置日志的輸出格式。
上述三者的關系是:一個 Logger 使用一個 Handler,一個 Handler 使用一個 Formatter。
輸出到控制台
預設輸出級别為WARNING
import logging
logging.info("info")
logging.debug("debug")
logging.warning("warning")
logging.error("error")
logging.critical("critical")
"""
WARNING:root:warning
ERROR:root:error
CRITICAL:root:critical
"""
輸出到檔案
import logging
filename = "{}.log".format(__file__)
fmt = "%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s"
logging.basicConfig(
level=logging.DEBUG,
filename=filename,
filemode="w",
format=fmt
)
logging.info("info")
logging.debug("debug")
logging.warning("warning")
logging.error("error")
logging.critical("critical")
打開日志檔案
2018-06-21 14:32:29,904 - logging_demo.py[line:26] - INFO: info
2018-06-21 14:32:29,905 - logging_demo.py[line:27] - DEBUG: debug
2018-06-21 14:32:29,905 - logging_demo.py[line:28] - WARNING: warning
2018-06-21 14:32:29,905 - logging_demo.py[line:29] - ERROR: error
2018-06-21 14:32:29,905 - logging_demo.py[line:30] - CRITICAL: critical
常用參數
format: 日志格式
eg: "%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s"
datefmt:日志時間顯示
eg: "%Y-%m-%d %H:%M:%S"
同時輸出到控制台和檔案
import logging
# 建立logger對象
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # log等級總開關
# log輸出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
# 控制台handler
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO) # log等級的開關
stream_handler.setFormatter(formatter)
# 檔案handler
file_handler = logging.FileHandler("logging.log")
file_handler.setLevel(logging.WARNING) # log等級的開關
file_handler.setFormatter(formatter)
# 添加到logger
logger.addHandler(stream_handler)
logger.addHandler(file_handler)
# 輸出日志
logger.info("info")
logger.debug("debug")
logger.warning("warning")
logger.error("error")
logger.critical("critical")
日志格式
%(levelno)s: 列印日志級别的數值
%(levelname)s: 列印日志級别名稱
%(pathname)s: 列印目前執行程式的路徑,其實就是sys.argv[0]
%(filename)s: 列印目前執行程式名
%(funcName)s: 列印日志的目前函數
%(lineno)d: 列印日志的目前行号
%(asctime)s: 列印日志的時間
%(thread)d: 列印線程ID
%(threadName)s: 列印線程名稱
%(process)d: 列印程序ID
%(message)s: 列印日志資訊
從配置檔案讀取日志配置
logging.conf
[loggers]
keys = root
[handlers]
keys = logfile
[formatters]
keys = generic
[logger_root]
handlers = logfile
[handler_logfile]
class = handlers.TimedRotatingFileHandler
args = ('test.log', 'midnight', 1, 10)
level = DEBUG
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s] %(message)s
import logging
import logging.config
logging.config.fileConfig('logging.conf')
logging.debug('debug message')
logging.info("info message")
logging.warn('warn message')
logging.error("error message")
logging.critical('critical message')
執行效果test.log
2018-10-27 09:50:13,064 WARNI [root:13] warn message
2018-10-27 09:50:13,065 ERROR [root:14] error message
2018-10-27 09:50:13,065 CRITI [root:15] critical message
參考