Python 函數(shù)——logging.basicConfig

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='myapp.log',

? ? ? ? ? ? ? ? ? ? filemode='w')


? ? logging.debug('This is debug message')

? ? logging.info('This is info message')

? ? logging.warning('This is warning message')

? ? logging.basicConfig函數(shù)各參數(shù):

? ? filename: 指定日志文件名

? ? filemode: 和file函數(shù)意義相同,指定日志文件的打開模式,'w'或'a'

? ? format: 指定輸出的格式和內容,format可以輸出很多有用信息,如上例所示:

? ? %(levelno)s: 打印日志級別的數(shù)值

? ? %(levelname)s: 打印日志級別名稱

? ? %(pathname)s: 打印當前執(zhí)行程序的路徑,其實就是sys.argv[0]

? ? %(filename)s: 打印當前執(zhí)行程序名

? ? %(funcName)s: 打印日志的當前函數(shù)

? ? %(lineno)d: 打印日志的當前行號

? ? %(asctime)s: 打印日志的時間

? ? %(thread)d: 打印線程ID

? ? %(threadName)s: 打印線程名稱

? ? %(process)d: 打印進程ID

? ? %(message)s: 打印日志信息

? ? datefmt: 指定時間格式,同time.strftime()

? ? level: 設置日志級別,默認為logging.WARNING

? ? stream: 指定將日志的輸出流,可以指定輸出到sys.stderr,sys.stdout或者文件,默認輸出到sys.stderr,當stream和filename同時指定時,stream被忽略

例子:

? ? logging.basicConfig(level=logging.DEBUG,

? ? ? ? ? ? ? ? ? ? ? ? format='%(asctime)s %(thread)d %(threadName)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',

? ? ? ? ? ? ? ? ? ? ? ? datefmt='%a, %d %b %Y %H:%M:%S',

? ? ? ? ? ? ? ? ? ? ? ? filename=logfile_path,

? ? ? ? ? ? ? ? ? ? ? ? filemode='w')

結果:

? ? Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:38] DEBUG Start the application.

? ? Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:25] DEBUG Begin class constructor.

? ? Sat, 04 Feb 2017 13:53:15 4296 MainThread ScriptRecorder.py[line:29] DEBUG Show the init window.

? ? Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:18] DEBUG Init window constructor.

? ? Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:62] DEBUG Get devices count.

? ? Sat, 04 Feb 2017 13:53:15 4296 MainThread init_window.py[line:55] DEBUG Get devices list.

---------------------

作者:fengleieee

來源:CSDN

原文:https://blog.csdn.net/fengleieee/article/details/54862877

版權聲明:本文為博主原創(chuàng)文章,轉載請附上博文鏈接!

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容