Python日志模塊logging簡介
logging分為4個模塊: loggers, handlers, filters, and formatters.
●loggers: 提供應(yīng)用程序調(diào)用的接口
●handlers: 把日志發(fā)送到指定的位置
●filters: 過濾日志信息
●formatters: 格式化輸出日志
Logger
Logger.setLevel() 設(shè)置日志級別
Logger.addHandler()和Logger.removeHandler() 增加和刪除日志處理器
Logger.addFilter()和Logger.removeFilter() 增加和刪除過濾器
Logger.debug(), Logger.info(), Logger.warning(), Logger.error(), and Logger.critical() 創(chuàng)建不同的級別的日志
getLogger() 獲取日志的根實例
Handler
setLevel() 設(shè)置日志級別
setFormatter() 設(shè)置輸出格式
addFilter() and removeFilter() 增加和刪除過濾器
Formatter
默認(rèn)形式為: %Y-%m-%d %H:%M:%S.
格式為: %()s
日志配置管理
硬編碼形式
import logging
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
輸出
$ python simple_logging_module.py
2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message
2005-03-19 15:10:26,620 - simple_example - INFO - info message
2005-03-19 15:10:26,695 - simple_example - WARNING - warn message
2005-03-19 15:10:26,697 - simple_example - ERROR - error message
2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message
通過文件配置管理日志
代碼:
import logging
import logging.config
logging.config.fileConfig('logging.conf')
# create logger
logger = logging.getLogger('simpleExample')
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
配置文件:
[loggers]
keys=root,simpleExample
[handlers]
keys=consoleHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
輸出:
$ python simple_logging_config.py
2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message
2005-03-19 15:38:55,979 - simpleExample - INFO - info message
2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message
2005-03-19 15:38:56,055 - simpleExample - ERROR - error message
2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message
日志格式
%(levelno)s: 打印日志級別的數(shù)值
%(levelname)s: 打印日志級別名稱
%(pathname)s: 打印當(dāng)前執(zhí)行程序的路徑,其實就是sys.argv[0]
%(filename)s: 打印當(dāng)前執(zhí)行程序名
%(funcName)s: 打印日志的當(dāng)前函數(shù)
%(lineno)d: 打印日志的當(dāng)前行號
%(asctime)s: 打印日志的時間
%(thread)d: 打印線程ID
%(threadName)s: 打印線程名稱
%(process)d: 打印進(jìn)程ID
%(message)s: 打印日志信息
流程圖
相關(guān)文章
在pytorch中如何查看模型model參數(shù)parameters
這篇文章主要介紹了在pytorch中如何查看模型model參數(shù)parameters,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11python數(shù)據(jù)類型判斷type與isinstance的區(qū)別實例解析
這篇文章主要介紹了python數(shù)據(jù)類型判斷type與isinstance的區(qū)別實例解析,具有一定參考價值,需要的朋友可以了解下。2017-10-10在Python中執(zhí)行和調(diào)用JavaScript的多種方法小結(jié)
JavaScript(JS)是一種常用的腳本語言,通常用于網(wǎng)頁開發(fā),但有時也需要在Python中執(zhí)行或調(diào)用JavaScript代碼,本文將詳細(xì)介紹Python中執(zhí)行和調(diào)用JavaScript的多種方法,每種方法都將附有示例代碼,方便理解如何在Python中與JavaScript進(jìn)行互動,需要的朋友可以參考下2023-11-11Python循環(huán)語句中else的用法總結(jié)
這篇文章給大家整理了關(guān)于Python中循環(huán)語句中else的用法,包括常規(guī)的 if else 用法、if else 快捷用法、與 for 關(guān)鍵字一起用、與 while 關(guān)鍵字一起用以及與 try except 一起用的用法總結(jié),有需要的朋友們可以參考借鑒。2016-09-09基于python的Tkinter實現(xiàn)一個簡易計算器
這篇文章主要介紹了基于python的Tkinter實現(xiàn)一個簡易計算器的相關(guān)資料,還為大家分享了僅用用50行Python代碼實現(xiàn)的簡易計算器,感興趣的小伙伴們可以參考一下2015-12-12詳解Python使用apscheduler定時執(zhí)行任務(wù)
在平常的工作中幾乎有一半的功能模塊都需要定時任務(wù)來推動,例如項目中有一個定時統(tǒng)計程序,定時爬出網(wǎng)站的URL程序,定時檢測釣魚網(wǎng)站的程序等等,都涉及到了關(guān)于定時任務(wù)的問題,所以就找到了python的定時任務(wù)模塊2022-03-03