Python_58之logging模块

日志模块

非常简单的限制线程安全的日志记录模块

就是一个写文件的模块

import logging
logging.basicConfig(filename='log_ss.log',format='%(asctime)s-%(name)s-%(levelname)s -%(module)s:%(message)s',datefmt='%Y-%m-%d %H:%M:%S %p',level=10)#日志文件路径 ; 操作时间 用户 操作类型 操作对象 内容;时间格式;level=10 表示logging 中编号大于等于10的方法才记录执行 10也可以用固定的变量代替logging.ERROR就代表40
"""
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

_levelToName = {
CRITICAL: 'CRITICAL',
ERROR: 'ERROR',
WARNING: 'WARNING',
INFO: 'INFO',
DEBUG: 'DEBUG',
NOTSET: 'NOTSET',
}
"""
#根据logging.ERROR 我们可以知道 各个方法的编号
logging.error('error')
logging.debug('debug')
logging.warn('warning')
logging.info('info')
logging.critical('catial')
输出————————————————————————————————————————生成一个log_ss.log文件——————————————————————————————————————————————————————————————————————
2018-05-08 12:56:27 PM-root-DEBUG -testloging:debug
2018-05-08 12:59:38 PM-root-ERROR -testloging:debug
2018-05-08 13:06:22 PM-root-ERROR -testloging:error
2018-05-08 13:06:22 PM-root-DEBUG -testloging:debug
2018-05-08 13:06:22 PM-root-WARNING -testloging:warning
2018-05-08 13:06:22 PM-root-INFO -testloging:info
2018-05-08 13:06:22 PM-root-CRITICAL -testloging:catial
*******************************************************************************************************************************************
其实logging.debug error info等方法执行的都是logging.log方法
import logging
logging.basicConfig(filename='log_ss.log',format='%(asctime)s-%(name)s-%(levelname)s -%(module)s:%(message)s',datefmt='%Y-%m-%d %H:%M:%S %p',level=10)#日志文件路径 ; 操作时间 用户 操作类型 操作对象 内容;时间格式;level=10 表示logging 中编号大于等于10的方法才记录执行
logging.log(10,'dayu10')
输出————————————————————————————————————————————————————————————————————————————————————#log方法中的编号一定要大于等于level的值
2018-05-08 13:08:45 PM-root-DEBUG -testloging:dayu10
*******************************************************************************************************************************************
logging模块的多个文件的读写
用到了logging模块的内置的一个方法,主要是两步走1、指定文件 2、通过比较判断是否写文件
import logging
# logging.basicConfig(filename='log_ss.log',format='%(asctime)s-%(name)s-%(levelname)s -%(module)s:%(message)s',datefmt='%Y-%m-%d %H:%M:%S %p',level=10)#日志文件路径 ; 操作时间 用户 操作类型 操作对象 内容;时间格式;level=10 表示logging 中编号大于等于10的方法才记录执行
# logging.log(10,'dayu10')

#写多个文件
file_1_1=logging.FileHandler('log1_11.log','a')
fmt=logging.Formatter(fmt='%(asctime)s-%(name)s-%(levelname)s -%(module)s:%(message)s')
file_1_1.setFormatter(fmt)

file_1_2=logging.FileHandler('log1_12.log','a')
fmt=logging.Formatter()
file_1_2.setFormatter(fmt)

logger1=logging.Logger('s1',level=10)#s1是一个标志,表示的是这个判断标准
logger1.addHandler(file_1_1)
logger1.addHandler(file_1_2)
logger1.critical('23444346634465434363')

输出结果——————————————————————————————————————————————————————————————
log1_11.log的内容
2018-05-08 13:54:17,478-s1-CRITICAL -testloging:23444346634465434363
log1_12.log的内容
23444346634465434363


 







posted on 2018-05-08 13:13  JuGooLar  阅读(130)  评论(0编辑  收藏  举报

导航