def initLogging():
logging.basicConfig(level=logging.INFO,
filename="{}_log.txt".format(__file__[:-3]),
filemode='a',
format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s: %(message)s')
initLogging()
logging.info('這是 loggging info message')
logging.debug('這是 loggging debug message')
logging.warning('這是 loggging a warning message')
logging.error('這是 an loggging error message')
logging.critical('這是 loggging critical message')
logging.info("{}_log.txt".format(__file__[:-3]))
#result
2019-06-21 01:54:33,421-LoggingDemo.py[line:13]-INFO: 這是 loggging info message
2019-06-21 01:54:33,421-LoggingDemo.py[line:15]-WARNING: 這是 loggging a warning message
2019-06-21 01:54:33,421-LoggingDemo.py[line:16]-ERROR: 這是 an loggging error message
2019-06-21 01:54:33,421-LoggingDemo.py[line:17]-CRITICAL: 這是 loggging critical message
2019-06-21 01:54:33,421-LoggingDemo.py[line:19]-INFO: G:/MyCode/study_python/package/frank/LoggingDemo_log.txt
1. 日志級(jí)別
日志一共分成5個(gè)等級(jí)噪漾,從低到高分別是:
DEBUG INFO WARNING ERROR CRITICAL
這5個(gè)等級(jí),也分別對(duì)應(yīng)5種打日志的方法: debug 、info 、warning 、error 谤绳、critical。
默認(rèn)的是WARNING,當(dāng)在WARNING或之上時(shí)才被跟蹤累魔。
2. 日志格式說(shuō)明
logging.basicConfig函數(shù)中,可以指定日志的輸出格式format够滑,這個(gè)參數(shù)可以輸出很多有用的信息垦写,如下:
%(levelno)s: 打印日志級(jí)別的數(shù)值
%(levelname)s: 打印日志級(jí)別名稱(chēng)
%(pathname)s: 打印當(dāng)前執(zhí)行程序的路徑,其實(shí)就是sys.argv[0]
%(filename)s: 打印當(dāng)前執(zhí)行程序名
%(funcName)s: 打印日志的當(dāng)前函數(shù)
%(lineno)d: 打印日志的當(dāng)前行號(hào)
%(asctime)s: 打印日志的時(shí)間
%(thread)d: 打印線(xiàn)程ID
%(threadName)s: 打印線(xiàn)程名稱(chēng)
%(process)d: 打印進(jìn)程ID
%(message)s: 打印日志信息
在工作中給的常用格式如下:
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
這個(gè)格式可以輸出日志的打印時(shí)間彰触,是哪個(gè)模塊輸出的梯投,輸出的日志級(jí)別是什么,以及輸入的日志內(nèi)容况毅。
注意:
只要用過(guò)一次log功能再次設(shè)置格式時(shí)將失效分蓖,
實(shí)際開(kāi)發(fā)中格式肯定不會(huì)經(jīng)常變化,所以剛開(kāi)始時(shí)需要設(shè)定好格式
#源碼
def basicConfig(**kwargs):
Do basic configuration for the logging system.
This function does nothing if the root logger already has handlers
configured. It is a convenience method intended for use by simple scripts
to do one-shot configuration of the logging package.
The default behaviour is to create a StreamHandler which writes to
sys.stderr, set a formatter using the BASIC_FORMAT format string, and
add the handler to the root logger.
A number of optional keyword arguments may be specified, which can alter
the default behaviour.
filename Specifies that a FileHandler be created, using the specified
filename, rather than a StreamHandler.
filemode Specifies the mode to open the file, if filename is specified
(if filemode is unspecified, it defaults to 'a').
format Use the specified format string for the handler.
datefmt Use the specified date/time format.
style If a format string is specified, use this to specify the
type of format string (possible values '%', '{', '$', for
%-formatting, :meth:`str.format` and :class:`string.Template`
- defaults to '%').
level Set the root logger level to the specified level.
stream Use the specified stream to initialize the StreamHandler. Note
that this argument is incompatible with 'filename' - if both
are present, 'stream' is ignored.
handlers If specified, this should be an iterable of already created
handlers, which will be added to the root handler. Any handler
in the list which does not have a formatter assigned will be
assigned the formatter created in this function.
Note that you could specify a stream created using open(filename, mode)
rather than passing the filename and mode in. However, it should be
remembered that StreamHandler does not close its stream (since it may be
using sys.stdout or sys.stderr), whereas FileHandler closes its stream
when the handler is closed.