麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

logging文件日志

2019-11-14 17:41:38
字體:
來源:轉載
供稿:網友

1. 例子

import logginglogging.basicConfig(filename='log.txt',  #文件名                    level=logging.DEBUG, #級別                    format=u'時間:%(asctime)s/n級別:%(levelname)s/n消息:%(message)s/n',  #日志格式                    datefmt='%Y-%m-%d %H:%M:%S') # 時間格式logging.debug(u'第一條記錄') logging.info(u'第二條記錄') 

2. 級別

日志所記錄的消息可以劃分為不同的級別,一般用以下幾種預定義的級別。

每種級別有對應的值,可以用來比較級別的高低。

級別
CRITICAL50
ERROR40
WARNING30
INFO20
DEBUG10
NOTSET0

每個級別都有對應的方法,用小寫字母,比如 logging.debug() , logging.info(),分別用來記錄 DEBUG 級別和 INFO 級別的消息。

logging.basicConfig 中配置的級別可以用來過濾消息,比配置級別低的消息將被忽略,不會寫入文件。

比如,如果一開始配置的是 level=logging.INFO ,那么調用 logging.debug() 處理的消息將被忽略,不會記錄到文件。只有用 info() 或者 warning() 以及更高級別才會被記錄。

3. 日志格式

格式化字符串支持如下參數:

參數解釋
%(asctime)sHuman-readable time when the LogRecord was created. By default this is of the form '2003-07-08 16:49:45,896'.
%(created)fTime when the LogRecord was created (as returned by time.time()).
%(filename)sFilename portion of pathname.
%(funcName)sName of function containing the logging call.
%(levelname)sText logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
%(levelno)sNumeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
%(lineno)dSource line number where the logging call was issued (if available).
%(module)sModule (name portion of filename).
%(msecs)dMillisecond portion of the time when the LogRecord was created.
%(message)sThe logged message.
%(name)sName of the logger used to log the call.
%(pathname)sFull pathname of the source file where the logging call was issued (if available).
%(PRocess)dProcess ID (if available).
%(processName)sProcess name (if available).
%(relativeCreated)dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
%(thread)dThread ID (if available).
%(threadName)sThread name (if available).

3. 時間格式

時間格式化字符串與time.strftime()使用相同的參數

參數解釋
%aLocale's abbreviated weekday name.
%ALocale's full weekday name.
%bLocale's abbreviated month name.
%BLocale's full month name.
%cLocale's appropriate date and time representation.
%dDay of the month as a decimal number [01,31].
%HHour (24-hour clock) as a decimal number [00,23].
%IHour (12-hour clock) as a decimal number [01,12].
%jDay of the year as a decimal number [001,366].
%mMonth as a decimal number [01,12].
%MMinute as a decimal number [00,59].
%pLocale's equivalent of either AM or PM.
%SSecond as a decimal number [00,61].
%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53].
%wWeekday as a decimal number [0(Sunday),6].
%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53].
%xLocale's appropriate date representation.
%XLocale's appropriate time representation.
%yYear without century as a decimal number [00,99].
%YYear with century as a decimal number.
%ZTime zone name (no characters if no time zone exists).
%%A literal '%' character.

4. 另一種寫法

麻煩一點,但是可以定制多個logger

import logginglogger = logging.getLogger(u'mylogger')  handler = logging.FileHandler(u'log1.txt')  formatter = logging.Formatter(u'時間:%(asctime)s/n級別:%(levelname)s/n消息:%(message)s/n')  handler.setFormatter(formatter)  logger.addHandler(handler)  logger.setLevel(logging.DEBUG)logger.debug(u'第一條記錄')logger.info(u'第二條記錄')logger2 = logging.getLogger(u'mylogger2')  handler2 = logging.FileHandler(u'log2.txt')  formatter2 = logging.Formatter(u'時間:%(asctime)s/n級別:%(levelname)s/n消息:%(message)s/n')  handler2.setFormatter(formatter2)  logger2.addHandler(handler2)  logger2.setLevel(logging.DEBUG)logger2.debug(u'第一條記錄')logger2.info(u'第二條記錄')

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: av免费在线网 | 国产一级一区 | 精品中文字幕在线观看 | 亚洲情av| 国产成人综合在线 | 精品中文字幕在线播放 | 精品国产一区二区三区四区在线 | 一级免费看片 | 情侣啪啪网站 | av在线更新 | 宅男噜噜噜66国产在线观看 | 老a影视网站在线观看免费 国产精品久久久久久久久久尿 | 欧美一区二区网站 | 97干色| 国产亚洲精品久久久久久网站 | 欧美成人一区免费视频 | 日本a在线观看 | 国产成人精品二区 | 欧美大电影免费观看 | 欧美成人a | 久久国产不卡 | 国产在线免 | 日日综合| 亚洲成人精品区 | 线观看免费完整aaa 欧美在线一级 | 免费黄色在线电影 | 日本68xxxx| 今井夏帆av一区二区 | 亚洲国产一区二区三区 | 精品亚洲成a人在线观看 | 香蕉久久久精品 | 国产精品91在线 | 韩国十九禁高潮床戏在线观看 | 国产在线一级片 | 高清国产午夜精品久久久久久 | 欧美日韩亚洲精品一区二区三区 | 欧美一区二区三区中文字幕 | 免费人成在线播放 | 一级做a爱视频 | 亚洲第一成人在线视频 | 久久综合综合久久 |