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: 指定輸出的格式和內(nèi)容,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: 設(shè)置日志級別,默認為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')

結(jié)果:

? ? 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

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

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

相關(guān)閱讀更多精彩內(nèi)容

  • 模塊和包 一 模塊 1 什么是模塊? 常見的場景:一個模塊就是一個包含了python定義和聲明的文件,文件名就是...
    go以恒閱讀 2,347評論 0 4
  • 一、給路由添加正則表達式 給路由參數(shù)添加正則表達式的原因:在實際開發(fā)時,url中往往會帶有很多的參數(shù),例如:/ad...
    IIronMan閱讀 1,759評論 1 12
  • 前言 在自動化測試實踐過程中,必不可少的就是進行日志管理,方便調(diào)試和生產(chǎn)問題追蹤,python提供了logg...
    苦葉子閱讀 933評論 0 0
  • 圖片發(fā)自簡書App 曉窗前,海棠重,香枕斜箋驚夢。東風(fēng)淚,一行行,過簾沾晚妝。 雙雙燕,聲聲喚,寸寸春...
    雪珝閱讀 229評論 1 1
  • 送福利啦價值過千元: 20本HR精選書籍+100個超10萬觀看量學(xué)習(xí)微視頻+2018人力資源發(fā)展趨勢報告。 要的,...
    老李說心態(tài)閱讀 293評論 0 2

友情鏈接更多精彩內(nèi)容