zoukankan      html  css  js  c++  java
  • logging.config模块---使用配置文件管理logger

    logging配置文件

    一.使用到的模块:

    logging.config

    官方文档:

    https://docs.python.org/3/library/logging.config.html

    非官方文档(中文版):

    http://python.usyiyi.cn/translate/python_352/library/logging.config.html

    1. 配置文件长什么样:

      官方: https://docs.python.org/3/library/logging.config.html#logging-config-fileformat

       

      fileConfig()理解的配置文件格式基于configparser的功能。文件必需包含名为[loggers], [handlers] 和 [formatters]的节:如下表

    [loggers]

    keys=root,log02,log03,log04,log05,log06,log07

     

    [handlers]

    keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09

     

    [formatters]

    keys=form01,form02,form03,form04,form05,form06,form07,form08,form09

    1:loggers节,以及字节的设置

    根logger必需指定级别handler列表。根logger节的例子给定如下。

    [logger_root]

    level=NOTSET

    handlers=hand01

    level可以是DEBUG, INFO, WARNING, ERROR, CRITICALNOTSET其中之一。NOTSET表示所有的消息都要记录,这只对根logger有效。级别值使用eval()在logging包空间演算得到。

    handlers是逗号分隔的handler名字的列表,这些必需出现在[handlers]节中。这些名字必需出现在[handlers]节中,且在配置文件中有对应的节。

     

    对于非根logger的logger来说,需要一些额外的信息。如下例所示。

    [logger_parser]

    level=DEBUG

    handlers=hand01

    propagate=1

    qualname=compiler.parser

    level 和 handlers 的解读和根logger的一样,如果非根logger的级别为NOTSET,系统参考高层次的logger来决定logger的有效级别。

    propagate为1表示将消息传递给高层次logger的handler,为0表示不传播。

    qualname是logger在层次中的名字,应用通过该名字得到logger。

     

    2:handler节,以及字节的设置

    [handler_hand01]

    class=StreamHandler

    level=NOTSET

    formatter=form01

    args=(sys.stdout,)

    class条目指示处理程序的类(如通过eval() 在logging包的命名空间)。

     

        The level is interpreted as for loggers, and NOTSET is taken to mean 'log everything'.

    formatter条目表示此处理程序的格式化程序的关键名称。如果为空,logging._defaultFormatter则使用默认格式化程序()。如果指定了一个名称,它必须出现在该[formatters]部分,并在配置文件中有相应的部分。

     

    args条目eval()在logging 程序包命名空间的上下文中使用时,是处理程序类的构造函数的参数列表。请参阅相关处理程序的构造函数,或下面的示例,了解如何构建典型的条目。

    [handler_hand02]

    class=FileHandler

    level=DEBUG

    formatter=form02

    args=('python.log', 'w')

     

    [handler_hand03]

    class=handlers.SocketHandler

    level=INFO

    formatter=form03

    args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)

     

    3:formatters节,以及字节的设置

    指定了格式化配置的节典型如下

    [formatter_form01]

    format=F1 %(asctime)s %(levelname)s %(message)s

    datefmt=

    class=logging.Formatter

    format是整体的格式化字符串,

    datefmt是strftime()兼容的日期/时间格式化字符串。如果为空,使用ISO8601格式,基本上等同于格式'%Y-%m-%d %H:%M:%S'。ISO8601格式在上述格式的末尾指明了毫秒,中间有个逗号。ISO8601格式的例子:2003-01-23 00:29:50,411。

    class可选。它表示formatter类的名字(带点的模块和类名)。实例化Formatter子类时该选项很有用。Formatter子类可以用来表示详细/概要的异常回溯。

     

     

     

    1. 配置文件写好之后怎么用?

    import logging

    import logging.config

    logging.config.fileConfig("logger.conf")

    logger = logging.getLogger("你当时写的qualname ")

    logger.debug('This is debug message')

    四:DEMO

    #logging_setting.ini

    [loggers]

    keys=root,log01 #可以没有别的,但是必须得有root

     

    [handlers]

    keys=hand01,hand02

     

    [formatters]

    keys=form01,form02

     

    [logger_root]

    level=NOTSET #可以不写

    handlers=hand01 #可以为空

     

     

    [logger_log01]

    level=ERROR

    handlers=hand01

    propagate=0

    qualname=test.test123

     

    [handler_hand01]

    class=StreamHandler

    level=DEBUG

    formatter=form01

    args=() #括号内可为空,但不可不写,括号内有一个内容时,必须在后面加",",以保证是tuple类型 除此之外,另外别的后面不允许有多余的逗号

     

     

    [handler_hand02]

    class=FileHandler

    level=NOTSET

    formatter=form01

    args=("test1.log","a",'utf-8')

     

    [formatter_form01]

    format=F1 %(asctime)s %(levelname)s %(message)s

    datefmt=

    class=logging.Formatter

     

    [formatter_form02]

    format= %(asctime)s %(levelname)s %(message)s

    datefmt=

    class=logging.Formatter

    import logging.config

    logging.config.fileConfig("logging_setting.ini")

    log1 = logging.getLogger("test.test123")

    log1.info("阿斯顿发")

     

     

     

     

    #args=()可以里面没有内容,但是只有一个内容的时候,后面必须加上逗号,表示是一个元组

  • 相关阅读:
    【Educational Codeforces Round 101 (Rated for Div. 2) C】Building a Fence
    【Codeforces Round #698 (Div. 2) C】Nezzar and Symmetric Array
    【Codeforces Round #696 (Div. 2) D】Cleaning
    【Codeforces Round #696 (Div. 2) C】Array Destruction
    【Educational Codeforces Round 102 D】Program
    【Educational Codeforces Round 102 C】No More Inversions
    【Good Bye 2020 G】Song of the Sirens
    【Good Bye 2020 F】Euclid's nightmare
    使用mobx入门
    requestAnimationFrame 控制速度模拟setinterval
  • 原文地址:https://www.cnblogs.com/twotigers/p/7743230.html
Copyright © 2011-2022 走看看