log装饰器和简单日志集成模板

  1 # coding:utf-8
  2 
  3 
  4 class log(object):
  5 
  6     def __init__(self, **kwargs):
  7         '''
  8         <logFm>,<funcLog>:日志输出格式
  9         #外层日志格式<logFm>[时间,日志名<logName>,日志级别,日志信息]
 10         #内层日志格式<funcLog>[函数名称,函数参数,函数位置]
 11         <logName>,:输出日志名称
 12         <conLevel>:日志显示级别
 13         <fileLevel>:日志文件记录级别
 14         ----------------------------------------------------------------------------------------------------------------
 15         <fileFm><fileLevel>,<filePath>,<logName>[日志记录格式,日志级别,日志路径,日志名称]
 16         #日志保存文件默认关闭,默认路径./log.log,默认级别DEBUG#
 17         <funcLog>修改涉及函数操作:
 18         def funclogfm(self, func, *args, **kwargs):
 19             if self.funcLogFm == None:
 20                 self.funcLogFm = self.funcLog % (
 21                     func.__name__, args, kwargs, func.__code__.co_firstlineno, func.__code__.co_filename)
 22             return self.funcLogFm
 23         ----------------------------------------------------------------------------------------------------------------
 24         '''
 25         from functools import wraps
 26         import logging, sys, inspect
 27         if 'logFm' in kwargs.keys():
 28             self.logFm = kwargs['logFm']
 29         else:
 30             self.logFm = '%(asctime)s - %(name)s - %(levelname)s - %(message)s;'
 31         if 'funcLog' in kwargs.keys():
 32             self.funcLog = kwargs['funcLog']
 33         else:
 34             self.funcLog = 'line:%s - funcName:<%s> - path:%s - parm:[%s, %s]'
 35         if 'conLevel' in kwargs.keys():
 36             for le in ['INFO', 'WARNING']:
 37                 if le == kwargs['conLevel']:
 38                     s = 'logging.' + le
 39                     ConLevel = eval(s)
 40                     logging.basicConfig(level=ConLevel, format=self.logFm)
 41         else:
 42             logging.basicConfig(level=logging.DEBUG, format=self.logFm)
 43         if 'logName' in kwargs.keys():
 44             self.logger = logging.getLogger(kwargs['logName'])
 45         else:
 46             self.logger = logging.getLogger('log')
 47         if 'filePath' in kwargs.keys():
 48             self.handler = logging.FileHandler(kwargs['filePath'])
 49         else:
 50             self.handler = logging.FileHandler("log.log")
 51         if 'fileFm' in kwargs.keys():
 52             formatter = logging.Formatter(kwargs['fileFm'])
 53         else:
 54             formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')
 55         if 'fileLevel' in kwargs.keys():
 56             for le in ['INFO', 'WARNING']:
 57                 if le == kwargs['fileLevel']:
 58                     s = 'logging.' + le
 59                     FileLevel = eval(s)
 60                     self.handler.setLevel(FileLevel)
 61         else:
 62             self.handler.setLevel(logging.INFO)
 63         self.handler.setFormatter(formatter)
 64         self.wraps = wraps
 65         self.funcLogFm = None
 66         self.logger.addHandler(self.handler)
 67         self.sys = sys
 68         self.inspect = inspect
 69 
 70     def funclogfm(self, func, *args, **kwargs):
 71         lineno = func.__code__.co_firstlineno
 72         who = func.__name__
 73         path = func.__code__.co_filename
 74         self.funcLogFm = self.funcLog % (lineno, who, path, args, kwargs,)
 75         return self.funcLogFm
 76 
 77     def DEBUG(self, func):
 78         '''日志装饰器'''
 79 
 80         @self.wraps(func)
 81         def tmp(*args, **kwargs):
 82             self.logger.debug(self.funclogfm(func, args, kwargs))
 83             try:
 84                 return func(*args, **kwargs)
 85             except:
 86                 err = "There was an exception in  "
 87                 err += func.__name__
 88                 self.logger.exception(err)
 89 
 90         return tmp
 91 
 92     def INFO(self, func):
 93         '''日志装饰器'''
 94 
 95         @self.wraps(func)
 96         def tmp(*args, **kwargs):
 97             self.logger.info(self.funclogfm(func, args, kwargs))
 98             try:
 99                 return func(*args, **kwargs)
100             except:
101                 err = "There was an exception in  "
102                 err += func.__name__
103                 self.logger.exception(err)
104 
105         return tmp
106 
107     def WARNING(self, func):
108         '''日志装饰器'''
109 
110         @self.wraps(func)
111         def tmp(*args, **kwargs):
112             self.logger.warning(self.funclogfm(func, args, kwargs))
113             try:
114                 return func(*args, **kwargs)
115             except:
116                 err = "There was an exception in  "
117                 err += func.__name__
118                 self.logger.exception(err)
119 
120         return tmp
121 
122     def debug(self, message):
123         import inspect
124         '''返回程序中的当前行号:inspect.currentframe().f_back.f_lineno'''
125         lineno = inspect.currentframe().f_back.f_lineno
126         who = inspect.getframeinfo(inspect.currentframe().f_back)
127         msg = 'line:%s - funcName:<%s> - path:%s - parm:%s' % (lineno, who[2], who[0], message)
128         return self.logger.debug(msg)
129 
130     def info(self, message):
131         import inspect
132         '''返回程序中的当前行号:inspect.currentframe().f_back.f_lineno'''
133         lineno = inspect.currentframe().f_back.f_lineno
134         who = inspect.getframeinfo(inspect.currentframe().f_back)
135         msg = 'line:%s - funcName:<%s> - path:%s - parm:%s' % (lineno, who[2], who[0], message)
136         return self.logger.info(msg)
137 
138     def warning(self, message):
139         import inspect
140         '''返回程序中的当前行号:inspect.currentframe().f_back.f_lineno'''
141         lineno = inspect.currentframe().f_back.f_lineno
142         who = inspect.getframeinfo(inspect.currentframe().f_back)
143         msg = 'line:%s - funcName:<%s> - path:%s - parm:%s' % (lineno, who[2], who[0], message)
144         return self.logger.warning(msg)

 

posted @ 2018-07-28 19:06  LLSix  阅读(771)  评论(0编辑  收藏  举报