【Python】logging结合decorator模式实优化日志输出
来源:互联网 发布:韩国扫码查真伪软件 编辑:程序博客网 时间:2024/05/20 10:23
python内置的loging模块非常简便易用, 很适合程序运行日志的输出。
而结合python的装饰器模式,则可实现简明实用的代码。测试代码如下所示:
#! /usr/bin/env python2.7# -*- encoding: utf-8 -*-import logginglogging.basicConfig(format='[%(asctime)s] %(message)s', level=logging.INFO)def time_recorder(func): """装饰器, 用在func方法执行前后, 增加运行信息""" def wrapper(): logging.info("Begin to execute function: %s" % func.__name__) func() logging.info("Finish executing function: %s" % func.__name__) return wrapper@time_recorderdef first_func(): print "I'm first_function. I'm doing something..."@time_recorderdef second_func(): print "I'm second_function. I'm doing something..."if __name__ == "__main__": first_func() second_func()
运行并得到输出:
[2014-04-01 18:02:13,724] Begin to execute function: first_funcI'm first_function. I'm doing something...[2014-04-01 18:02:13,725] Finish executing function: first_func[2014-04-01 18:02:13,725] Begin to execute function: second_funcI'm second_function. I'm doing something...[2014-04-01 18:02:13,725] Finish executing function: second_func
7 0
- 【Python】logging结合decorator模式实优化日志输出
- Python日志输出——logging模块
- python日志输出—logging配置文件
- Python日志输出——logging模块
- python日志输出—logging配置文件
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- python logging动态变更输出日志文件名
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- python日志输出—logging配置文件
- Python日志输出——logging模块
- Python日志输出——logging模块
- 项目-系统接口实现 如何做好接口
- Raysoft.Framework.Date_Time.DateConvertHelper
- socket高并发
- android基本架构
- Azure 网站的新增功能:可配置的环境变量
- 【Python】logging结合decorator模式实优化日志输出
- 毕业论文的点点滴滴(一)
- NYOJ244&南阳理工oj_244
- VMWare安装liunx教程(ubuntu)
- Python入门教程-02 变量
- Java虚拟机详解
- PHP视频教程
- 黑马程序员----.net 高级技术之程序集与反射的使用
- 惊 百度site域名只显示主域名 网站快照倒退到1995年