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