zoukankan      html  css  js  c++  java
  • 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()
    -----------------------------------------
     
  • 相关阅读:
    .NET框架设计—常被忽视的C#设计技巧
    判断网络是否链接
    ADO.NET入门教程(五) 细说数据库连接池
    爬虫selenium中截图
    爬虫极滑块验证思路
    Linux 磁盘分区、挂载
    linux中crontab任务调度
    第30课 操作符重载的概念
    第29课 类中的函数重载
    第28课 友元的尴尬能力
  • 原文地址:https://www.cnblogs.com/niaocaizhou/p/11063707.html
Copyright © 2011-2022 走看看