zoukankan      html  css  js  c++  java
  • 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"
    }
  • 相关阅读:
    IPV6地址中的%号什么意思
    比较分析C++、Java、Python、R语言的面向对象特征,这些特征如何实现的?有什么相同点?
    Linux 查看本机串口方法
    非对称加密与GPG/PGP
    bootstrap下拉三角
    bootstrap文本背景色
    bootstrap文本颜色
    bootstrap栅格系统响应式
    软件测试(一) 软件测试的阶段划分
    YoTube 视频如何下载
  • 原文地址:https://www.cnblogs.com/chenadong/p/9616058.html
Copyright © 2011-2022 走看看