Python_基礎_(llogging,)
import logging
logging.debug("")
logging.info("")
logginf.warning("")
logging.error("")
logging.critical("")# 默認的級別為warning
# level總共分5個級別:debug < info< warning< error< critical
## 設置日誌級別,日誌格式,輸出位置
import logging logging.basicConfig( level=logging.DEBUG, filename= "logger.log", filemode = "w", format = "%(asctime)s %(lineno)d %(message)s %(filename)s" ) logging.debug(‘debug message‘) logging.info(‘info message‘) logging.warning(‘warning message‘) logging.error(‘error message‘) logging.critical(‘critical message‘)
# 在輸出文件logging.log中 當日誌級別設置為debug時,輸出大於等於debug的日誌信息2018-12-18 22:46:05,141 16 debug message 日誌模塊.py 2018-12-18 22:46:05,190 17 info message 日誌模塊.py 2018-12-18 22:46:05,190 18 warning message 日誌模塊.py 2018-12-18 22:46:05,190 19 error message 日誌模塊.py 2018-12-18 22:46:05,190 20 critical message 日誌模塊.py
# 當日誌的級別設置為 warning 輸出大於等於Warning的日誌信息 2018-12-18 22:50:55,063 12 warning message 日誌模塊.py2018-12-18 22:50:55,063 13 error message 日誌模塊.py 2018-12-18 22:50:55,063 14 critical message 日誌模塊.py
##
filename # 指定日誌的文件ming
filemode # 和file函數的意義相同,指定日誌的打開模式,"w","a"
datefmt # 指定日期的時間格式
level # 設置日誌的級別
format # 指定輸出的格式和內容
%(name)s # 日誌的名字
%(levelno)s # 數字形式的日誌級別
%(levelname)s # 文本形式的日誌級別
%(pathname)s # 調用日誌輸出函數的模塊的完整路徑名,可能沒有
%(filename)s # 調用日誌輸出函數的模塊的文件名
%(module)s # 調用日誌輸出函數的模塊名
%(funcName)s # 調用日誌輸出函數的函數名
%(lineno)d # 調用日誌輸出函數的語句所在的代碼行
%(created)f # 當前時間,用UNIX標準的表示時間的浮 點數表示
%(relativeCreated)d # 輸出日誌信息時的,自Logger創建以 來的毫秒數
%(asctime)s # 字符串形式的當前時間。默認格式是 “2003-07-08 16:49:45,896”。逗號後面的是毫秒
%(thread)d # 線程ID。可能沒有
%(threadName)s # 線程名。可能沒有
%(process)d # 進程ID。可能沒有
%(message)s # 用戶輸出的消息
## logger對象
logger = logging.getLogger() # 創建logger對象 fh = loggign.FileHandler("test.log") # 可向文件中發送內容 ch = logging.StreamHandler() # 可向控制臺輸送內容 fm = logging.Formatter("%(asctime)s %(lineno)d %(message)s %(filename)s") # 格式 fh.setFormat(fm) # 給輸出到文件的設置格式 ch.setFormat(fm) # 給輸出到控制臺的設置格式 logger.addHandler(fh) # 給logger logger.addHandler(ch) # 給logger logger.setLevel("DEBUG") # 可對logger設置級別 logger.debug("hello") logger.info("hello") logger.waring("hello") logger.error("hello") logger.critical("hello")
## 日誌補充
import logging logger1 = logging.getlogger("mylogger") logger1.setLevel(logging.DEBUG) logger2 = logging.getlogger("mylogger") logger2.setLevel(logging.INFO) # 註實際上logger1與logger2指向同一個對象,指向mylogger 設置級別時,將logger1設為DEBUG,將logger2設為INFO,實際上兩個都為INFO,個輸出4條日誌 . . . logger1.debug("hello") logger1.info("hello") logger1.waring("hello") logger1.error("hello") logger1.critical("hello") logger2.debug("hello") logger2.info("hello") logger2.waring("hello") logger2.error("hello") logger2.critical("hello")二,configparser 模塊
## 配置文件解析模塊
## 註:配置文件中不區分大小寫
# 常見的配置文件
待完成...
Python_基礎_(llogging,)