简体   繁体   English

将 Logback appender 与标准 App Engine Java 11 一起用于 Cloud Logging 时出现 NullPointerException

[英]NullPointerException when using Logback appender for Cloud Logging with standard App Engine Java 11

I've followed this guide to write my application logs to Cloud Logging using the logback appender.我已按照本指南使用 logback appender 将我的应用程序日志写入 Cloud Logging。 My app is a Jetty web-server which I run by adding the following entry-point to my app.yaml file:我的应用程序是一个 Jetty 网络服务器,我通过将以下入口点添加到我的 app.yaml 文件来运行它:

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

This is the added dependency in the pom file.这是在 pom 文件中添加的依赖项。

<dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-logging-logback</artifactId>
        <version>0.122.1-alpha</version>
</dependency>

This the logback.xml file located in src\\main\\resources:这是位于 src\\main\\resources 中的 logback.xml 文件:

<!-- [START logging_logback_config] -->
<configuration>
  <appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
    <log>application.log</log> <!-- Optional : default java.log -->
    <resourceType>gae_app</resourceType> <!-- Optional : default: auto-detected, fallback: global -->
    <flushLevel>WARN</flushLevel> <!-- Optional : default ERROR -->
  </appender>

  <root level="info">
    <appender-ref ref="CLOUD" />
  </root>
</configuration>
<!-- [END logging_logback_config] -->

This is how I'm using the logger:这就是我使用记录器的方式:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Quickstart {
  private static final Logger logger = LoggerFactory.getLogger(Quickstart.class);

  public static void main(String[] args) {
    logger.info("Logging INFO with Logback");
    logger.error("Logging ERROR with Logback");
  }
}

When I run the application this is the exception I get:当我运行应用程序时,这是我得到的异常:

