日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python logging模塊

系統(tǒng) 1841 0

logging模塊

簡介

logging模塊是python內(nèi)置模塊,專門為打印日志的模塊

          
            import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
          
        

默認情況下python的logging模塊將日志打印到標準輸出中,且只顯示了大于等于WARNING的日志,這說明默認的日志級別為WARNING(日志級別等級CRITICAL > ERROR > WARN > INFO > DEBUG),默認的日志格式日志級別:Logger名稱:用戶輸出消息

靈活配置日志級別,日志格式,輸出位置

          
            import logging
fh = logging.FileHandler(filename='xxx.log',encoding='utf-8') # 存放日志的文件
fh1 = logging.FileHandler(filename='xxx2.log',encoding='utf-8')
sh = logging.StreamHandler()  # 屏幕打印日志
logging.basicConfig(level=logging.INFO, # 日志的級別
                    handlers=[fh,sh,fh1], 
                    datefmt='%Y-%m-%d %H:%M:%S', # 時間
                    format='%(asctime)s - %(name)s[%(lineno)d] - % (levelname)s -%(module)s:  %(message)s') # 格式化格式輸出
logging.debug('debug message')      # 情況越輕
logging.info('info message')        # 信息類的日志
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
          
        

日志切割

          
            import time
import logging
from logging import handlers

sh = logging.StreamHandler()
# 按文件大寫進行切割,只保留5個文件
rh = handlers.RotatingFileHandler('myapp.log', maxBytes=1024,backupCount=5)
# 按時間進行切割 
fh = handlers.TimedRotatingFileHandler(filename='x2.log', when='s', interval=5, encoding='utf-8')
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S %p',
    handlers=[fh,sh,rh],
    level=logging.ERROR
)

for i in range(1,100000):
    time.sleep(1)
    logging.error('KeyboardInterrupt error %s'%str(i))

          
        

配置參數(shù):

            
              logging.basicConfig() # 函數(shù)中可通過具體參數(shù)來更改logging模塊默認行為,可用參數(shù)有:

filename:# 用指定的文件名創(chuàng)建FiledHandler,這樣日志會被存儲在指定的文件中。
filemode:# 文件打開方式,在指定了filename時使用這個參數(shù),默認值為“a”還可指定為“w”。
format:# 指定handler使用的日志顯示格式。
datefmt:# 指定日期時間格式。
level:# 設置rootlogger(后邊會講解具體概念)的日志級別
stream:# 用指定的stream創(chuàng)建StreamHandler。可以指定輸出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默認為sys.stderr。若同時列出了filename和stream兩個參數(shù),則stream參數(shù)會被忽略。

#format參數(shù)中可能用到的格式化串:
%(name)s #Logger的名字
%(levelno)s #數(shù)字形式的日志級別
%(levelname)s #文本形式的日志級別
%(pathname)s #調(diào)用日志輸出函數(shù)的模塊的完整路徑名,可能沒有
%(filename)s #調(diào)用日志輸出函數(shù)的模塊的文件名
%(module)s #調(diào)用日志輸出函數(shù)的模塊名
%(funcName)s #調(diào)用日志輸出函數(shù)的函數(shù)名
%(lineno)d #調(diào)用日志輸出函數(shù)的語句所在的代碼行
%(created)f #當前時間,用UNIX標準的表示時間的浮 點數(shù)表示
%(relativeCreated)d #輸出日志信息時的,自Logger創(chuàng)建以 來的毫秒數(shù)
%(asctime)s #字符串形式的當前時間。默認格式是 “2003-07-08 16:49:45,896”。逗號后面的是毫秒
%(thread)d #線程ID。可能沒有
%(threadName)s #線程名。可能沒有
%(process)d #進程ID。可能沒有
%(message)s #用戶輸出的消息
            
          

logger對象配置

          
            import logging

logger = logging.getLogger()
# 創(chuàng)建一個handler,用于寫入日志文件
fh = logging.FileHandler('test.log',encoding='utf-8') 

# 再創(chuàng)建一個handler,用于輸出到控制臺 
ch = logging.StreamHandler() 
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setLevel(logging.DEBUG)

fh.setFormatter(formatter) 
ch.setFormatter(formatter) 
logger.addHandler(fh) #logger對象可以添加多個fh和ch對象 
logger.addHandler(ch) 

logger.debug('logger debug message') 
logger.info('logger info message') 
logger.warning('logger warning message') 
logger.error('logger error message') 
logger.critical('logger critical message')
          
        

logging庫提供了多個組件:Logger、Handler、Filter、Formatter。Logger對象提供應用程序可直接使用的接口,Handler發(fā)送日志到適當?shù)哪康牡兀現(xiàn)ilter提供了過濾日志信息的方法,F(xiàn)ormatter指定日志顯示格式。另外,可以通過:logger.setLevel(logging.Debug)設置級別,當然,也可以通過

fh.setLevel(logging.Debug)單對文件流設置某個級別。


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 石家庄市| 河津市| 行唐县| 高碑店市| 繁昌县| 古丈县| 马龙县| 习水县| 濮阳市| 苏尼特右旗| 新乡市| 无极县| 宁明县| 屯留县| 平利县| 当雄县| 富川| 潞西市| 武乡县| 汉寿县| 容城县| 苍南县| 临桂县| 扎鲁特旗| 婺源县| 阿克苏市| 桦川县| 兰考县| 平江县| 怀化市| 东乡县| 建阳市| 安远县| 方山县| 巴里| 山东| 马龙县| 荣成市| 容城县| 合山市| 拜城县|