Google App Engine/Python - 更改日志记录格式

发布于 2024-09-06 11:03:47 字数 1068 浏览 2 评论 0原文

如何更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎夕旧梦 2024-09-13 11:03:48

这是一种无需重复输出即可更改日志记录格式的方法:

# directly access the default handler and set its format directly
logging.getLogger().handlers[0].setFormatter(fr)

这有点像黑客,因为您必须直接访问存储在根记录器中的处理程序列表。问题是 GAE 在代码运行之前自动使用日志记录 - 这会创建一个默认处理程序。不幸的是,我不知道如何在不直接访问上面的 handlers 列表的情况下获得对此处理程序的引用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文