在Python 2.4b1中使用字典args记录消息会导致异常 [英] Logging messages with dictionary args in Python 2.4b1 leads to exception

查看:59
本文介绍了在Python 2.4b1中使用字典args记录消息会导致异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我想在Py2.4b1中这样做:


-------- -----------------------

导入日志

values = {''test'': ''bla''}

logging.log(logging.FATAL,''测试是%(测试)s'',值)

------ -------------------------


这导致例外:

-----------------------------

Traceback(最近一次调用最后一次):

文件" /usr/lib/python2.4/logging / __ init __。py",第692行,发出

msg = self.format(record)

文件" /usr/lib/python2.4/logging / __ init __。py",第578行,格式

返回fmt.format(记录)

文件" /usr/lib/python2.4/logging / __ init __。py",第368行,格式为

record.message = record.getMessage()

文件" /usr/lib/python2.4/logging / __ init __。py",第239行,在getMessage中

msg = msg%self.args

TypeError :格式要求映射

-----------------------------


现在,values /是/映射。我查看了logging / __ init__.py,但是我只能看到''* args''参数(因为''值''被输入)应该

已在logging.log()中解压缩:


apply(root.log,(level,msg)+ args,kwargs)


这应该导致我的

理解中的第二个参数是''(level,msg,args)''哪个应该允许在以后执行此操作时使用dicts
$在LogRecord中b $ b:


msg = msg%self.args


现在,我有点困惑,这不是''工作,我也很担心

,这只是在模块方法中完成,而不是在实际处理日志记录的类中的内部(这里的根对象)案件)。这应该是

鼓励模块方法的转移行为和具有相同名称的Logger

对象方法。对我来说这看起来像个错误。添加

元组应该/仅在类内完成,而不是在模块函数中完成。


直接处理字典不是更好在LogRecord里面?

这样的东西可以解决这个问题:


------------------- ----

如果len(self.args)== 1:

msg = msg%self.args [0]#可能是单个值或字典

else:

msg = msg%self.args#看起来像一个值元组

----------- ------------


有谁可以对此发表评论?


Stefan

Hi!

I''m trying to do this in Py2.4b1:

-------------------------------
import logging
values = {''test'':''bla''}
logging.log(logging.FATAL, ''Test is %(test)s'', values)
-------------------------------

This leads to en exception:

-----------------------------
Traceback (most recent call last):
File "/usr/lib/python2.4/logging/__init__.py", line 692, in emit
msg = self.format(record)
File "/usr/lib/python2.4/logging/__init__.py", line 578, in format
return fmt.format(record)
File "/usr/lib/python2.4/logging/__init__.py", line 368, in format
record.message = record.getMessage()
File "/usr/lib/python2.4/logging/__init__.py", line 239, in getMessage
msg = msg % self.args
TypeError: format requires a mapping
-----------------------------

Now, values /is/ a mapping. I looked into logging/__init__.py, but I can
only see that the ''*args'' parameter (as which ''values'' is fed in) should
have been unpacked like this in logging.log():

apply(root.log, (level, msg)+args, kwargs)

Which should lead to the second argument being ''(level, msg, args)'' in my
understanding and which should allow using dicts when doing this later on
in LogRecord:

msg = msg % self.args

Now, I''m somewhat confused that this doesn''t work and I''m also worried
that this is only done in the module methods and not inside the class that
actually handles the logging (the root object in this case). This should
encourage a diverting behaviour of the module methods and the Logger
object methods with the same name. This looks like a bug to me. Adding the
tuple should /only/ be done inside the class, not in the module functions.

Wouldn''t it be better to handle dictionaries directly inside LogRecord?
Something like this would do the trick:

-----------------------
if len(self.args) == 1:
msg = msg % self.args[0] # may be a single value or a dictionary
else:
msg = msg % self.args # looks like a value tuple
-----------------------

Could anyone please comment on this?

Stefan

推荐答案

Stefan Behnel< be ******* @ dvs1.informatik.tu-darmstadt.de>在消息新闻中写道:< cl ********** @ lnx107.hrz.tu-darmstadt.de> ...
Stefan Behnel <be*******@dvs1.informatik.tu-darmstadt.de> wrote in message news:<cl**********@lnx107.hrz.tu-darmstadt.de>...
现在,我有点困惑这个不起作用,我也担心这只是在模块方法中完成,而不是在实际处理日志记录的类中(在这种情况下是根对象)。这应该鼓励模块方法和具有相同名称的Logger
对象方法的转移行为。对我来说这看起来像个错误。添加
元组应该/仅在类内完成,而不是在模块函数中完成。
Now, I''m somewhat confused that this doesn''t work and I''m also worried
that this is only done in the module methods and not inside the class that
actually handles the logging (the root object in this case). This should
encourage a diverting behaviour of the module methods and the Logger
object methods with the same name. This looks like a bug to me. Adding the
tuple should /only/ be done inside the class, not in the module functions.




这不是一个bug;但是,用例并非不合理,所以我会尝试

并将功能(由Stefan发送给我作为补丁)进入2.4

final。 br />

问候,

Vinay Sajip



It''s not a bug; however, the use case is not unreasonable, so I''ll try
and get the functionality (sent by Stefan to me as a patch) into 2.4
final.

Regards,
Vinay Sajip


Stefan Behnel< be ******* @ dvs1.informatik.tu-darmstadt.de>在消息新闻中写道:< cl ********** @ lnx107.hrz.tu-darmstadt.de> ...
Stefan Behnel <be*******@dvs1.informatik.tu-darmstadt.de> wrote in message news:<cl**********@lnx107.hrz.tu-darmstadt.de>...
嗨!

我想在Py2.4b1中这样做:

