• python logging模块


    先来一个简单的例子:

    参考文档:https://blog.csdn.net/z_johnny/article/details/50740528

    import logging
    
    logging.debug("debug_messages")     # debug信息
    logging.info("info_messages")
    logging.warning("warning_messages")
    logging.error("error_messages")
    logging.critical("critical_messages")  # 严重错误
    
    >>>
    WARNING:root:warning_messages
    ERROR:root:error_messages
    CRITICAL:root:critical_messages
    # 默认只输出warning以上严重级别的日志,包括warning


    # 设置日志等级使用:
    logging.basicConfig(level=logging.DEBUG)

    设置输出格式,并且将日志输出到指定的文件:

    import logging
    import time
    logging.basicConfig(level=logging.DEBUG,
    format="[%(asctime)s] [%(filename)s] [line:%(lineno)d] %(levelname)s %(message)s)",
    filename="tem.log",
    filemode="a")


    def count():
    logging.debug("test")
    time.sleep(5)

    count()

    >>>
    [2018-12-21 11:16:58,058] [test013.py] [line:16] DEBUG test)

    %(name)s Logger的名字
    %(levelno)s 数字形式的日志级别
    %(levelname)s 文本形式的日志级别
    %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
    %(filename)s 调用日志输出函数的模块的文件名
    %(module)s 调用日志输出函数的模块名
    %(funcName)s 调用日志输出函数的函数名
    %(lineno)d 调用日志输出函数的语句所在的代码行
    %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
    %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
    %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
    %(thread)d 线程ID。可能没有
    %(threadName)s 线程名。可能没有
    %(process)d 进程ID。可能没有
    %(message)s 用户输出的消息

    另外,baseconfig里面的配置,是可以写在字典里面的,以**config的方式传给类或者对象;

    config = {
    "level": logging.DEBUG,
    "filename": "tem.log",
    "filemode": "a"
    }
  • 相关阅读:
    手动实现 SpringMVC
    2014年9月9日 高级命令command的使用(上)
    2014年8月29日 透视图补充及视图开头
    2014年8月24日 菜单 工具条 右键菜单(上下文菜单)
    2014年8月14日 透视图
    2014年8月8日
    2014年8月1日
    关于EMF中从schema到ecore转变中的默认处理问题
    JAVA一些常用的时间操作
    echarts基本使用
  • 原文地址:https://www.cnblogs.com/chenadong/p/9616058.html
Copyright © 2020-2023  润新知