前言
今天就简单的对日志做个封装,实际工作中直接拿去用吧
方法1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
""" ------------------------------------ @time : 2019/5/22 8:12 @auth : linux超 @file : logfile.py @ide : pycharm @motto: real warriors,dare to face the bleak warning,dare to face the incisive error! ------------------------------------ """ import logging class log( object ): def __init__( self , name = __name__, path = 'mylog.log' , level = 'debug' ): self .__name = name self .__path = path self .__level = level self .__logger = logging.getlogger( self .__name) self .__logger.setlevel( self .__level) def __ini_handler( self ): """初始化handler""" stream_handler = logging.streamhandler() file_handler = logging.filehandler( self .__path, encoding = 'utf-8' ) return stream_handler, file_handler def __set_handler( self , stream_handler, file_handler, level = 'debug' ): """设置handler级别并添加到logger收集器""" stream_handler.setlevel(level) file_handler.setlevel(level) self .__logger.addhandler(stream_handler) self .__logger.addhandler(file_handler) def __set_formatter( self , stream_handler, file_handler): """设置日志输出格式""" formatter = logging.formatter( '%(asctime)s-%(name)s-%(filename)s-[line:%(lineno)d]' '-%(levelname)s-[日志信息]: %(message)s' , datefmt = '%a, %d %b %y %h:%m:%s' ) stream_handler.setformatter(formatter) file_handler.setformatter(formatter) def __close_handler( self , stream_handler, file_handler): """关闭handler""" stream_handler.close() file_handler.close() @property def logger( self ): """构造收集器,返回looger""" stream_handler, file_handler = self .__ini_handler() self .__set_handler(stream_handler, file_handler) self .__set_formatter(stream_handler, file_handler) self .__close_handler(stream_handler, file_handler) return self .__logger if __name__ = = '__main__' : log = log(__name__, 'file.log' ) logger = log.logger logger.debug( 'i am a debug message' ) logger.info( 'i am a info message' ) logger.warning( 'i am a warning message' ) logger.error( 'i am a error message' ) logger.critical( 'i am a critical message' ) |
初始化方法参数说明
name:自定义日志的名字, 默认是root, 但是我这里是使用调用文件的__name__ 作为默认名字
path:生成的日志的文件名
level:日志的级别,我这里把所有的级别都默认设置了level=debug
方法2
使用logging.fileconfig这个模块实现(不知道这个模块的找度娘恶补一下把)
1.使用配置文件构造日志配置信息
logger.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
[loggers] keys = root, example01, example02 [logger_root] level = debug handlers = hand01, hand02 [logger_example01] handlers = hand01, hand02 qualname = example01 propagate = 0 [logger_example02] handlers = hand01, hand03 qualname = example02 propagate = 0 [handlers] keys = hand01, hand02, hand03 [handler_hand01] class = streamhandler level = info formatter = form01 args = (sys.stdout, ) [handler_hand02] class = filehandler level = debug formatter = form01 args = ( 'log/test_case_log.log' , 'a' ) [handler_hand03] class = handlers.rotatingfilehandler level = info formatter = form01 args = ( 'log/test_case_log.log' , 'a' , 10 * 1024 * 1024 , 3 ) [formatters] keys = form01, form02 [formatter_form01] format = % (asctime)s - % (filename)s - [line: % (lineno)d] - % (levelname)s - [loginfomessage]: % (message)s datefmt = % a, % d % b % y % h: % m: % s [formatter_form02] format = % (name) - 12s : % (levelname) - 8s - [日志信息]: % (message)s datefmt = % a, % d % b % y % h: % m: % s |
封装python代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
""" ------------------------------------ @time : 2019/5/22 9:37 @auth : linux超 @file : log.py @ide : pycharm @motto: real warriors,dare to face the bleak warning,dare to face the incisive error! ------------------------------------ """ import logging from logging import config class mylog( object ): def __init__( self ): config.fileconfig( 'logger.ini' ) self .logger = logging.getlogger( 'example01' ) @property def my_logger( self ): return self .logger if __name__ = = '__main__' : log = mylog() log.my_logger.info( 'it is my test log message info' ) |
总结
两种方法各有好处吧
第一种代码很好理解思路清晰 , 但是不利于项目都维护,比如日志文件名,日志格式等无法修改,只能通过代码内部修改
第二种其实是使用config模块内部使用配置文件操作模块configparser做了封装, 用配置文件来构造自定义日志器,好处很明显,我们只要通过修改配置文件就能修改日志的格式,名字,级别等等一些设置,无需改动代码,而且使用很简单, 其实这种方法完全不需要封装一个python代码,哪个模块需要输出日志,直接调用config模块获得logger就可以了(就是上面文件的那几行代码),今天就到这吧......
以上所述是小编给大家介绍的python操作日志的封装方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/linuxchao/p/linuxchao-logger.html