logging-----日志模块

 

 1 import logging
 2 #creat logger 第一步,创建一个记录器
 3 logging_name = 'test'
 4 logger = logging.getLogger(logging_name)
 5 logger.setLevel(logging.DEBUG)
 6 #creat file hander 第二步,创建handler处理器
 7 log_path = './log.log'
 8 fh = logging.FileHandler(log_path)
 9 fh.setLevel(logging.DEBUG)
10 #creat formatter 第三步,格式化器
11 fmt = "%(asctime)s-15s %(levelname)s %(message)s"
12 datefmt = "%a %d %b %Y %H:%M:%S"
13 formatter = logging.Formatter(fmt,datefmt)
14 #add handler and formatter to logger  第四步,处理器添加到记录器
15 fh.setFormatter(formatter)
16 logger.addHandler(fh)
17 #print log info
18 logger.debug('debug message')
19 logger.info('info message')
20 logger.warning('warn message')
21 logger.error('error message')
22 logger.critical('critical message')

 

# 日志格式
#--------------------------------------------------
# %(asctime)s       年-月-日 时-分-秒,毫秒 2013-04-26 20:10:43,745
# %(filename)s      文件名,不含目录
# %(pathname)s      目录名,完整路径
# %(funcName)s      函数名
# %(levelname)s     级别名
# %(lineno)d        行号
# %(module)s        模块名
# %(message)s       消息体
# %(name)s          日志模块名
# %(process)d       进程id
# %(processName)s   进程名
# %(thread)d        线程id
# %(threadName)s    线程名

猜你喜欢

转载自www.cnblogs.com/minghuanghuang/p/9258167.html