Python的日志,看上去啰啰嗦嗦的。请大神写了个通俗易懂简单方便通用的日志:
import logging# 配置日志记录级别和输出方式
def log_exceptions(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: logging.exception(f"Exception in function {func.__name__}: {str(e)}")
logging.basicConfig(level=logging.DEBUG, filename='mylog.log', filemode='w', format='%(asctime)s - %(levelname)s - %(message)s')
raise # 抛出异常,以保持原始行为
return wrapper @log_exceptions def demo_function1(): # 函数1的逻辑代码
print(1 / 0)
@log_exceptions def demo_function2(): # 函数2的逻辑代码 # 更多的被装饰函数...
标签:logging,log,Python,func,日志,def From: https://www.cnblogs.com/usegear/p/17541140.html