Logback: Logging Exceptions with null message cause NullPointerException thrown by LoggingAppender and no log issued #248
Labels
api: logging
Issues related to the googleapis/java-logging-logback API.
priority: p2
Moderately-important priority. Fix may not be included in next release.
type: bug
Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Hello,
we're using the latest java-logging-logback library for our Google Cloud infrastructure with the LogbackAppender that appends on Stackdriver.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-logging-logback</artifactId>
<version>0.118.5-alpha</version>
</dependency>
When logging particular Exceptions with a null message the class com.google.cloud.logging.logback.LoggingAppender throws a NullPointerException and the log is not printed on the appender.
Putting the logback configuration in debug mode, on stdout an appender Exception is printed but the previously logged Exception is lost.
Steps to reproduce
<configuration debug="true">
...
<appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
<!-- Optional : filter logs at or above a level -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<log>test</log>
<flushLevel>DEBUG</flushLevel>
</appender>
19:33:00,377 |-ERROR in com.google.cloud.logging.logback.LoggingAppender[CLOUD] - Appender [CLOUD] failed to append. java.lang.NullPointerException
at java.lang.NullPointerException
at at java.base/java.lang.StringBuilder.(Unknown Source)
at at com.google.cloud.logging.logback.LoggingAppender.logEntryFor(LoggingAppender.java:294)
at at com.google.cloud.logging.logback.LoggingAppender.append(LoggingAppender.java:231)
at at com.google.cloud.logging.logback.LoggingAppender.append(LoggingAppender.java:79)
at at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:84)
at at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:48)
at at ch.qos.logback.classic.Logger.appendLoopOnAppenders(Logger.java:270)
at at ch.qos.logback.classic.Logger.callAppenders(Logger.java:257)
at at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:422)
at at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:384)
at at ch.qos.logback.classic.Logger.error(Logger.java:539)
at at com.test.logger.impl.Slf4jLogger.error(Slf4jLogger.java:140)
at at com.test.resources.PingResource.ping(PingResource.java:41)
at at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
Where the Issue is located
The issue can be found in the LoggingAppender source, in logEntryFor function (row 294):
At the first row, StringBuilder constructor throws NullPointerException because it expects a not-null string.
Fix suggestion
A way to fix the issue is to instantiate StringBuilder with no parameters then call append() passing the formatted message as append() is null safe.
Thank you for your attention!
The text was updated successfully, but these errors were encountered: