Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用decorator从我的业务逻辑到日志文件生成值?_Python_Decorator_Aop_Python Decorators - Fatal编程技术网

Python 如何使用decorator从我的业务逻辑到日志文件生成值?

Python 如何使用decorator从我的业务逻辑到日志文件生成值?,python,decorator,aop,python-decorators,Python,Decorator,Aop,Python Decorators,我想从日志文件中的业务逻辑生成值,但我不确定如何进行 import time import logging logging.basicConfig(filename='new_example.log',level=logging.DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') logging

我想从日志文件中的业务逻辑生成值,但我不确定如何进行

import time
import logging

logging.basicConfig(filename='new_example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('Please check')

def log_check(func):
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        logging.info(func.__name__ + "took" + str ((end - start) * 1000) + "mili seconds")
        #logging.info('this is what {}'.format(func.__name__)+ func["user"] + " : " + func["passw"])
        print (func.__name__ + "took" + str ((end - start) * 1000) + "mili seconds")
        return result
    return wrapper()

@log_check
def login():
    user = raw_input("Username: ")
    passw = raw_input("Password: ")
    return {"user":user, "passw":passw}
我希望在日志文件中生成我为用户和密码提供的值。 我该怎么做? 我正在获取信息:root:logintook1964.99991417mili seconds作为我在日志文件中的记录,但我希望作为用户和密码输入的值也会随之生成。

您应该使用result[user]和result[passw],而不是func[user]和func[passw],因为这些值在函数的返回值中,不在函数本身中:

def log_check(func):
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        logging.info(func.__name__ + "took" + str ((end - start) * 1000) + "mili seconds")
        logging.info('this is what {}'.format(func.__name__)+ result["user"] + " : " + result["passw"])
        print (func.__name__ + "took" + str ((end - start) * 1000) + "mili seconds")
        return result
    return wrapper()

如果泛型decorator对修饰后的函数返回值有任何假设,那么它就不能工作-在这里,您所能做的就是记录结果,不管结果是什么,即:

def log_check(func):
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        # proper use of `logger` methods: dont build the whole string, just 
        # pass a format string and the matching arguments)
        logging.info("%s took %s milliseconds", func.__name__,  (end - start) * 1000)
        # if you hope to use this decorator on anything else than
        # your login function, don't assume _anything_ about what
        # the decorated func might return
        logging.info('func %s returned %s', func.__name__, result)
        return result


    # You want to return the wrapper function, not
    # to call it !!!
    # return wrapper()
    return wrapper
这就是说,您不应该将不相关的问题混为一谈,即利用数据计时函数执行和函数返回的特定于域的数据等。我会亲自从函数本身中进行特定于域的日志记录。而且我不会记录用户名/密码对之类的敏感数据。请提醒我,如果你有泄露此类信息的习惯,请不要使用你的程序。

这只适用于登录功能-在任何不返回带有用户和密码密钥的dict的程序上使用此装饰程序,它将崩溃。使装饰师的使用变得毫无用处,不是吗?