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'第二條記錄')
日志所記錄的消息可以劃分為不同的級別,一般用以下幾種預定義的級別。
每種級別有對應的值,可以用來比較級別的高低。
級別 | 值 |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
每個級別都有對應的方法,用小寫字母,比如 logging.debug() , logging.info(),分別用來記錄 DEBUG 級別和 INFO 級別的消息。
logging.basicConfig 中配置的級別可以用來過濾消息,比配置級別低的消息將被忽略,不會寫入文件。
比如,如果一開始配置的是 level=logging.INFO ,那么調用 logging.debug() 處理的消息將被忽略,不會記錄到文件。只有用 info() 或者 warning() 以及更高級別才會被記錄。
格式化字符串支持如下參數:
參數 | 解釋 |
---|---|
%(asctime)s | Human-readable time when the LogRecord was created. By default this is of the form '2003-07-08 16:49:45,896'. |
%(created)f | Time when the LogRecord was created (as returned by time.time()). |
%(filename)s | Filename portion of pathname. |
%(funcName)s | Name of function containing the logging call. |
%(levelname)s | Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'). |
%(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
%(lineno)d | Source line number where the logging call was issued (if available). |
%(module)s | Module (name portion of filename). |
%(msecs)d | Millisecond portion of the time when the LogRecord was created. |
%(message)s | The logged message. |
%(name)s | Name of the logger used to log the call. |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
%(PRocess)d | Process ID (if available). |
%(processName)s | Process name (if available). |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
%(thread)d | Thread ID (if available). |
%(threadName)s | Thread name (if available). |
時間格式化字符串與time.strftime()使用相同的參數
參數 | 解釋 |
---|---|
%a | Locale's abbreviated weekday name. |
%A | Locale's full weekday name. |
%b | Locale's abbreviated month name. |
%B | Locale's full month name. |
%c | Locale's appropriate date and time representation. |
%d | Day of the month as a decimal number [01,31]. |
%H | Hour (24-hour clock) as a decimal number [00,23]. |
%I | Hour (12-hour clock) as a decimal number [01,12]. |
%j | Day of the year as a decimal number [001,366]. |
%m | Month as a decimal number [01,12]. |
%M | Minute as a decimal number [00,59]. |
%p | Locale's equivalent of either AM or PM. |
%S | Second as a decimal number [00,61]. |
%U | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. |
%w | Weekday as a decimal number [0(Sunday),6]. |
%W | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. |
%x | Locale's appropriate date representation. |
%X | Locale's appropriate time representation. |
%y | Year without century as a decimal number [00,99]. |
%Y | Year with century as a decimal number. |
%Z | Time zone name (no characters if no time zone exists). |
%% | A literal '%' character. |
麻煩一點,但是可以定制多個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'第二條記錄')
新聞熱點
疑難解答