Python: How to Create an Exception Logging Decorator
exception_decor.py
import functools
import logging
def create_logger():
"""
Creates a logging object and returns it
"""
logger = logging.getLogger("example_logger")
logger.setLevel(logging.INFO)
# create the logging file handler
fh = logging.FileHandler("/path/to/test.log")
fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(fmt)
fh.setFormatter(formatter)
# add handler to logger object
logger.addHandler(fh)
return logger
def exception(function):
"""
A decorator that wraps the passed in function and logs
exceptions should one occur
"""
@functools.wraps(function)
def wrapper(*args, **kwargs):
logger = create_logger()
try:
return function(*args, **kwargs)
except:
# log the exception
err = "There was an exception in "
err += function.__name__
logger.exception(err)
# re-raise the exception
raise
return wrapper
在该代码中,有两个函数。第一个函数创建了日志对象并返回该日志对象。第二个函数是我们的装饰器函数。我们在一个try/except
中封装传递的函数,当logger
中发生任何异常的时候,进行日志记录。并且我还记录了当异常发生时的函数名称。
现在让我们测试下该装饰器。
from exception_decor import exception
@exception
def zero_divide():
1 / 0
if __name__ == '__main__':
zero_divide()
运行以上测试代码后,会出现以下错误日志:
2016-06-09 08:26:50,874 - example_logger - ERROR - There was an exception in zero_divide
Traceback (most recent call last):
File "/home/mike/exception_decor.py", line 29, in wrapper
return function(*args, **kwargs)
File "/home/mike/test_exceptions.py", line 5, in zero_divide
1 / 0
ZeroDivisionError: integer division or modulo by zero
传递一个 logger
到装饰器
# exception_logger.py
import logging
def create_logger():
"""
Creates a logging object and returns it
"""
logger = logging.getLogger("example_logger")
logger.setLevel(logging.INFO)
# create the logging file handler
fh = logging.FileHandler(r"/path/to/test.log")
fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(fmt)
fh.setFormatter(formatter)
# add handler to logger object
logger.addHandler(fh)
return logger
logger = create_logger()
现在修改前面出现的装饰器,以便可以接受 logger
作为参数。
# exception_decor.py
import functools
def exception(logger):
"""
A decorator that wraps the passed in function and logs
exceptions should one occur
@param logger: The logging object
"""
def decorator(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
# log the exception
err = "There was an exception in "
err += func.__name__
logger.exception(err)
# re-raise the exception
raise
return wrapper
return decorator
最后修改测试脚本:
from exception_decor import exception
from exception_logger import logger
@exception(logger)
def zero_divide():
1 / 0
if __name__ == '__main__':
zero_divide()