Google App Engine/Python - 更改日志记录格式
如何更改 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)
logging.error("Reconfiguring logging")
但这会导致重复的日志输出:来自 google/appengine/tools/dev_appserver.py
(或 Google 代码中的某处),以及我的一个上面新的StreamHandler
。上面的代码输出:
ERROR 2010-06-23 20:46:18,871 initialize.py:38] Reconfiguring logging 2010-06-23 20:46:18,871 ERROR Reconfiguring logging
其中顶行显然来自 dev_appserver.py
,底行来自我的代码。
所以我想必然的问题是:如何更改 Google App Engine 的格式,同时避免重复输出?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种无需重复输出即可更改日志记录格式的方法:
这有点像黑客,因为您必须直接访问存储在根记录器中的处理程序列表。问题是 GAE 在代码运行之前自动使用
日志记录
- 这会创建一个默认处理程序。不幸的是,我不知道如何在不直接访问上面的handlers
列表的情况下获得对此处理程序的引用。Here is one way you can change the logging format without duplicating output:
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 useslogging
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 thehandlers
list as above.