Google App Engine / Python - 更改记录格式 [英] Google App Engine/Python - Change logging formatting
问题描述
如何更改Google App Engine中 logging
模块的输出格式?
(例如:
log_format =*%(asctime)s%(levelname)-8s%(message)s
date_format =%a,%d%b%Y%H:%M:%S
console = logging.StreamHandler()
fr = logging.Formatter( log_format)
console.setFormatter(fr)
logger = logging.getLogger()
logger.addFilter(SuperfluousFilter())
logger.addHandler(console)
logger.setLevel(logging.DEBUG)
console.setLevel(logging.DEBUG)
$ b logging.error(重新配置日志记录)
code>
然而,这会导致重复的日志输出:一个来自 google / appengine / tools / dev_appserver.py
(或Google代码中的某处) ,还有一个来自我上面的新的 StreamHandler
。以上代码输出:
错误2010-06-23 20:46:18,871 initialize.py:38]重新配置记录
2010-06-23 20:46:18,871错误重新配置日志记录
其中第一行显然来自 dev_appserver。 py
,这是我的代码中的底线。
所以我想推论的问题是:如何更改Google App Engine的格式避免重复输出?
感谢您阅读。
Brian
<您可以在不重复输出的情况下更改记录格式:
<$> c $ c>#直接访问默认处理程序并直接设置其格式
logging.getLogger()。handlers [0] .setFormatter(fr)
由于您必须直接访问存储在根记录器中的处理程序
列表,因此这有点破解。问题是GAE会在您的代码运行之前自动使用 logging ,这会创建一个默认处理程序。不幸的是,我没有看到如何在不直接访问
处理程序
列表的情况下获得对此处理程序的引用。
How can one change the formatting of output from the logging
module in Google App Engine?
I've tried, e.g.:
log_format = "* %(asctime)s %(levelname)-8s %(message)s"
date_format = "%a, %d %b %Y %H:%M:%S"
console = logging.StreamHandler()
fr = logging.Formatter(log_format)
console.setFormatter(fr)
logger = logging.getLogger()
logger.addFilter(SuperfluousFilter())
logger.addHandler(console)
logger.setLevel(logging.DEBUG)
console.setLevel(logging.DEBUG)
logging.error("Reconfiguring logging")
However this results in duplicate logging output: One with the logging handler from google/appengine/tools/dev_appserver.py
(or somewhere in the Google code), and one from my new StreamHandler
above. The above code outputs:
ERROR 2010-06-23 20:46:18,871 initialize.py:38] Reconfiguring logging 2010-06-23 20:46:18,871 ERROR Reconfiguring logging
Where the top line is clearly from dev_appserver.py
, the bottom line from my code.
So I guess the corollary question is: How can change the formatting of Google App Engine, yet avoid the duplicate output?
Thank you for reading.
Brian
Here is one way you can change the logging format without duplicating output:
# directly access the default handler and set its format directly
logging.getLogger().handlers[0].setFormatter(fr)
This is a bit of a hack because you have to directly access the handlers
list stored in the root logger. The problem is GAE automatically uses logging
before your code is ever run - this creates a default handler. Unfortunately, I don't see how you can get a reference to this handler without directly accessing the handlers
list as above.
这篇关于Google App Engine / Python - 更改记录格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!