我的最佳选择是使用自定义格式化程序(而不是logging.formatter)。。。以下是logging.Formatter.format的源代码,供参考:def format(self, record):

record.message = record.getMessage()

if string.find(self._fmt,"%(asctime)") >= 0:

record.asctime = self.formatTime(record, self.datefmt)

s = self._fmt % record.__dict__

if record.exc_info:

# Cache the traceback text to avoid converting it multiple times

# (it's constant anyway)

if not record.exc_text:

record.exc_text = self.formatException(record.exc_info)

if record.exc_text:

if s[-1:] != "\n":

s = s + "\n"

s = s + record.exc_text

return s

我很清楚,如果self.\u fmt是从文本文件(单行)中读取的,那么任何类型的转义都是不可能的。也许您可以从logging.Formatter扩展,重写此方法并用第4行替换如下内容:s = self._fmt.replace('\\n', '\n') % record.__dict__

或者更一般的东西,如果你想让其他东西也逃脱。

编辑:或者,您可以在init方法中执行此操作一次(而不是每次格式化消息)。但正如其他人已经指出的,ConfigParser支持多行,因此不需要走这条路。。。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