简体   繁体   English

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

[英]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.

相关问题 如何在Google App Engine中动态更改python应用程序的日志记录级别? - How to dynamically change logging level of python app in Google App Engine? 如何更改Google App Engine Python生产服务器中的日志记录级别? - How to change logging level in google app engine python production server? python日志记录不适用于使用Google App Engine的Web应用程序 - python logging does not work for web app using google app engine 在CodeEnvy中查看Google App Engine Python日志消息 - viewing google app engine Python logging messages in CodeEnvy 让logging.debug()在Google App Engine / Python上运行 - Getting logging.debug() to work on Google App Engine/Python 在App Engine Standard python中使用Google Stackdriver日志时出错 - Error using Google Stackdriver Logging in App Engine Standard python Google Stackdriver使用Redis队列登录App Engine(Python) - Google Stackdriver logging in App Engine (Python) using Redis queue 如何有效使用Google App Engine python应用程序中的日志记录? - How to efficiently use logging in Google App Engine python application? Python更改目录Google App Engine - Python Change Directory Google App Engine Google App Engine,更改哪个python版本 - Google App Engine, Change which python version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM