#python日志:logging模块使用
logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级、日志保存路径、日志文件回滚等,记录运行时的过程
##日志事件级别
级别排序:CRITICAL>ERROR>WARNING>INFO>DEBUG
默认等级:WARNING
##Logger
###创建logger
```
importlogging
logger=logging.getLogger(__name__)#默认root
```
###设置日志记录的级别
```
level_relations={
'debug':logging.DEBUG,
'info':logging.INFO,
'warning':logging.WARNING,
'error':logging.ERROR,
'crit':logging.CRITICAL
}#日志级别关系映射
logger.setLevel(level_relations['debug'])
```
###将日志内容传递到相关联的handlers中
```
logger.addHandler()
logger.removeHandler()
```
###Handlers处理器
###输出到控制台
```
importlogging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s%(filename)s%(levelname)s%(message)s',
datefmt='%a%d%b%Y%H:%M:%S'
)
logging.getLogger(__name__)
logging.info('info')
logging.debug('debug')
logging.warning('warning')
```
###控制台输出参数
###将日志同时输出到屏幕和日志文件
```
importlogging.handlers
importlogging.config
fromsysimport_getframeasget
importos
defcreate_file(filename):
ifnotos.path.exists(filename):
withopen(filename,'a',encoding='utf-8')asf:
pass
classLogger:
level_relations={
'debug':logging.DEBUG,
'info':logging.INFO,
'warning':logging.WARNING,
'error':logging.ERROR,
'crit':logging.CRITICAL
}#日志级别关系映射
#第一种创建方式
def__init__(self,name,filename):
logger=logging.getLogger(name)#创建日志对象
logger.setLevel(Logger.level_relations.get('debug'))#设置日志等级
fmt=f'[{name}P-%(process)sT-%(thread)s%(funcName)s%(asctime)s%(message)s'
formatter=logging.Formatter(fmt,'%Y-%m-%d%H:%M:%S')#时间格式
create_file(filename)
#设置屏幕显示
consloe=logging.StreamHandler()#控制台输出日志
consloe.setLevel(Logger.level_relations.get('info'))#设置控制台输出日志级别
consloe.setFormatter(formatter)#设置控制台打印格式
#设置日志保存文件中
file_handler=logging.handlers.TimedRotatingFileHandler(filename,when='s',interval=2,backupCount=2,encoding='utf-8')#这里我设置的是2秒产生1个日志文件,保存近两天的日志
#when:h(时)/m(分)/s(秒)/d(天)/midnight(凌晨)等等,interval:间隔时间,backupCount=0保存几天的日志
file_handler.setLevel(Logger.level_relations.get('info'))#设置输出到日志文件等级
file_handler.setFormatter(formatter)#设置写到日志文件格式
file_handler.suffix='%Y-%m-%d_%M_%S'#日志文件后缀
#将日志内容传递到相关联的handlers中
logger.addHandler(consloe)
logger.addHandler(file_handler)
self.log=logger
definfo(self,message):
#调用该函数的函数文件名,调用该函数的函数名字,调用该函数的行号
self.log.info('info{}{}{}{}'.format(get(1).f_code.co_filename.split('/')[-1],get(1).f_code.co_name,get(1).f_lineno,message))
deferror(self,message):
self.log.info('info{}{}{}{}'.format(get(1).f_code.co_filename.split('/')[-1],get(1).f_code.co_name,get(1).f_lineno,message))
```
###sys._getframe
```
importsys
sys._getframe().f_code.co_filename#当前文件名,可以通过__file__获得
sys._getframe(0).f_code.co_name#当前函数名
sys._getframe(1).f_code.co_name#调用该函数的函数名字,如果没有被调用,则返回<module>
sys._getframe(0).f_lineno#当前函数的行号
sys._getframe(1).f_lineno#调用该函数的行号
```
###logging.basicConfig(**kwargs)
```
#filename为空时默认屏幕输出,反之写入文件
#logging.basicConfig(level=logging.DEBUG,
#format='%(asctime)s%(filename)s%(levelname)s%(message)s',
#datefmt='%a%d%b%Y%H:%M:%S',
#filename='my.log',
#filemode='a'
#)
#即写入文件,又在屏幕输出
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s%(filename)s%(levelname)s%(message)s',
datefmt='%a%d%b%Y%H:%M:%S',
handlers=[logging.StreamHandler(),logging.FileHandler('my.log',encoding='utf-8')],
)
```
###dictConfig
```
#通过YAML文件配置
importyaml
withopen('common_config.yaml','r',encoding='utf-8')asf:
data=yaml.load(f,yaml.Loader)
config_d=data['log_config']
#通过json
config_d={
"version":1,
"disable_existing_loggers":True,
"formatters":{
"standrd":{
"format":"%(asctime)s-%(name)s-%(levelname)s-%(message)s",
"datefmt":"%Y-%m-%d%H:%M:%S"
}
},
"handlers":{
"console":{
"class":"logging.StreamHandler",
"level":"DEBUG",
"formatter":"standrd",
"stream":"ext://sys.stdout"
},
"info_file_handler":{
"class":"logging.handlers.TimedRotatingFileHandler",
"level":"INFO",
"formatter":"standrd",
"filename":"C:/Users/huangxiangyi/Desktop/flask_ka/hxy_test/my.log",
"backupCount":20,
"encoding":"utf8"
},
'loggers':{
'main':{
'level':'INFO',
'propagate':False,
'handlers':['console','info_file_handler']
}
},
},
"root":{
"level":"INFO",
"handlers":[
"console",
"info_file_handler"
]
}
}
logging.config.dictConfig(config_d)
logging.getLogger('main')
```
###common_config.yaml文件
```
#日志配置,根据自己需要自行添加
url:&urlC:/Users/huangxiangyi/Desktop/flask_ka/hxy_test/
log_config:
version:1
disable_existing_loggers:true
formatters:
standrd:
format:"%(asctime)s-%(name)s-%(levelname)s-%(message)s"
datefmt:"%Y-%m-%d%H:%M:%S"
handlers:
console:
class:logging.StreamHandler
level:DEBUG
formatter:standrd
stream:ext://sys.stdout
info_file_handler:
class:logging.handlers.TimedRotatingFileHandler
level:INFO
formatter:standrd
filename:!!python/object/apply:os.path.join[*url,my.log]
backupCount:20
encoding:utf8
loggers:
main:
level:INFO
propagate:false
handlers:[console,info_file_handler]
root:
level:INFO
handlers:[console,info_file_handler]
```