Python logging模块封装与动态日志级别切换方案
本文介绍如何将Python内置logging模块封装为独立模块,并通过配置文件动态切换日志级别。核心方案包括读取ini配置、动态获取Level以及解决重复日志问题的handler清空策略,确保线上环境无需修改代码即可调整日志输出等级。
Python日志模块封装与动态切换方案
查阅大量资料后发现,现有教程内容高度同质化。为了进一步精简代码并实现解耦,本文旨在达成以下两个核心目标。
第一,将logging模块的初始化、配置及设置逻辑封装至独立模块中;
第二,支持根据配置动态切换logging.level。常见教程中日志级别是写死的,若线上已使用logging.info(msg),后续需切换为logging.debug(msg),则必须能够依据配置文件中的设置来动态调整日志级别。
实现该方案涉及两个主要文件:
logging_class:负责将logging模块的初始化、配置及设置逻辑封装至单一模块,并读取配置文件中的日志等级设置项。其他需要使用日志功能的模块只需import此模块即可;
applogconfig.ini:用于存储配置信息的文件。
日志类实现与配置文件解析
logging_class的具体实现如下:
import logging
import sys
import ConfigParser
def log_building(log_file):
try:
#set format
format_str=logging.Formatter("%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s")
#create stander output handler
crit_hand=logging.StreamHandler(sys.stderr)
crit_hand.setFormatter(format_str)
#create file handler
file_hand=logging.FileHandler(log_file,'a')
file_hand.setFormatter(format_str)
app_log=logging.getLogger(__name__)
app_log.addHandler(crit_hand)
app_log.addHandler(file_hand)
#必须设置,否则无法输出
app_log.setLevel(logging.NOTSET)
return app_log
except Exception as e:
logging.shutdown()
raise e
def config_file_get(fpath):
try:
cnf_dict={}
cfg=ConfigParser.SafeConfigParser()
cfg.read(fpath)
for section in cfg.sections():
#将ini中的item组合到字典中,key=section+_option
for item in cfg.items(section):
key= section+'_'+item[0]
value=item[1]
if cnf_dict.get(key,None)==None:
cnf_dict[key]=value
return cnf_dict
except Exception as e:
raise e
def log_level_get(level):
DEBUG_LEVEL={'CRITICAL':logging.CRITICAL,'ERROR':logging.ERROR,'WARNING':logging.WARNING,
'INFO':logging.INFO,'DEBUG':logging.DEBUG
}
try:
return DEBUG_LEVEL.get(level.upper())
except Exception as e:
raise e
applogconfig.ini的内容示例:
[log]
log_level=ERROR
dir=log
以下是unittest测试用例的内容:
import unittest
import logging_class
import os
import logging
class Test(unittest.TestCase):
cfg={}
def setUp(self):
print 'test begin'
self.cfg={}
def tearDown(self):
print 'test end'
def testlog_level_get(self):
currentWorkingPath = r'E:\Myworkspace\python\logging_module\logging_module'
ini_file=os.path.normpath(os.path.abspath(os.path.join(currentWorkingPath,'applogconfig.ini')))
self.cfg=logging_class.config_file_get(ini_file)
self.assertEqual(self.cfg['log_log_level'].upper(), 'ERROR', 'OK')
def testlog_level_set(self):
currentWorkingPath = r'E:\Myworkspace\python\logging_module\logging_module'
ini_file=os.path.normpath(os.path.abspath(os.path.join(currentWorkingPath,'applogconfig.ini')))
self.cfg=logging_class.config_file_get(ini_file)
#print self.cfg['log_log_level']
self.assertEqual(logging_class.log_level_get(self.cfg['log_log_level']), logging.ERROR, 'OK')
def testlog_building(self):
currentWorkingPath = r'E:\Myworkspace\python\logging_module\logging_module'
ini_file=os.path.normpath(os.path.abspath(os.path.join(currentWorkingPath,'applogconfig.ini')))
log_file=os.path.normpath(os.path.abspath(os.path.join(currentWorkingPath,'b.log')))
self.cfg=logging_class.config_file_get(ini_file)
#print self.cfg['log_log_level']
level=logging_class.log_level_get(self.cfg['log_log_level'])
log=logging_class.log_building(log_file)
log.log(level, 'dddds')
log.debug('msg')
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
测试输出结果如下:
Finding files... done.
Importing test modules ... done.
test begin
test end
test begin
test end
test begin
2016-12-15 17:59:04,059 logging_module_test.py[line:48] ERROR dddds
test end
----------------------------------------------------------------------
Ran 3 tests in 0.004s
解决重复日志输出的Handler策略
补充知识:一种可避免重复日志的logging封装方法。
在调试logging封装过程中发现,当已调用的logging封装函数被其他函数再次调用时,会出现重复日志。原因是不同位置创建了不同的handler,导致日志重复输出。可以使用一种“暴力”方法来解决此问题。
该暴力方式即在每次创建新对象时,清空logger.handlers列表。
以下是我常用的封装代码示例:
import logging
import time,os
'''
使用方法:
import mylog
log = mylog.Log().getlog()
log.debug("###")
'''
class Log():
def __init__(self,logger="mylog"):
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.DEBUG)
self.log_time = "\\"+time.strftime("%Y-%m-%d_%H_%M", time.localtime())+".log"
# 在进程路径创建log文件夹
# self.log_path = os.path.join(os.getcwd() + "\\log")
# 固定在mylog上一级创建
self.log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)) + "\\log")
if os.path.exists(self.log_path) and os.path.isdir(self.log_path):
pass
else:
os.makedirs(self.log_path)
self.log_name = os.path.join(self.log_path + self.log_time)
#因为多出调用logger会生成多个handlers,所以每次调用清空handler
self.logger.handlers = []
fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8')
formatter = logging.Formatter('[%(levelname)s][%(asctime)s] [%(filename)s]->[%(funcName)s] line:%(lineno)d ---> %(message)s')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
self.logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
self.logger.addHandler(ch)
fh.close()
def getlog(self):
return self.logger
if __name__ == "__main__":
log = Log().getlog()
log.debug("hello")
以上关于Python将logging模块封装成单独模块并实现动态切换Level的方式即为全部内容,希望能为大家提供参考,也希望大家多多支持本站。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









