RotatingFileHandler和日志配置文件 [英] RotatingFileHandler and logging config file

查看:101
本文介绍了RotatingFileHandler和日志配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我已成功编写Python以使用

RotatingFileHandler执行我想要的操作,但无法通过以下方式获取相同的行为

a配置文件。


我想在每次运行应用程序时创建一个日志文件,最多10个

文件保持最后的调用。这是通过


完成的.self._logger = logging.getLogger(''PDGUI'')

#我们将'手动'旋转文件,所以使用零旋转大小。

handler = logging.handlers.RotatingFileHandler(\

" .. \\PDGUI.log"," a", 0,10)

handler.doRollover()#强制使用下一个文件


formatter = logging.Formatter("%(asctime)s " + \

"%(levelname)s \ t(消息)s")

handler.setFormatter(formatter)

self._logger.addHandler(handler)

self._logger.setLevel(logging.INFO)


我想做同样的事情使用配置文件(这样,如果这个

我开始了有一个相当简单的配置文件,唯一有趣的一点

是:


[handler_hand01]

class = handlers.Rotat ingFileHandler

level = NOTSET

formatter = form01

args =(" .. \\PDGUI.log",a ;,0,10,)

但是我还有一个问题 - 如何强制配置文件中出现handler.doRollover()

?在配置文件本身中似乎没有任何方法可以做到这一点,这是好的,也许是合适的。

[顺便说一下,还有其他人注意到RotatingFileHandler在文档中没有记录

?所有其他文件处理程序至少有一个段落

他们的选项,但RFH没有!!


我找不到任何办法代码中的handler.doRollover(),如果

我已经开始使用配置文件了。类似

logging.config.fileConfig(options.logconfig)

#BUGBUG如果配置文件指定了RotatingFileHandler,

#我们现在需要强制执行doRollover,但是没办法!



handler = logging.getLogger()。getHandler()#THIS DOESN'' T EXIST!

handler.doRollover()#强制使用下一个文件

想法,建议等?这太糟糕了 - 使用代码方法,我可以完全按照我想要的那样做b $ b,但不能使用配置文件。


- rob

Hello,

I''ve successfully coded Python to do what I want with a
RotatingFileHandler, but am having trouble getting the same behavior via
a config file.

I wanted to create one log file each time I run my app, with up to 10
files kept from the last invocations. This was accomplished with

self._logger = logging.getLogger(''PDGUI'')
# We will rotate the files ''manually'', so use zero rotate size.
handler = logging.handlers.RotatingFileHandler( \
"..\\PDGUI.log", "a", 0, 10)
handler.doRollover() # force the next file to be used

formatter = logging.Formatter("%(asctime)s " + \
"%(levelname)s\t%(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._logger.setLevel(logging.INFO)

I''d like to do the same thing with a config file (so that, if this
*isn''t* the behavior I want, I can change the config file, of course).
I started with a rather plain config file, with the only interesting bit
being:

[handler_hand01]
class=handlers.RotatingFileHandler
level=NOTSET
formatter=form01
args=("..\\PDGUI.log", "a", 0, 10,)
But I still have one problem - how do I force the "handler.doRollover()"
to happen with the config file? There doesn''t seem to be any way to do
this in the config file itself, which is OK and perhaps appropriate
[BTW, has anyone else noticed that RotatingFileHandler isn''t documented
in the docs? All the other file handlers have at least a paragraph on
their options, but nothing for RFH!]

I can''t find any way to do the handler.doRollover() in code, either, if
I''ve started off with a config file. Something like
logging.config.fileConfig(options.logconfig)

# BUGBUG If the config file specifies a RotatingFileHandler,
# we need to force a doRollover now, but there''s no way!
#
handler = logging.getLogger().getHandler() # THIS DOESN''T EXIST!
handler.doRollover() # force the next file to be used
Ideas, suggestions, etc? It''s too bad - with the code method, I can do
exactly what I want, but not with the config file.

- rob

推荐答案

Rob Cranfill写道:
Rob Cranfill wrote:
[BTW,还有其他人注意到RotatingFileHandler没有记录
在文档中?所有其他文件处理程序至少有一个关于
选项的段落,但RFH没有任何内容!]
[BTW, has anyone else noticed that RotatingFileHandler isn''t documented
in the docs? All the other file handlers have at least a paragraph on
their options, but nothing for RFH!]




这是最新的文档。


Kent



It is in the latest docs.

Kent


Kent Johnson写道:
Kent Johnson wrote:

这是最新的docs。

肯特

It is in the latest docs.

Kent




不,不是。 (但感谢你的回复!)

http://docs.python.org/lib/logging-c...ileformat.html

包含所有其他内容(好的,也许不是全部,我没有彻底检查过,但是

它有九个''em)但没有RFH。


或者那不是 ;最新的文档?


- rob



No, it isn''t. (But thanks for replying anyway!)

http://docs.python.org/lib/logging-c...ileformat.html

has all the others (OK, maybe not all, I haven''t thoroughly checked, but
it''s got nine of ''em) but nothing for RFH.

Or is that not "the latest docs"?

- rob


Rob Cranfill写道:
Rob Cranfill wrote:
Kent Johnson写道:
Kent Johnson wrote:
这是最新的文档。
不,它不是。 (但感谢你的回复!)
It is in the latest docs.
No, it isn''t. (But thanks for replying anyway!)




你能证明它不是吗? ;-)
http://docs.python .org / lib / logging-c ... ileformat.html

包含所有其他内容(好吧,也许不是全部,我还没有彻底检查过,但是
它有九个''em)但RFH没有。

或者那不是最新的文档?



Can you prove it isn''t? ;-)
http://docs.python.org/lib/logging-c...ileformat.html

has all the others (OK, maybe not all, I haven''t thoroughly checked, but
it''s got nine of ''em) but nothing for RFH.

Or is that not "the latest docs"?




当然,但它不是唯一可以查看当前文档的地方。

请尝试此处: http://docs.python.org/lib/node332.html


拼图中缺失的部分可能是配置文件中''args''与参数之间的连接



传递给类的__init__方法....


-Peter



Sure, but it''s not the only place to look in the current docs.
Try here instead: http://docs.python.org/lib/node332.html

The missing piece of the puzzle might be the connection
between the ''args'' in the config file and the arguments
passed to the __init__ method of the class....

-Peter


这篇关于RotatingFileHandler和日志配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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