Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python日志对象_Python_Logging - Fatal编程技术网

Python日志对象

Python日志对象,python,logging,Python,Logging,我正在尝试根据记录器的类重新格式化发送到记录器的输出数据 例如: 字符串将按原样打印 字典/列表将自动缩进/美化为html 我的自定义类将单独处理并转换为html 我的问题是发送到格式化程序的消息始终是字符串。文档特别指出,您可以将对象作为消息发送,但在我格式化它们之前,它似乎正在将对象转换为字符串 class MyFormatter(logging.Formatter): def format(self, record): #The problem is that

我正在尝试根据记录器的类重新格式化发送到记录器的输出数据

例如:

  • 字符串将按原样打印
  • 字典/列表将自动缩进/美化为html
  • 我的自定义类将单独处理并转换为html
我的问题是发送到格式化程序的消息始终是字符串。文档特别指出,您可以将对象作为消息发送,但在我格式化它们之前,它似乎正在将对象转换为字符串

class MyFormatter(logging.Formatter):

    def format(self, record):
        #The problem is that record.message is already a string...
        ...

哪里是我处理作为消息发送的对象的合适位置?

可能在您正在记录的对象的
\uu str\uuu()
方法中?

好的,我已经找到了

官方文档中的文档有点不清楚,但基本上有两个属性

LogRecord.message->消息的字符串表示形式 及 LogRecord.msg->消息本身

要获取实际对象,必须引用.msg才能使其工作


我希望这对其他人有用。

日志记录不应包含.message,除非它已由默认格式化程序(或添加.message的costum格式化程序)格式化。也许您有一个多处理程序,或者您正在从派生函数调用Formatter.format(record),这应该是不必要的?您是正确的。我没有显式调用父类format(),但是,我没有重写默认的init,因此我怀疑这与此有关。