1、簡單使用logging,不建立logger對象。python
import logging logging.basicConfig(level=logging.WARNING, format='%(asctime)s [%(levelname)s] %(message)s') logging.debug('debug log') logging.info('info log') logging.warning('warning log') logging.error('error log') logging.critical('critical log') ------------------------------------------------------------------------- 2017-03-08 16:42:17,420 [WARNING] warning log 2017-03-08 16:42:17,421 [ERROR] error log 2017-03-08 16:42:17,421 [CRITICAL] critical log [Finished in 0.3s]
配置longging,而後使用函數進行日誌輸出。主要就是使用logging.basicConfig方法進行配置。app
有幾個主要的關鍵字參數寫一下。函數
一、filename和filemode參數:當想要把日誌輸出的特定的文件的時候使用這兩個參數指定文件和打開文件的模式。debug
logging.basicConfig(level=logging.WARNING, filename='./log.txt', filemode='w', format='%(asctime)s [%(levelname)s] %(message)s')
這樣就能夠,將日誌寫到log.txt中去了。日誌
二、fromat參數,這個用來設置日誌輸出的format。主要字段以下:code
# | %(name)s Name of the logger (logging channel) # | %(levelno)s Numeric logging level for the message (DEBUG, INFO, # | WARNING, ERROR, CRITICAL) # | %(levelname)s Text logging level for the message ("DEBUG", "INFO", # | "WARNING", "ERROR", "CRITICAL") # | %(pathname)s Full pathname of the source file where the logging # | call was issued (if available) # | %(filename)s Filename portion of pathname # | %(module)s Module (name portion of filename) # | %(lineno)d Source line number where the logging call was issued # | (if available) # | %(funcName)s Function name # | %(created)f Time when the LogRecord was created (time.time() # | return value) # | %(asctime)s Textual time when the LogRecord was created # | %(msecs)d Millisecond portion of the creation time # | %(relativeCreated)d Time in milliseconds when the LogRecord was created, # | relative to the time the logging module was loaded # | (typically at application startup time) # | %(thread)d Thread ID (if available) # | %(threadName)s Thread name (if available) # | %(process)d Process ID (if available) # | %(message)s The result of record.getMessage(), computed just as # | the record is emitted # |
2、將日誌同時輸出到標註錯誤輸出和文件中,使用logger對象。orm
import logging logger = logging.getLogger() logger.setLevel(logging.INFO) #設置日誌輸出的總開關,只有超過INFO級別的日誌會輸出。 logfile = './log.txt' lf = logging.FileHandler(logfile, mode='a+') #建立文件Handler對象 lf.setLevel(logging.DEBUG) # ld = logging.StreamHandler() #建立控制檯StreamHandler對象 ld.setLevel(logging.WARNING) # formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s") lf.setFormatter(formatter) #設置format ld.setFormatter(formatter) logger.addHandler(lf) logger.addHandler(ld) #添加輸出handle對象 logger.debug('debug log') logger.info('info log') logger.warning('warning log') logger.error('error log') logger.critical('critical log') -------------------------------------------------------- 2017-03-08 16:59:19,153 [WARNING] warning log 2017-03-08 16:59:19,153 [ERROR] error log 2017-03-08 16:59:19,153 [CRITICAL] critical log [Finished in 0.3s]
同時日誌會輸出到了當前文件夾中的log.txt中。對象
日誌級別:get
# Level Numeric value # CRITICAL 50 # ERROR 40 # WARNING 30 # INFO 20 # DEBUG 10 # NOTSET 0