----------------------------- -
import logging
values = {''test'':''bla''}
logging.log(logging.FATAL,''Test is%(test)s''' ,价值观)
-------------------------------

这导致en例外:

-----------------------------
Traceback(最近一次通话) :
文件" /usr/lib/python2.4/logging / __ init __。py",第692行,在发光中
msg = self.format(记录)
文件" / usr /lib/python2.4/logging/__init__.py" ;,第578行,格式为
return fmt.format(record)
文件" /usr/lib/python2.4/logging / __ init__。 py",第368行,格式为
record.message = record.getMessage()
文件" /usr/lib/python2.4/logging / __ init __。py",第239行,在getMessage中
msg = msg%se lf.args
TypeError:格式需要映射
-----------------------------

不确定2.4但是在2.3中: -

logging.fatal(''测试是%(测试)s'',值)

$ b然而,$ b有相同的错误: -

logging.fatal(''测试是%(测试)s''%值)


很好。为什么你不应该以这种方式使用它?

现在,values /是/映射。我查看了logging / __ init__.py,但我只能看到''* args''参数(其中''值''被输入)应该
已经解压缩如下logging.log():

apply(root.log,(level,msg)+ args,kwargs)

哪个应该导致第二个参数为''(级别,msg,args)''在我的理解中,哪些应该允许在以后在LogRecord中执行此操作时使用dicts:

msg = msg%self.args


* args不是映射,它是一个元组。将找到

格式字符串中的位置参数。

现在,我有点困惑,这不起作用,我也很担心这只是在模块方法中完成,而不是在实际处理日志记录的类中(在这种情况下是根对象)。这应该鼓励模块方法和具有相同名称的Logger
对象方法的转移行为。对我来说这看起来像个错误。添加
元组应该/仅在类内完成,而不是在模块函数中完成。

直接在LogRecord中处理字典会不会更好?
某事像这样可以解决问题:

-----------------------
if len(self.args)= = 1:
msg = msg%self.args [0]#可能是单个值或字典
否则:
msg = msg%self.args#看起来像一个值元组<赞/ -----------------------

有谁可以对此发表评论?

Stefan
Hi!

I''m trying to do this in Py2.4b1:

-------------------------------
import logging
values = {''test'':''bla''}
logging.log(logging.FATAL, ''Test is %(test)s'', values)
-------------------------------

This leads to en exception:

-----------------------------
Traceback (most recent call last):
File "/usr/lib/python2.4/logging/__init__.py", line 692, in emit
msg = self.format(record)
File "/usr/lib/python2.4/logging/__init__.py", line 578, in format
return fmt.format(record)
File "/usr/lib/python2.4/logging/__init__.py", line 368, in format
record.message = record.getMessage()
File "/usr/lib/python2.4/logging/__init__.py", line 239, in getMessage
msg = msg % self.args
TypeError: format requires a mapping
-----------------------------
Not sure about 2.4 but but in 2.3:-
logging.fatal( ''Test is %(test)s'', values)

with the same error, however :-
logging.fatal( ''Test is %(test)s'' % values)

is fine. An why shouldn''t you use it in this fashion?
Now, values /is/ a mapping. I looked into logging/__init__.py, but I can
only see that the ''*args'' parameter (as which ''values'' is fed in) should
have been unpacked like this in logging.log():

apply(root.log, (level, msg)+args, kwargs)

Which should lead to the second argument being ''(level, msg, args)'' in my
understanding and which should allow using dicts when doing this later on
in LogRecord:

msg = msg % self.args

*args is not a mapping, its a tuple. Positional arguments in the
format string will be found.
Now, I''m somewhat confused that this doesn''t work and I''m also worried
that this is only done in the module methods and not inside the class that
actually handles the logging (the root object in this case). This should
encourage a diverting behaviour of the module methods and the Logger
object methods with the same name. This looks like a bug to me. Adding the
tuple should /only/ be done inside the class, not in the module functions.

Wouldn''t it be better to handle dictionaries directly inside LogRecord?
Something like this would do the trick:

-----------------------
if len(self.args) == 1:
msg = msg % self.args[0] # may be a single value or a dictionary
else:
msg = msg % self.args # looks like a value tuple
-----------------------

Could anyone please comment on this?

Stefan




我没看过来源,所以我一直在评论。


问候,Paul Clinch



I haven''t read the source so I punt on commenting.

Regards, Paul Clinch


Paul Clinch schrieb:
Paul Clinch schrieb:
logging.fatal(''Test is%(test)s''%values)
很好。为什么你不应该以这种方式使用它?
logging.fatal( ''Test is %(test)s'' % values)
is fine. An why shouldn''t you use it in this fashion?




我以为日志API通常只是在
$ b $时才会做昂贵的事情b它知道它必须。你/可以/做到这一点,但因为它没有免费获得

- 为什么不首先检查你是否真的会记录这条消息?


好吧,我知道无论如何都有开销:函数调用,对象创建,

等。但复制字符串和创建对象的字符串表示

可以是/非常/昂贵,只有在日志消息是绝对写的时候才能完成。


我只是说从参数构造一条日志消息是这样一个常见的事情是值得为它提供有效的API支持。并且

我认为在这一点上防止字典的使用确实是反pythonic




Stefan



I thought a logging API was usually trying to do costly things only when
it knows it has to. You /could/ do that, but since it doesn''t come for
free - why not first do the check if you will actually log this message?

Ok, I know there''s an overhead anyway: function calls, object creation,
etc. But copying strings and creating string representations of objects
can be /very/ expensive and should only be done if the log message is
definitely written.

I''d simply say that constructing a log message from arguments is such a
common thing that it is worth providing efficient API support for it. And
I think it''s truely counter-pythonic to prevent the usage of dictionaries
at this point.

Stefan


这篇关于在Python 2.4b1中使用字典args记录消息会导致异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