zoukankan      html  css  js  c++  java
  • python模块--logging

    一、logging模块的简单应用

    1 import logging
    2 
    3 logging.debug('debug message')
    4 logging.info('ingo message')
    5 logging.warning('warning message')
    6 logging.error('error message')
    7 logging.critical('critical message')

    输出为:

    WARNING:root:warning message
    ERROR:root:error message
    CRITICAL:root:critical message

    可见,默认情况下,python的logging模块将日志打印到了标准输出中,且只显示了大于等于warning级别的日志,这说明默认的日志级别设置为warning(日志界别等级critical > error > warning > info > debug > notset ),默认的日志格式为日志级别:logger名称:用户输出消息。

    二、灵活配置日志级别,日志格式,输出位置

     1 import logging
     2 logging.basicConfig(
     3     level = logging.DEBUG,    #修改了级别,从原来的默认warning,改成了debug
     4     filename="logger.log",#不在外面显示,只在内部显示
     5     filemode='w',
     6     # format="%(asctime)s %(filename)s[line:%(lineno)d] %(message)s",
     7 )
     8 
     9 logging.debug('debug message')
    10 logging.info('ingo message')
    11 logging.warning('warning message')
    12 logging.error('error message')
    13 logging.critical('critical message')

    输出

    DEBUG:root:debug message
    INFO:root:ingo message
    WARNING:root:warning message
    ERROR:root:error message
    CRITICAL:root:critical message
    可见在logging.basicconfig()函数中,可通过具体参数来更改logging模块默认行为,可用参数有filename:用指定的文件名创建filed handler,这样日志会被存在指定的文件中。

    filemode:文件安打开方式,在指定了filename时使用这个参数,默认值为”a"还可以指定为"w".
    format:指定handler使用的日志显示格式

    datefmt:指定日期格式。
    level:设置rootlogger的人日志级别。
    stream:用指定的stream创建sys.stderr。若同时列出了filename和stream两个参数,
    则stream参数会被忽略。

    format参数中可能用的格式化串:
    %(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-02-03 12:34:10,234”。逗号后面的时毫秒
    %(thread)d 县城ID,可能没有
    %(process)d 进程ID,可能没有
    %(threadName)s 线程名,可能没有
    %(message)s 用户输出的消息

    【待续】

  • 相关阅读:
    数据仓库建模方法初步
    金融行业信用评级主题和概念清单
    数据挖掘标准规范之CRISP-DM基础
    HBase与Zookeeper数据结构查询
    R语言数据挖掘相关包总结-转帖
    R语言学习路线图-转帖
    在IIS上部署Asp.Net Core 2.2.0
    [转]Winform打包工具SetupFactory 9 的使用
    SQLite带参数处理方法
    Web.Config中配置字符串含引号的处理
  • 原文地址:https://www.cnblogs.com/jianguo221/p/9010029.html
Copyright © 2011-2022 走看看