在 Google App Engine 应用程序中关闭控制台日志记录

Posted

技术标签:

【中文标题】在 Google App Engine 应用程序中关闭控制台日志记录【英文标题】:Turn off console logging in Google App Engine Application 【发布时间】:2013-08-06 02:12:09 【问题描述】:

我有一个在 Google App Engine (GAE) v1.8.2 上运行的应用程序。我一直在使用 java.util.logging。我的课程将记录器定义为:

private static final Logger logger = Logger.getLogger(MyClass.class.getName());

我的 appengine-web.xml 有以下几行:

  <!-- Configure java.util.logging -->
  <system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
  </system-properties>

logging.properties 文件包含以下几行:

# A default java.util.logging configuration.
# (All App Engine logging is through java.util.logging by default).
#
# To use this configuration, copy it into your application's WEB-INF
# folder and add the following to your appengine-web.xml:
# 
# <system-properties>
#   <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
# </system-properties>
# WARNING , INFO, SEVERE, OFF
handlers=java.util.logging.ConsoleHandler
# Set the default logging level for all loggers to WARNING
.level = OFF
java.util.logging.ConsoleHandler.level=OFF
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

但是,我仍然在浏览器控制台中继续看到我的代码中的 INFO 日志记录。如何完全关闭控制台注销?

更新:我的 appengine-java-sdk-1.8.2/config/sdk/logging.properties 文件的内容:

# Logging configuration file for Google App Engine tools.

# Specify the handlers to create in the root logger
# (all loggers are children of the root logger)
# The following creates the console handler
handlers = java.util.logging.ConsoleHandler

# Set the default logging level for the root logger
.level = INFO

# Set the default logging level for the datanucleus loggers
DataNucleus.JDO.level=WARNING
DataNucleus.Persistence.level=WARNING
DataNucleus.Cache.level=WARNING
DataNucleus.MetaData.level=WARNING
DataNucleus.General.level=WARNING
DataNucleus.Utility.level=WARNING
DataNucleus.Transaction.level=WARNING
DataNucleus.Datastore.level=WARNING
DataNucleus.ClassLoading.level=WARNING
DataNucleus.Plugin.level=WARNING
DataNucleus.ValueGeneration.level=WARNING
DataNucleus.Enhancer.level=WARNING
DataNucleus.SchemaTool.level=WARNING

# FinalizableReferenceQueue tries to spin up a thread and fails.  This
# is inconsequential, so don't scare the user.
com.google.common.base.FinalizableReferenceQueue.level=WARNING
com.google.appengine.repackaged.com.google.common.base.FinalizableReferenceQueue.level=WARNING

# We assume that people will generally want to see this message, even
# if they override the root level to WARNING.  If they really want to
# turn it off, they can always override this level as well.
com.google.appengine.tools.development.DevAppServerImpl.level=INFO

谢谢。

【问题讨论】:

【参考方案1】:

您应该确认LogManager 正在使用您的logging.properties。一种简单的测试方法是将ConsoleHandler 格式化程序从java.util.logging.SimpleFormatter 更改为java.util.logging.XMLFormatter,然后查看您的输出是否为xml。如果是 xml,那么您可以尝试注释掉 handlers= 行。如果输出仍然来自SimpleFormatter,则:

    检查 logging.properties 的路径。 确保SecurityManager 不会阻止对日志的更改。 编写代码遍历所有记录器并输出所有附加处理程序的类名。然后更新您的 logging.properties 以禁用可能正在写入控制台的任何其他处理程序。 编写代码以遍历所有记录器并禁用所有控制台处理程序。 System.out.close(); System.err.close(); new FileOutputStream(FileDescriptor.out).close(); new FileOutputStream(FileDescriptor.err).close();

【讨论】:

我仍然看到 SimpleFormatter 的输出。我的 logging.properties 文件位于 WEB-INF 下。如何检查#2、#3 和#4?感谢您的帮助。 @DFB #2 System.getSecurityManager().checkPermission。 #3 & #4 使用 LogManager 获取所有记录器名称,然后按名称询问每个记录器。 我应该检查哪个权限?我不能在 GAE 中使用 LogManager。它说 GAE 的 Java 运行时环境不支持 LogManager。在我的代码中,我只有信息和警告日志(logger.info("something") 和 logger.warn("something"))。似乎它根本没有使用我的 logging.properties。再次感谢您的帮助。 检查'new LoggingPermission("control")'。我同意它似乎根本没有使用它。让我倾向于#1。必须有另一种方法来指定路径。 IE。 "./WEB-INF/logging.properties" 我刚刚注意到它确实在 Eclipse 的控制台中切换为 XML 格式,但在 Chrome 的控制台中,它仍然是简单格式。

以上是关于在 Google App Engine 应用程序中关闭控制台日志记录的主要内容,如果未能解决你的问题,请参考以下文章

在 Google App Engine 中上传文件

使用 rtmplite 在 Google App Engine 中录制音频?

人们如何在自己的域中使用 Google App-Engine 应用程序?

Google-App-Engine 上的 Grails - 它死了吗? [关闭]

在 Google App Engine 中部署 Spring Boot gradle 应用

如何在 Django/Google App Engine 中制作日志颜色?