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


    ### 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()
    -----------------------------------------
     
  • 相关阅读:
    软件工程作业3.28
    毕业论文管理系统建模图
    软件工程建模图作业
    酒店管理系统
    闪屏和功能引导页面代码编写
    Android算法编程代码
    3.28软件工程作业
    毕业论文管理系统
    图书管理系统建模图
    酒店预订系统故事
  • 原文地址:https://www.cnblogs.com/niaocaizhou/p/11063707.html
Copyright © 2020-2023  润新知