[英]Google App Engine/Python - Change logging formatting
How can one change the formatting of output from the logging
module in Google App Engine? 如何更改Google App Engine中logging
模块的输出格式?
I've tried, eg: 我试过了,例如:
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. 但是,这会导致重复的日志记录输出:一个是来自google/appengine/tools/dev_appserver.py
(或Google代码中某处)的日志记录处理程序,另一个来自我上面的新StreamHandler
。 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. 顶行显然来自dev_appserver.py
,来自我的代码的底线。
So I guess the corollary question is: How can change the formatting of Google App Engine, yet avoid the duplicate output? 所以我想推论的问题是:如何更改Google App Engine的格式,同时避免重复输出?
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. 这有点像黑客,因为您必须直接访问存储在根记录器中的handlers
列表。 The problem is GAE automatically uses logging
before your code is ever run - this creates a default handler. 问题是GAE在您的代码运行之前自动使用logging
- 这会创建一个默认处理程序。 Unfortunately, I don't see how you can get a reference to this handler without directly accessing the handlers
list as above. 不幸的是,我没有看到如何在不直接访问上面的handlers
列表的情况下获得对此处理程序的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.