【问题标题】:change config file's format dynamically for python为python动态更改配置文件的格式
【发布时间】:2013-12-12 17:36:51
【问题描述】:

我有以下关于 python 日志记录模块的问题。

  1. 能否从代码中动态更改配置文件中定义的格式?

  2. 我的应用程序中的一个模块能否通过配置文件配置其格式,而其他模块通过 python 脚本配置其格式和其他属性?

  3. 我想将上下文信息添加到我的日志中,但我正在使用配置文件来定义记录器属性。我该怎么办?

附:如果需要,我可以提供一些代码和应用程​​序结构。

结构 -

1.) 代理 - 调用 logging_setup 脚本来设置记录器配置 2.) 模块

我有一个 logging_setup 脚本,它有以下方法:

logging_setup.py

import logging
import logging.handlers

LOGGER_FORMAT="%(asctime)s %(CMDID)s %(levelname)s %(message)s"

class testFormatter(logging.Formatter):
  def format(self,record):
    record.message=record.getMessage()
    if string.find(self._fmt,"%(asctime)") >= 0:
      record.asctime = self.formatTime(record, self.datefmt)

    if threading.currentThread().getName() in cmdidDict:
      record.CMDID=cmdidDict[threading.currentThread().getName()]
    else:
      record.CMDID="Oda_EnvId"
    return self._fmt % record.__dict__

def initLogging(loggername,newCMDID):
  global CMDID
  global newCMDID
  logger=logger.getLogger(loggername)
  format=testFormatter(LOGGER_FORMAT)
  *set up other config*

所以我想在我的日志中动态设置 CMDID 变量,这是通过使用 testFormatter 类并使用该类初始化记录器来实现的。还有更多内容,但我希望你能明白。

LOGGER_FORMAT="%(asctime)s %(CMDID)s %(levelname)s"

但现在我想从模块本身更改日志的日志级别,始终保持格式相同。所以我决定创建一个通用配置文件,它可以在整个应用程序中为我设置日志记录配置。我的问题是如何让这个配置文件设置 CMDID 变量的格式。

配置文件

[loggers]
keys=root,testModule

[formatters]
keys=generic

[handlers]
keys=fh

[logger_root]
level=DEBUG
handlers=fh

[logger_testModule]
level=ERROR        <<-- setting my log level here
handlers=fh
qualname=testModule
propagate=0

[handler_fh]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=generic
maxBytes=1000
args=('spam.log',)

[formatter_generic]
format=%(asctime)s %(levelname)s %(message)s   <<---This should somehow set CMDID var

【问题讨论】:

  • 请更清楚您的要求
  • 添加了代码...希望现在有点清楚了。
  • 尝试使用ConfigParser并在运行时从配置section读取各种日志级别并插入
  • Siva 你能举例说明你插入它是什么意思吗?你的意思是创建一个新的处理程序并添加它??
  • 实际上我只是希望配置文件中指定的格式应该被我在 python 代码中定义的内容覆盖。我怎样才能做到这一点??

标签: python logging


【解决方案1】:

我认为可能需要提供该代码和应用程​​序结构。

  1. 不清楚更改格式是什么意思。什么格式?

  2. 当然。当你为 variable_1 赋值时,从配置中拉取它,然后当你为 variable_2 赋值时,从其他地方获取值。

  3. 您希望日志中包含哪些上下文信息?您可以将日志配置为具有未预先确定的字段并将变量值记录到其中。因此,您所要做的就是根据代码中发生的情况设置该变量的值并将其写入日志。

【讨论】:

    【解决方案2】:

    你可以试试这个:

    import sys
    import ConfigParser
    
    CONFIG = ConfigParser.ConfigParser()
    
    CONFIG.read('config.ini')  # This is your Config file
    
    print "BEFORE CHANGE"
    CONFIG.write(sys.stdout)
    
    ## Try printing any option under any section
    #print CONFIG.get('section1', 'option1')
    
    CONFIG.set('section1', 'option1', value=1234)
    
    print "AFTER CHANGE"
    CONFIG.write(sys.stdout)
    

    输出:

    BEFORE CHANGE
    [section1]
    option1 = aaaa
    option2 = bbbb
    
    [sectin2]
    option1 = cccc
    option2 = dddd
    
    AFTER CHANGE
    [section1]
    option1 = 1234
    option2 = bbbb
    
    [sectin2]
    option1 = cccc
    option2 = dddd
    

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多