ERROR in ch.qos.logback.core.joran.spi.Interpreter@23:14 - RuntimeException in Action for tag [appender] com.google.cloud.logging.LoggingException: java.lang.NullPointerException
    at com.google.cloud.logging.LoggingException: java.lang.NullPointerException
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingRpcFactory.create(LoggingOptions.java:63)
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingRpcFactory.create(LoggingOptions.java:55)
    at  at com.google.cloud.ServiceOptions.getRpc(ServiceOptions.java:560)
    at  at com.google.cloud.logging.LoggingOptions.getLoggingRpcV2(LoggingOptions.java:129)
    at  at com.google.cloud.logging.LoggingImpl.<init>(LoggingImpl.java:122)
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingFactory.create(LoggingOptions.java:46)
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingFactory.create(LoggingOptions.java:41)
    at  at com.google.cloud.ServiceOptions.getService(ServiceOptions.java:540)
    at  at com.google.cloud.logging.logback.LoggingAppender.getLogging(LoggingAppender.java:275)
    at  at com.google.cloud.logging.logback.LoggingAppender.start(LoggingAppender.java:236)
    at  at ch.qos.logback.core.joran.action.AppenderAction.end(AppenderAction.java:90)
    at  at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:309)
    at  at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:193)
    at  at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:179)
    at  at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)
    at  at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
    at  at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150)
    at  at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
    at  at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
    at  at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
    at  at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
    at  at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:417)
    at  at io.grpc.netty.shaded.io.netty.util.internal.logging.Slf4JLoggerFactory.<init>(Slf4JLoggerFactory.java:42)
    at  at io.grpc.netty.shaded.io.netty.util.internal.logging.InternalLoggerFactory.newDefaultFactory(InternalLoggerFactory.java:44)
    at  at io.grpc.netty.shaded.io.netty.util.internal.logging.InternalLoggerFactory.getDefaultFactory(InternalLoggerFactory.java:69)
    at  at io.grpc.netty.shaded.io.netty.util.internal.logging.InternalLoggerFactory.getInstance(InternalLoggerFactory.java:92)
    at  at io.grpc.netty.shaded.io.netty.util.internal.logging.InternalLoggerFactory.getInstance(InternalLoggerFactory.java:85)
    at  at io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent.<clinit>(PlatformDependent.java:80)
    at  at io.grpc.netty.shaded.io.netty.util.AsciiString.<init>(AsciiString.java:223)
    at  at io.grpc.netty.shaded.io.netty.util.AsciiString.<init>(AsciiString.java:210)
    at  at io.grpc.netty.shaded.io.netty.util.AsciiString.cached(AsciiString.java:1401)
    at  at io.grpc.netty.shaded.io.netty.util.AsciiString.<clinit>(AsciiString.java:48)
    at  at io.grpc.netty.shaded.io.grpc.netty.Utils.<clinit>(Utils.java:74)
    at  at io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder.<clinit>(NettyChannelBuilder.java:82)
    at  at io.grpc.netty.shaded.io.grpc.netty.NettyChannelProvider.builderForAddress(NettyChannelProvider.java:38)
    at  at io.grpc.netty.shaded.io.grpc.netty.NettyChannelProvider.builderForAddress(NettyChannelProvider.java:24)
    at  at io.grpc.ManagedChannelBuilder.forAddress(ManagedChannelBuilder.java:39)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:348)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.access$1900(InstantiatingGrpcChannelProvider.java:82)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider$1.createSingleChannel(InstantiatingGrpcChannelProvider.java:239)
    at  at com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:72)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:249)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:227)
    at  at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:205)
    at  at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:136)
    at  at com.google.cloud.logging.spi.v2.GrpcLoggingRpc.<init>(GrpcLoggingRpc.java:148)
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingRpcFactory.create(LoggingOptions.java:61)
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingRpcFactory.create(LoggingOptions.java:55)
    at  at com.google.cloud.ServiceOptions.getRpc(ServiceOptions.java:560)
    at  at com.google.cloud.logging.LoggingOptions.getLoggingRpcV2(LoggingOptions.java:129)
    at  at com.google.cloud.logging.LoggingImpl.<init>(LoggingImpl.java:122)
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingFactory.create(LoggingOptions.java:46)
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingFactory.create(LoggingOptions.java:41)
    at  at com.google.cloud.ServiceOptions.getService(ServiceOptions.java:540)
    at  at com.google.cloud.logging.LoggingHandler.getLogging(LoggingHandler.java:361)
    at  at com.google.cloud.logging.LoggingHandler.<init>(LoggingHandler.java:195)
    at  at com.google.cloud.logging.LoggingHandler.<init>(LoggingHandler.java:151)
    at  at com.google.cloud.logging.LoggingHandler.<init>(LoggingHandler.java:120)
    at  at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at  at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at  at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at  at org.eclipse.jetty.server.handler.ContextHandler$StaticContext.createInstance(ContextHandler.java:2881)
    at  at org.eclipse.jetty.servlet.ServletContextHandler$Context.createInstance(ServletContextHandler.java:1299)
    at  at org.eclipse.jetty.server.handler.ContextHandler$StaticContext.createListener(ContextHandler.java:2892)
    at  at org.eclipse.jetty.servlet.ListenerHolder.doStart(ListenerHolder.java:94)
    at  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
    at  at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:369)
    at  at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1449)
    at  at org.eclipse.jetty.maven.plugin.JettyWebAppContext.startWebapp(JettyWebAppContext.java:328)
    at  at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1414)
    at  at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:910)
    at  at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:288)
    at  at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:524)
    at  at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:397)
    at  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
    at  at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
    at  at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
    at  at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
    at  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
    at  at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
    at  at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
    at  at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
    at  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
    at  at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
    at  at org.eclipse.jetty.server.Server.start(Server.java:423)
    at  at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
    at  at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
    at  at org.eclipse.jetty.server.Server.doStart(Server.java:387)
    at  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
    at  at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:449)
    at  at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:310)
    at  at org.eclipse.jetty.maven.plugin.JettyRunWarExplodedMojo.execute(JettyRunWarExplodedMojo.java:59)
    at  at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
    at  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
    at  at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
    at  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at  at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at  at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at  at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at  at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at  at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at  at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at  at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
    at  at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at  at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    at  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at  at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at  at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at  at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at  at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at  at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
Caused by: java.io.IOException: java.lang.NullPointerException
    at  at com.google.cloud.logging.spi.v2.GrpcLoggingRpc.<init>(GrpcLoggingRpc.java:187)
    at  at com.google.cloud.logging.LoggingOptions$DefaultLoggingRpcFactory.create(LoggingOptions.java:61)
    at  ... 120 common frames omitted
