記錄Python腳本的運(yùn)行日志的方法
一、logging模塊
Python中有一個(gè)模塊logging,可以直接記錄日志
# 日志級(jí)別 # CRITICAL 50 # ERROR 40 # WARNING 30 # INFO 20 # DEBUG 10
logging.basicConfig()函數(shù)中的具體參數(shù):
filename: 指定的文件名創(chuàng)建FiledHandler,這樣日志會(huì)被存儲(chǔ)在指定的文件中;
filemode: 文件打開(kāi)方式,在指定了filename時(shí)使用這個(gè)參數(shù),默認(rèn)值為“w”還可指定為“a”;
format: 指定handler使用的日志顯示格式;
datefmt: 指定日期時(shí)間格式。,格式參考strftime時(shí)間格式化(下文)
level: 設(shè)置rootlogger的日志級(jí)別
stream: 用指定的stream創(chuàng)建StreamHandler??梢灾付ㄝ敵龅絪ys.stderr,sys.stdout或者文件,默認(rèn)為sys.stderr。
若同時(shí)列出了filename和stream兩個(gè)參數(shù),則stream參數(shù)會(huì)被忽略。
format參數(shù)中可能用到的格式化信息:
%(name)s |
Logger的名字 |
%(levelno)s |
數(shù)字形式的日志級(jí)別 |
%(levelname)s |
文本形式的日志級(jí)別 |
%(pathname)s |
調(diào)用日志輸出函數(shù)的模塊的完整路徑名,可能沒(méi)有 |
%(filename)s |
調(diào)用日志輸出函數(shù)的模塊的文件名 |
%(module)s |
調(diào)用日志輸出函數(shù)的模塊名 |
%(funcName)s |
調(diào)用日志輸出函數(shù)的函數(shù)名 |
%(lineno)d |
調(diào)用日志輸出函數(shù)的語(yǔ)句所在的代碼行 |
%(created)f |
當(dāng)前時(shí)間,用UNIX標(biāo)準(zhǔn)的表示時(shí)間的浮 點(diǎn)數(shù)表示 |
%(relativeCreated)d |
輸出日志信息時(shí)的,自Logger創(chuàng)建以 來(lái)的毫秒數(shù) |
%(asctime)s |
字符串形式的當(dāng)前時(shí)間。默認(rèn)格式是 “2003-07-08 16:49:45,896”。逗號(hào)后面的是毫秒 |
%(thread)d |
線程ID??赡軟](méi)有 |
%(threadName)s |
線程名??赡軟](méi)有 |
%(process)d |
進(jìn)程ID。可能沒(méi)有 |
%(message)s |
用戶輸出的消息 |
二、logging模塊測(cè)試
1、打印日志到標(biāo)準(zhǔn)輸出中
import logging logging.debug('debug message') logging.info('info message') logging.warning('warning message')
輸出結(jié)果
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe D:/pyworkpeace/tupian.py 'https://www.tianyancha.com/login'
WARNING:root:warning messageProcess finished with exit code 0
可以看出默認(rèn)情況下Python的logging模塊將日志打印到了標(biāo)準(zhǔn)輸出中,且只顯示了大于等于WARNING級(jí)別的日志。默認(rèn)的日志的格式為:
日志級(jí)別:Logger名稱:用戶輸出消息
2、將日志文件輸入到文件中
import os logging.basicConfig(filename=os.path.join(os.getcwd(),'log.txt'),level=logging.DEBUG) logging.debug('this is a message')
運(yùn)行這三行代碼后會(huì)在安裝Python的目錄中出現(xiàn)一個(gè)log.txt文件,文件內(nèi)容
DEBUG:root:this is a message
DEBUG:root:debug message
3、自定義格式,輸出日志文件
# -*-coding:utf-8-*- import logging def console_out(logFilename): ''''' Output log to file and console ''' # Define a Handler and set a format which output to file logging.basicConfig( level=logging.DEBUG, # 定義輸出到文件的log級(jí)別,大于此級(jí)別的都被輸出 format='%(asctime)s %(filename)s : %(levelname)s %(message)s', # 定義輸出log的格式 datefmt='%Y-%m-%d %A %H:%M:%S', # 時(shí)間 filename=logFilename, # log文件名 filemode='w') # 寫入模式“w”或“a” # Define a Handler and set a format which output to console console = logging.StreamHandler() # 定義console handler console.setLevel(logging.INFO) # 定義該handler級(jí)別 formatter = logging.Formatter('%(asctime)s %(filename)s : %(levelname)s %(message)s') # 定義該handler格式 console.setFormatter(formatter) # Create an instance logging.getLogger().addHandler(console) # 實(shí)例化添加handler # Print information # 輸出日志級(jí)別 logging.debug('logger debug message') logging.info('logger info message') logging.warning('logger warning message') logging.error('logger error message') logging.critical('logger critical message') if __name__ == "__main__": console_out('logging.log')
輸出結(jié)果:
此時(shí)也會(huì)自動(dòng)生成一個(gè)日志文件,日志文件和運(yùn)行文件在同一個(gè)文件夾中,文件名logging.log
2017-10-23 Monday 11:37:59 hgghf : DEBUG logger debug message
2017-10-23 Monday 11:37:59 hgghf : INFO logger info message
2017-10-23 Monday 11:37:59 hgghf : WARNING logger warning message
2017-10-23 Monday 11:37:59 hgghf : ERROR logger error message
2017-10-23 Monday 11:37:59 hgghf : CRITICAL logger critical message
修改輸出路徑:
filename='/tmp/test1.log', # log文件名
當(dāng)將腳本中這行代碼換一下,那么我們輸出日志的路徑地址就換成了D:\tmp
下面的方式同樣可以達(dá)到上述結(jié)果
4、自定義輸出位置
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='/tmp/test.log', filemode='w') logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
由于運(yùn)行腳本放在D:\pyworkpeace\下,輸出文件在D盤tmp文件夾下test.log,內(nèi)容如下:
Mon, 23 Oct 2017 15:00:05 tupian.py[line:11] DEBUG debug message
Mon, 23 Oct 2017 15:00:05 tupian.py[line:12] INFO info message
Mon, 23 Oct 2017 15:00:05 tupian.py[line:13] WARNING warning message
Mon, 23 Oct 2017 15:00:05 tupian.py[line:14] ERROR error message
Mon, 23 Oct 2017 15:00:05 tupian.py[line:15] CRITICAL critical message
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python中l(wèi)ogging日志記錄到文件及自動(dòng)分割的操作代碼
- python3中的logging記錄日志實(shí)現(xiàn)過(guò)程及封裝成類的操作
- Python loguru日志庫(kù)之高效輸出控制臺(tái)日志和日志記錄
- python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割
- python logging重復(fù)記錄日志問(wèn)題的解決方法
- Python Logging 日志記錄入門學(xué)習(xí)
- Python 記錄日志的靈活性和可配置性介紹
- python日志記錄模塊實(shí)例及改進(jìn)
- Python記錄詳細(xì)調(diào)用堆棧日志的方法
- Python 程序員必須掌握的日志記錄
相關(guān)文章
Python常用數(shù)據(jù)庫(kù)接口sqlite3和MySQLdb學(xué)習(xí)指南
在本章節(jié)中,我們將學(xué)習(xí) Python 中常用的數(shù)據(jù)庫(kù)接口,包括 sqlite3用于SQLite數(shù)據(jù)庫(kù)和MySQLdb用于 MySQL 數(shù)據(jù)庫(kù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Python基于identicon庫(kù)創(chuàng)建類似Github上用的頭像功能
這篇文章主要介紹了Python基于identicon庫(kù)創(chuàng)建類似Github上用的頭像功能,結(jié)合具體實(shí)例形式分析了identicon庫(kù)操作圖形的具體步驟與相關(guān)使用技巧,需要的朋友可以參考下2017-09-09淺談python連續(xù)賦值可能引發(fā)的錯(cuò)誤
今天小編就為大家分享一篇淺談python連續(xù)賦值可能引發(fā)的錯(cuò)誤,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11Python中實(shí)現(xiàn)文本預(yù)處理的方法小結(jié)
文本數(shù)據(jù)是數(shù)據(jù)科學(xué)和自然語(yǔ)言處理領(lǐng)域的關(guān)鍵組成部分,本文將深入探討Python中文本預(yù)處理的關(guān)鍵步驟,并提供豐富的示例代碼,希望對(duì)大家有所幫助2023-12-12Python機(jī)器學(xué)習(xí)NLP自然語(yǔ)言處理基本操作之Seq2seq的用法
Seq2Seq模型是輸出的長(zhǎng)度不確定時(shí)采用的模型,這種情況一般是在機(jī)器翻譯的任務(wù)中出現(xiàn),將一句中文翻譯成英文,那么這句英文的長(zhǎng)度有可能會(huì)比中文短,也有可能會(huì)比中文長(zhǎng),所以輸出的長(zhǎng)度就不確定了2021-10-10解決Python?出現(xiàn)File?“<stdin>“,?line?1非語(yǔ)法錯(cuò)誤的問(wèn)題
這篇文章主要介紹了Python?出現(xiàn)File?“<stdin>“,?line?1非語(yǔ)法錯(cuò)誤的解決辦法,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03