首页 > 编程语言 > 详细

Python---进阶---logging---装饰器打印日志2

时间:2019-06-21 12:24:32      阅读:178      评论:0      收藏:0      [点我收藏+]

### logging

- logging.debug

- logging.info

- logging.warning

- logging.error

- logging.critical

--------------------------------

import   logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("this is a debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
--------------------------------
### 装饰器
- 使用装饰器,打印函数执行的时间
----------------------------------
import logging
LOG_FORMART = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(format = LOG_FORMART)
def log(func):
    def wrapper(*arg, **kw):
        logging.error("this is info message")
        return func(*arg, **kw)
    return wrapper

@log
def test():
    print("test done")
   
test()
------------------------------------------
#####   使用装饰器,根据不同的函数,传入的日志不相同
LOG_FORMART = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(format = LOG_FORMART, filename = "my.log")

def log(text):
    def decorator(func):
        def wrapper(*arg, **kw):
            logging.error(text)
            return func(*arg, **kw)
        return wrapper
    return decorator
@log("test done")
def test():
    print("test done")
   
@log("main log")
def main():
    print("main done")
test()
main()
-----------------------------------------
 

Python---进阶---logging---装饰器打印日志2

原文:https://www.cnblogs.com/niaocaizhou/p/11063707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!