Caused by: java.lang.NullPointerException
    at  at io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder$NettyTransportFactory.<init>(NettyChannelBuilder.java:682)
    at  at io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder.buildTransportFactory(NettyChannelBuilder.java:530)
    at  at io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder$NettyChannelTransportFactoryBuilder.buildClientTransportFactory(NettyChannelBuilder.java:188)
    at  at io.grpc.internal.ManagedChannelImplBuilder.build(ManagedChannelImplBuilder.java:621)
    at  at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:264)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:383)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.access$1900(InstantiatingGrpcChannelProvider.java:82)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider$1.createSingleChannel(InstantiatingGrpcChannelProvider.java:239)
    at  at com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:72)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:249)
    at  at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:227)
    at  at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:205)
    at  at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:136)
    at  at com.google.cloud.logging.spi.v2.GrpcLoggingRpc.<init>(GrpcLoggingRpc.java:148)

The easiest way to solve this issue would be to build a simple hello world application and try to achieve the grouping under the request logs.解决这个问题最简单的方法是构建一个简单的hello world应用程序,并尝试在请求日志下实现分组。

Refer to the link to achieve this.请参阅链接以实现此目的。

Here's the workaround on how to group the logs under the request logs :以下是如何在请求日志下对日志进行分组的解决方法:

In the main file SpringbootApplication.java :在主文件SpringbootApplication.java 中

there is the following method:有以下方法:

 @GetMapping("/")
public String hello(@RequestHeader(value = "x-cloud-trace-context") String traceId) {
String[] parts = traceId.split("/"); 
String trace = parts[0];  
MDC.put("logging.googleapis.trace", String.format("projects/%s/traces/%s", "YOUR_PROJECT_ID", trace));
logger.info("Logging INFO with Logback" + trace);
logger.error("Logging ERROR with Logback" + trace);  
return trace;

} }

I am getting the x-cloud-trace-context header by using spring mvc annotation and setting the first part of the trace in the MDC.我通过使用 spring mvc 注释并在 MDC 中设置跟踪的第一部分来获取 x-cloud-trace-context 标头。 You can either keep this method or use a http request filter to abstract this part away, instead of doing it in each method.您可以保留此方法或使用 http 请求过滤器将这部分抽象出来,而不是在每个方法中都这样做。 Also make sure to replace "YOUR_PROJECT_ID" with your actual projectid.还要确保将“YOUR_PROJECT_ID”替换为您的实际项目 ID。

The MDC property is then picked up by the TraceLoggingEventEnhancer defined in the logback.xml file which then makes sure that all logs show up under your request log. MDC 属性然后由 logback.xml 文件中定义的 TraceLoggingEventEnhancer 获取,然后确保所有日志都显示在您的请求日志下。

Note : Google has provided a fix as mentioned here .注意:Google 已提供了此处提到的修复程序。 Make sure you are using the latest version for google-cloud-core.确保您使用的是最新版本的 google-cloud-core。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 App Engine Cloud Endpoints框架(Java) - 在本地运行时出现NullPointerException - App Engine Cloud Endpoints Frameworks (Java) - NullPointerException when run locally 服务名称在 Google Cloud App Engine 标准 spring 启动 java 11 maven 部署中没有改变 - Service name isn't changing in Google Cloud App Engine standard spring boot java 11 maven deploy using app.yml configuration 更新到 App Engine Java 11 标准环境:Eclipse 中的项目方面 - Updating to App Engine Java 11 Standard Environment: Project Facets in Eclipse Google Cloud Memorystore通过“ App Engine Java标准环境”进行连接 - Google Cloud Memorystore connect from “App Engine Java Standard Environment” Logback追加程序与软件包不匹配,而是登录到控制台 - Logback appender not matching package, logging to console instead 如何在 java 标准(无弹簧)中为日志记录和日志文件构建 Logback? - How To Build Logback for logging and log file in java standard (without spring)? 通过 Logback 记录到 App Engine request_log - Logging to App Engine request_log through Logback 迁移到 Java 时出现 Logback 错误 11 - Logback Error Occurred when Migrating to Java 11 在Java中实施应用程序时,在Google App引擎的应用程序中记录事件 - Logging Events in Application in Google App engine when implementing application in Java Google Cloud App Engine - Eclipse 在 app.yaml 中显示“运行时:java 11”不是 Java - Google Cloud App Engine - Eclipse shows "runtime: java 11" in app.yaml is not Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM