zoukankan      html  css  js  c++  java
  • 用python 装饰器打log

    # coding=utf-8

      
    from time import time
    def logged(when):
        def log(f,*args,**kargs):
            print("called: function:%s,args:%r,kargs:%r"%(f,args,kargs))
        def pre_logged(f):
            def wrapper(*args,**kargs):
                log(f,*args,**kargs)
                return f(*args,**kargs)
        def post_logged(f):
            def wrapped(*args,**kargs):
                now=time()
                try:
                    return f(*args,**kargs)
                finally:
                    log(f,*args,**kargs)
                    print("time delta:%s"%(time()-now))
            return wrapped
        try:
            #从这里开始调用
            return{"pre":pre_logged,"post":post_logged}[when]
        except Exception as e:
            print(e)
      
    @logged("post")
    def hello(name):
        print("hello",name)
    @logged("post")
    def test(a,b=1):
        print(a+b)
      
    hello("world")
    test(1,2)
  • 相关阅读:
    mysql把查询结果集插入到表理
    js遍历json数据
    php事务回滚
    win10定时执行php脚本
    php输出json的内容
    图像的几个基本概念
    linux系统编程之I/O内核数据结构
    linux系统编程之错误处理
    深拷贝和浅拷贝
    mysql用户的创建
  • 原文地址:https://www.cnblogs.com/chenjingyi/p/5742051.html
Copyright © 2011-2022 走看看