日志级别:
NumberName
50CRITICAL
40ERROR
30WARNING(默认)
20INFO
10DEBUG
**比设定的level低的level会被过滤掉,不打印
输出格式:
%(name)s日志名称
%(asctime)s日志时间
%(filename)s文件名
%(pathname)s文件的全路径名
%(funcName)s函数名
%(levelname)s日志等级
%(levelno)s数字化日志等级
%(lineno)d行数
%(module)s模块名
%(msecs)d时间中毫秒部分
%(process)d进程ID
%(processName)s进程名
%(thread)d线程ID
%(threadName)s线程名
一.基础basicConfig用法
import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger=logging.getLogger(__file__) #如果不给定参数,默认参数为root
logger.info('this is a info message')
logger.debug('this is a debug message')
logger.error('this is a error message')
*补充*
basicConfig的参数:
level:设置日志级别
format:指定输出格式,如上面的输出格式
filename:创建FileHandler,当这个参数使用后,不再使用StreamHandler
filemode:文件写入方式,默认是a
datefmt:设定时间日期格式,这个格式会覆盖format里面的asctime的格式
stream:设定StreamHandler,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename参数同时指定时,stream会被忽略
二.FileHandler,StreamHandler日志输出到屏幕和console
import logging import time #create logger logger=logging.getLogger(__file__) logger.setLevel(logging.DEBUG) #create FileHandler filehandler=logging.FileHandler('a.log') filehandler.setLevel(logging.INFO) #create StreamFilehandler streamhandler=logging.StreamHandler() streamhandler.setLevel(logging.INFO) #Create formatter formatter=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') formatter.datefmt="%Y-%m-%d %H:%M:%S" filehandler.setFormatter(formatter) streamhandler.setFormatter(formatter) logger.addHandler(filehandler) logger.addHandler(streamhandler) logger.info('this is a info message') logger.debug('this is a debug message') logger.error('this is a error message')
三.Config.ini文件来配置logging模块:
Config.ini文件:
######################Formatters################### [formatters] keys=f1 #定义f1 formatter,可以定义多个,key=f1,f2,f3... [formatter_f1] #注意格式是formatter_f1 format=%(filename)s-%(levelname)s-%(message)s datefmt=%Y-%m-%d %H:%M:%S #####################Handlers###################### [handlers] keys=h1,h2 #定义h1,h2两个handler,一个是FileHandler,一个是StreamHandler [handler_h1] #注意格式handler_h1 class=FileHandler args=('a.log','a') level=INFO formatter=f1 [handler_h2] #注意格式handler_h2 class=StreamHandler args=(sys.stdout,) #注意sys.stdout后面的,号 level=INFO formatter=f1 ###################loggers####################### [loggers] keys=root #root是父类,是必须的,还可以自定义其它的,key=root,l1,l2... [logger_root] #注意格式 level=DEBUG handlers=h1,h2 #同时输出到console和文件
import logging from logging.config import fileConfig fileConfig('logger_config.ini') logger1=logging.getLogger() logger1.info('this is a info message')
四.yaml 文件配置logging模块
logger.yaml:
version: 1 disable_existing_logger: False formatters: f1: format: "%(filename)s-%(levelname)s-%(message)s" datefmt: "%Y-%m-%d %H:%M:%S" handlers: h1: class: logging.StreamHandler level: INFO formatter: f1 stream: ext://sys.stdout h2: class: logging.FileHandler level: INFO formatter: f1 filename: 'a.log' loggers: l1: level: DEBUG handlers: [h1,h2] propagate: no root: level: INFO handlers: [h1,h2]
import logging import yaml from logging.config import dictConfig with open('logger.yaml','r') as f: config=yaml.safe_load(f) dictConfig(config) logger=logging.getLogger() logger.info('this is a info message')
五.json文件配置logging模块
logger.json:
{ "version": 1, "disable_existing_loggers": false, "formatters":{ "f1":{ "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s", "datefmt": "%Y-%m-%d %H:%M:%S" } }, "handlers": { "h1": { "class": "logging.FileHandler", "level": "INFO", "formatter": "f1", "filename": "a.log" }, "h2": { "class": "logging.StreamHandler", "level": "INFO", "formatter": "f1", "stream": "ext://sys.stdout" } }, "loggers":{ "l1":{ "level": "DEBUG", "handlers": [ "h1", "h2" ] } }, "root":{ "level":"DEBUG", "handlers":["h1","h2"] } }
import logging import json from logging.config import dictConfig with open('logger.json','r') as f: config=json.load(f) dictConfig(config) logger=logging.getLogger() logger.info('this is a info message')