首页 > 解决方案 > 将 logging.properties 与标准 Appengine Java 11 一起使用

问题描述

我正在尝试使用logging.properties文件对我的标准 AppEngine Java 11 应用程序进行一些日志记录修改,我的应用程序是一个 Jetty Web 服务器,我通过将以下入口点添加到我的app.yaml文件来运行它

runtime: java11
entrypoint: 'java -cp "*" com.jettymain.Main webapp.war'

现在这里的谷歌文档建议为了使用logging.properties. 配置文件的路径必须作为系统属性提供给您的应用程序:-Djava.util.logging.config.file=/path/to/logging.properties

我已经尝试在代码中设置它,首先在 com.jettymain.Main 类中通过执行以下操作启动 Jetty 嵌入式 Web 服务器

System.setProperty("java.util.logging.config.file", "WEB-INF/logging.properties")

但这不起作用,修改入口点app.yaml确实使网络服务器加载了这些配置,但它未能加载谷歌云日志记录处理程序类com.google.cloud.logging.LoggingHandler,我需要将这些日志写入谷歌 Stackdriver,我有谷歌云日志依赖添加到我的应用程序和网络服务器中,但这没有帮助。

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-logging</artifactId>
  <version>3.0.1</version>
</dependency>

修改入口品脱

runtime: java11
entrypoint: 'java -cp "*" com.jettymain.Main webapp.war -Djava.util.logging.config.file=WEB-INF/logging.properties'

logging.properties 文件,它是可以在此处找到的示例文件以及一些额外的东西

# To use this configuration, add to system properties : -Djava.util.logging.config.file="/path/to/file"
.level = INFO

# it is recommended that io.grpc and sun.net logging level is kept at INFO level,
# as both these packages are used by Cloud internals and can result in verbose / initialization problems.
io.grpc.netty.level=INFO
sun.net.level=INFO

handlers=com.google.cloud.logging.LoggingHandler
# default : java.log
com.google.cloud.logging.LoggingHandler.log=custom_log

# default : INFO
com.google.cloud.logging.LoggingHandler.level=FINE

# default : ERROR
com.google.cloud.logging.LoggingHandler.flushLevel=EMERGENCY

# default : auto-detected, fallback "global"
com.google.cloud.logging.LoggingHandler.resourceType=container

# custom formatter
com.google.cloud.logging.LoggingHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s

# Level for all logs coming from GWT client (can't filter by specific classes on client)
com.google.gwt.logging.server.RemoteLoggingServiceUtil.level = WARNING
com.beoui.geocell.level=WARNING

标签: javagoogle-app-enginejettyjava.util.logginggoogle-cloud-logging

解决方案


进入System.setProperty任何方法。它也可以是主要方法。我认为问题是根据此链接the logging.properties“必须进入”,而不是代码中提到的目录。WEB-INF/classes directoryWEB-INF/

System.setProperty("java.util.logging.config.file", "WEB-INF/classes/logging.properties")

推荐阅读