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


    #### logging

    - logging.debug

    - logging.info

    - logging.warning

    - logging.error

    - logging.critical

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

    import logging
    LOG_FORMART = "%(asctime)s - %(levelmane)s - %(message)s"
    logging.basicConfig(level=logging.DEBUG, format=LOG_FORMART, filename="my.log")
    logging.debug("this is  debug")
    logging.info("this is  info")
    logging.warning("this is  warning")
    logging.error("this is  error")
    logging.critical("this is  critical")
    ----------------------------------
    二、装饰器
    -  使用装饰器,打印函数执行的时间
    ----------------------------------
    #####  使用装饰器,根据不同的函数,传入的日志不相同
    #def log(func):
    #    def wrapper(*arg, **kv):
    #        logging.error("this is info message")
    #        return func(*arg, **kv)
    #    return wrapper
    def log(text):
        def decorator(func):
            def wrapper(*arg, **kv):
                logging.error(text)
                return func(*arg, **kv)
            return wrapper
        return decorator
    @log("test donw")
    def test():
        print("test done")
       
    @log("main done")
    def main():
        print("main done")
       
    test()
    main()
    -----------------------------
    三、
     
  • 相关阅读:
    shell---telnet shell实现
    设计模式-建造者模式
    关于Http2
    转载Resharper使用
    设计模式-原型模式
    设计模式-代理模式
    设计模式-装饰器模式
    设计模式-简单工厂和策略模式
    C#直接发送打印机命令到打印机及ZPL常用打印命令
    C#打印机操作类
  • 原文地址:https://www.cnblogs.com/niaocaizhou/p/11061060.html
Copyright © 2020-2023  润新知