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)載請附上博文鏈接!