yaml文件:
version: 1
disable_existing_loggers: false
formatters:
default_fmt:
format: '%(asctime)s %(name)s %(filename)s(%(lineno)s) %(levelname)s: %(message)s'
datefmt: '%Y-%m-%d %H:%M:%S'
## 配置handlers 输出格式
handlers:
# 多个handlers
ipe_info:
class: logging.FileHandler
level: INFO
formatter: default_fmt
filename: ../logs/info.log
ipe_error: # handlers 键值对的键
class: logging.FileHandler
level: INFO # 写文件的信息登记
formatter: default_fmt # 写入文件的输出格式
filename: ../logs/error.log #日志文件路径
loggers:
ipe_info:
level: INFO
handlers: [ipe_info]
propagate: no
ipe_error:
level: ERROR # 信息等级
handlers: [ipe_error] # 指定哪个handlers
propagate: no # 是否往下一级抛出异常日志
root:
level: INFO # 没有指定handler时 默认使用root 作为handler
handlers: [ipe_info, ipe_error]
代码:
# coding: utf-8
import os
import sys
import yaml
from logging import config, getLogger
from ipe_config import ROOT_PATH
BASEPATH = ROOT_PATH + '/util'
def logConfig():
with open(os.path.join(BASEPATH, 'logging.yaml')) as fp:
dicts = yaml.safe_load(fp)
return dicts
# def test_log():
# log_tmpl = 'this is %s log'
# lg.debug(log_tmpl, 'debug')
# lg.info(log_tmpl, 'info')
config.dictConfig(logConfig())
lg = getLogger('ipe_info')
lge = getLogger('ipe_error')
if __name__ == '__main__':
# test_log()
pass
# print(logConfig())
lg.warning('rrrr')
lge.error('gg')
网友评论