函数式简单配置函数
logging.debug('debug message') # 调试模式 logging.info('info message') # 基础信息 logging.warning('warning message') # 警告 logging.error('error message') # 错误 logging.critical('critical message')# 严重错误
默认状况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志,这说明默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG),默认的日志格式为日志级别:Logger名称:用户输出消息。spa
import logging logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message') 结果: WARNING:root:warning message ERROR:root:error message CRITICAL:root:critical message import logging logging.basicConfig(level=logging.DEBUG) logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message') 结果: DEBUG:root:debug message INFO:root:info message WARNING:root:warning message ERROR:root:error message CRITICAL:root:critical message
灵活配置日志级别,日志格式,输出位置:线程
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='test.log') logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
配置参数:debug
logging.basicConfig()函数中可经过具体参数来更改logging模块默认行为,可用参数有: filename:用指定的文件名建立FiledHandler,这样日志会被存储在指定的文件中。 filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。 format:指定handler使用的日志显示格式。 datefmt:指定日期时间格式。 level:设置rootlogger(后边会讲解具体概念)的日志级别 stream:用指定的stream建立StreamHandler。能够指定输出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。 format参数中可能用到的格式化串: %(name)s Logger的名字 %(levelno)s 数字形式的日志级别 %(levelname)s 文本形式的日志级别 %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有 %(filename)s 调用日志输出函数的模块的文件名 %(module)s 调用日志输出函数的模块名 %(funcName)s 调用日志输出函数的函数名 %(lineno)d 调用日志输出函数的语句所在的代码行 %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示 %(relativeCreated)d 输出日志信息时的,自Logger建立以 来的毫秒数 %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒 %(thread)d 线程ID。可能没有 %(threadName)s 线程名。可能没有 %(process)d 进程ID。可能没有 %(message)s用户输出的消息
info的应用: import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename="test.log") class Manager: def __init__(self,name): self.name=name def create_student(self): student_name=input("输入学生名字:") logging.info("%s creat a student %s" % (self.name,student_name)) cw = Manager("CW") cw.create_student()
debug的应用 import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S') exp = 3+4/4 logging.debug(str(4/4)) print(4)
# basicConfig
# 不能将一个log信息既输出到屏幕 又输出到文件
logger对象配置调试
import logging # 建立一个logger对象 logger = logging.getLogger() # 建立一个文件管理操做符 fh = logging.FileHandler('logger.log',encoding='utf-8') # 建立一个屏幕管理操做符 sh = logging.StreamHandler() # 建立一个日志输出的格式 format1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # 文件管理操做符 绑定一个 格式 fh.setFormatter(format1) # 屏幕管理操做符 绑定一个 格式 sh.setFormatter(format1) logger.setLevel(logging.DEBUG) # logger对象 绑定 文件管理操做符 logger.addHandler(fh) # logger对象 绑定 屏幕管理操做符 logger.addHandler(sh) logger.debug('debug message') logger.info('info message') logger.warning('warning message') logger.error('error message') logger.critical('critical message')