vy / log4j2-logstash-layout

Log4j 2.x plugin for customizable and Logstash-friendly JSON layout.
Apache License 2.0
89 stars 26 forks source link

Caused by: java.lang.RuntimeException: failed serializing JSON #29

Closed chrissydee closed 5 years ago

chrissydee commented 5 years ago

Hi Volkan,

Since we use the JSONEventLayoutV0 of the Logstash implementation, I created my own template. Within the written log file, the JSON string is fragmented in many places and no longer valid. In our application logs I can see several exceptions. All exceptions refer to the used ByteBuffer.

log4j2.xml

<LogstashLayout dateTimeFormatPattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
  timeZoneId="UTC"
  eventTemplateUri="classpath:JSONEventLayoutV0.json"
  locationInfoEnabled="true"
  prettyPrintEnabled="false"
  stackTraceEnabled="true"/>

JSONEventLayoutV0.json

{
  "@version": null,
  "@message": "${json:message}",
  "@timestamp": "${json:timestamp}",
  "@source_host": "${hostName}",
  "@fields": {
    "exception": {
      "stacktrace": "${json:exception:stackTrace:text}",
      "exception_class": "${json:exception:className}",
      "exception_message": "${json:exception:message}"
    },
    "file": "${json:source:fileName}",
    "method": "${json:source:methodName}",
    "level": "${json:level}",
    "line_number": "${json:source:lineNumber}",
    "loggerName": "${json:logger:name}",
    "class": "${json:source:className}",
    "mdc": "${json:mdc}",
    "ndc": "${json:ndc}",
    "threadName": "${json:thread:name}"
  }
}

Stacktrace

org.apache.logging.log4j.core.appender.AppenderLoggingException: An exception occurred processing Appender RollingFileJson
        at org.apache.logging.log4j.core.appender.DefaultErrorHandler.error(DefaultErrorHandler.java:75) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.AppenderControl.handleAppenderError(AppenderControl.java:165) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:158) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170) ~[log4j-api-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125) ~[log4j-api-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108) ~[log4j-api-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007) ~[log4j-api-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866) ~[log4j-api-2.11.1.jar!/:2.11.1]
        at org.apache.logging.slf4j.Log4jLogger.debug(Log4jLogger.java:119) ~[log4j-slf4j-impl-2.11.1.jar!/:2.11.1]
        at com.mongodb.diagnostics.logging.SLF4JLogger.debug(SLF4JLogger.java:56) ~[mongo-java-driver-3.8.2.jar!/:?]
        at com.mongodb.internal.connection.LoggingCommandEventSender.sendStartedEvent(LoggingCommandEventSender.java:68) ~[mongo-java-driver-3.8.2.jar!/:?]
        at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:246) ~[mongo-java-driver-3.8.2.jar!/:?]
        at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83) ~[mongo-java-driver-3.8.2.jar!/:?]
        at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:38) ~[mongo-java-driver-3.8.2.jar!/:?]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:180) ~[mongo-java-driver-3.8.2.jar!/:?]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:124) ~[mongo-java-driver-3.8.2.jar!/:?]
        at java.lang.Thread.run(Thread.java:834) ~[?:?]
Caused by: java.lang.RuntimeException: failed serializing JSON
        at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:143) ~[log4j2-logstash-layout-0.16.jar!/:?]
        at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:34) ~[log4j2-logstash-layout-0.16.jar!/:?]
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:177) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.appender.RollingFileAppender.append(RollingFileAppender.java:309) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156) ~[log4j-core-2.11.1.jar!/:2.11.1]
        ... 23 more
Caused by: java.lang.IllegalArgumentException: newPosition > limit: (547 > 8192)
        at java.nio.Buffer.createPositionException(Buffer.java:318) ~[?:?]
        at java.nio.Buffer.position(Buffer.java:293) ~[?:?]
        at java.nio.ByteBuffer.position(ByteBuffer.java:1086) ~[?:?]
        at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:237) ~[?:?]
        at org.apache.logging.log4j.core.layout.ByteBufferDestinationHelper.writeToUnsynchronized(ByteBufferDestinationHelper.java:47) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:141) ~[log4j2-logstash-layout-0.16.jar!/:?]
        at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:34) ~[log4j2-logstash-layout-0.16.jar!/:?]
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:177) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.appender.RollingFileAppender.append(RollingFileAppender.java:309) ~[log4j-core-2.11.1.jar!/:2.11.1]
        at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156) ~[log4j-core-2.11.1.jar!/:2.11.1]
        ... 23 more
org.apache.logging.log4j.core.appender.AppenderLoggingException: An exception occurred processing Appender RollingFileJson
at org.apache.logging.log4j.core.appender.DefaultErrorHandler.error(DefaultErrorHandler.java:75) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.AppenderControl.handleAppenderError(AppenderControl.java:165) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:158) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.slf4j.Log4jLogger.log(Log4jLogger.java:376) ~[log4j-slf4j-impl-2.11.1.jar!/:2.11.1]
at org.slf4j.bridge.SLF4JBridgeHandler.callLocationAwareLogger(SLF4JBridgeHandler.java:221) ~[jul-to-slf4j-1.7.25.jar!/:1.7.25]
at org.slf4j.bridge.SLF4JBridgeHandler.publish(SLF4JBridgeHandler.java:303) ~[jul-to-slf4j-1.7.25.jar!/:1.7.25]
at java.util.logging.Logger.log(Logger.java:979) ~[?:?]
at java.util.logging.Logger.doLog(Logger.java:1006) ~[?:?]
at java.util.logging.Logger.logp(Logger.java:1283) ~[?:?]
at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:175) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.juli.logging.DirectJDKLog.error(DirectJDKLog.java:141) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:236) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:685) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[?:?]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.12.jar!/:9.0.12]
at java.lang.Thread.run(Thread.java:834) ~[?:?]
Caused by: java.lang.RuntimeException: failed serializing JSON
at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:143) ~[log4j2-logstash-layout-0.16.jar!/:?]
at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:34) ~[log4j2-logstash-layout-0.16.jar!/:?]
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:177) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.appender.RollingFileAppender.append(RollingFileAppender.java:309) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156) ~[log4j-core-2.11.1.jar!/:2.11.1]
... 35 more
Caused by: java.lang.IllegalArgumentException: newPosition > limit: (16384 > 8192)
at java.nio.Buffer.createPositionException(Buffer.java:318) ~[?:?]
at java.nio.Buffer.position(Buffer.java:293) ~[?:?]
at java.nio.ByteBuffer.position(ByteBuffer.java:1086) ~[?:?]
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:237) ~[?:?]
at org.apache.logging.log4j.core.layout.ByteBufferDestinationHelper.writeToUnsynchronized(ByteBufferDestinationHelper.java:43) ~[log4j-core-2.11.1.jar!/:2.11.1]
at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:141) ~[log4j2-logstash-layout-0.16.jar!/:?]
at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:34) ~[log4j2-logstash-layout-0.16.jar!/:?]
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:177) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.appender.RollingFileAppender.append(RollingFileAppender.java:309) ~[log4j-core-2.11.1.jar!/:2.11.1]
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156) ~[log4j-core-2.11.1.jar!/:2.11.1]
... 35 more
vy commented 5 years ago

Hey @chrissydee, thanks for the report. I will see if I can find time to work on it sometime this week. In the meantime, if you can create a narrowed down case that reproduces the problem for me, it would be really helpful.

chrissydee commented 5 years ago

Hey @vy, I adapt your LogstashLayoutDemo. Maybe you have to run the code multiple times before you see the wrong JSON lines. If you increase the num of log messages or threads, you can see your Exception from the LogstashLayout.class. Replace the log4j2.xml and add JSONEventLayoutV0.json too.

Thanks, for your fast reply.

Best,

Christian

log4j2-logstash-layout/layout-demo/src/main/java/com/vlkan/log4j2/logstash/layout/demo/LogstashLayoutDemo.java

package com.vlkan.log4j2.logstash.layout.demo;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class LogstashLayoutDemo {

    private static Logger LOGGER= LogManager.getLogger(LogstashLayoutDemo.class);

    private static String LOREM= "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut molestie in sapien vestibulum varius. Donec malesuada lacinia leo sit amet posuere. Praesent a odio ut felis elementum gravida ut id risus. Ut egestas et dolor at varius. Donec placerat interdum vulputate. Cras tellus dui, pretium lacinia feugiat in, mollis ut nulla. Praesent eu elementum nunc, eu elementum urna. Vestibulum malesuada vulputate nulla faucibus dapibus. In hac habitasse platea dictumst. Duis tempor suscipit leo in ornare.";
    private static int NUM_THREADS= 10;
    private static int NUM_LOGS_PER_THREAD= 15;

    public static void main(String[] args) {
        Runnable runnable= () -> {
            for (int i= 0; i < NUM_LOGS_PER_THREAD; i++) {
                String logMessage = generateRandomLogMessage();
                switch((new Random()).nextInt(2)) {
                    case 0:
                        LOGGER.info(logMessage);
                        break;
                    case 1:
                        LOGGER.debug(logMessage);
                        break;
                    default:
                        LOGGER.error(logMessage, new RuntimeException("Error!"));
                        break;
                }
            }
        };
        for (int i= 0; i < NUM_THREADS; i++) {
            Thread thread= new Thread(runnable);
            thread.start();
        }
    }

    public static String generateRandomLogMessage() {
        List<String> list= Arrays.asList(LOREM.split("\\s*?\\.\\s*"));
        Random rnd= new Random();
        return String.join(". ", list.subList(0, 1+Math.abs(rnd.nextInt(list.size()-1))));
    }

}

log4j2-logstash-layout/layout-demo/src/main/resources/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <LogstashLayout dateTimeFormatPattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
                            timeZoneId="UTC"
                            eventTemplateUri="classpath:JSONEventLayoutV0.json"
                            locationInfoEnabled="true"
                            prettyPrintEnabled="false"
                            stackTraceEnabled="true"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="DEBUG">
            <AppenderRef ref="CONSOLE"/>
        </Root>
    </Loggers>
</Configuration>

log4j2-logstash-layout/layout-demo/src/main/resources/JSONEventLayoutV0.json

{
  "@version": null,
  "@message": "${json:message}",
  "@timestamp": "${json:timestamp}",
  "@source_host": "${hostName}",
  "@fields": {
    "exception": {
      "stacktrace": "${json:exception:stackTrace:text}",
      "exception_class": "${json:exception:className}",
      "exception_message": "${json:exception:message}"
    },
    "file": "${json:source:fileName}",
    "method": "${json:source:methodName}",
    "level": "${json:level}",
    "line_number": "${json:source:lineNumber}",
    "loggerName": "${json:logger:name}",
    "class": "${json:source:className}",
    "mdc": "${json:mdc}",
    "ndc": "${json:ndc}",
    "threadName": "${json:thread:name}"
  }
}
vy commented 5 years ago

@chrissydee, I believe you can temporarily work around the issue by disabling TLA via log4j2.enable.threadlocals=false JVM property variable. In the meantime, I am checking the issue. (#27 is taking longer than I expected and sort of related with this issue.)

chrissydee commented 5 years ago

Hi @vy, changing the property log4j2.enable.threadlocals does not work. Even in my provided code example the JSON becomes invalid. I put a breakpoint at LogstashLayoutSerializationContexts

        boolean threadLocalEnabled = THREAD_LOCALS_ENABLED;
        return threadLocalEnabled
                ? createThreadLocalSupplier(jsonFactory, maxByteCount, prettyPrintEnabled, emptyPropertyExclusionEnabled)
                : createNewInstanceSupplier(jsonFactory, maxByteCount, prettyPrintEnabled, emptyPropertyExclusionEnabled);

to check if the property is used. Both supplier, the ThreadLocalSupplier and NewInstanceSupplier, has this issue.

By the way: the documentation of Log4J2 says, that the property is ineffective in web applications (see https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties).

This system property can be used to switch off the use of threadlocals, which will partly disable Log4j's garbage-free behaviour: to be fully garbage-free, Log4j stores objects in ThreadLocal fields to reuse them, otherwise new objects are created for each log event. Note that this property is not effective when Log4j detects it is running in a web application.

vy commented 5 years ago

@chrissydee, I think the fix I pushed for #27 addresses your issue as well via this change. If I am not mistaken, after having that synchronized block, I cannot reproduce the bug anymore. Would you mind testing and verifying the fix, please? In the meantime, I will try to write a unit test for that.

vy commented 5 years ago

@chrissydee, I have pushed LogstashLayoutConcurrentEncodeTest to bakomchik-fix-corrupted-generator-state branch. I also want to add your name to the list of contributors in the README. Shall I just write "chrissydee" or would you rather prefer your real name linked to your GitHub account?

vy commented 5 years ago

Merged the changes to master and cut a release. v0.18 should momentarily arrive in Maven Central.

JupellyRakesh commented 3 years ago

Hi @vy, Please check and confirm Issue still exist for new maven version(log4j2-logstash-layout

1.0.5) as well if we use logging as debug in springboot project **Error Message**: 2021-07-28 00:53:57,344 main ERROR An exception occurred processing Appender springFrameWorkLog java.lang.RuntimeException: failed serializing JSON at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:232) at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:58) at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:197) at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:190) at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:181) at org.apache.logging.log4j.core.appender.RollingFileAppender.append(RollingFileAppender.java:312) at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156) at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129) at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120) at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84) at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:540) at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:498) at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:481) at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:456) at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:82) at org.apache.logging.log4j.core.Logger.log(Logger.java:161) at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2205) at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2159) at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2142) at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1994) at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1852) at org.apache.commons.logging.LogAdapter$Log4jLog.log(LogAdapter.java:270) at org.apache.commons.logging.LogAdapter$Log4jLog.debug(LogAdapter.java:240) at org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener.logAutoConfigurationReport(ConditionEvaluationReportLoggingListener.java:126) at org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener.logAutoConfigurationReport(ConditionEvaluationReportLoggingListener.java:104) at org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener.onApplicationEvent(ConditionEvaluationReportLoggingListener.java:94) at org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener$ConditionEvaluationReportListener.onApplicationEvent(ConditionEvaluationReportLoggingListener.java:164) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:938) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) at com.blujaysolutions.springboot.LoggingInJsonApplication.main(LoggingInJsonApplication.java:22) Caused by: java.nio.BufferOverflowException at java.base/java.nio.HeapByteBuffer.put(HeapByteBuffer.java:235) at com.vlkan.log4j2.logstash.layout.util.ByteBufferOutputStream.write(ByteBufferOutputStream.java:49) at com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2177) at com.fasterxml.jackson.core.json.UTF8JsonGenerator._writeStringSegment2(UTF8JsonGenerator.java:1491) at com.fasterxml.jackson.core.json.UTF8JsonGenerator._writeStringSegment(UTF8JsonGenerator.java:1438) at com.fasterxml.jackson.core.json.UTF8JsonGenerator._writeStringSegments(UTF8JsonGenerator.java:1321) at com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeString(UTF8JsonGenerator.java:517) at com.vlkan.log4j2.logstash.layout.resolver.MessageResolver.resolveText(MessageResolver.java:63) at com.vlkan.log4j2.logstash.layout.resolver.MessageResolver.resolve(MessageResolver.java:54) at com.vlkan.log4j2.logstash.layout.resolver.MessageResolver.resolve(MessageResolver.java:29) at com.vlkan.log4j2.logstash.layout.resolver.TemplateResolvers.lambda$ofObjectNode$5(TemplateResolvers.java:146) at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:264) at com.vlkan.log4j2.logstash.layout.LogstashLayout.encode(LogstashLayout.java:222) ... 40 more
vy commented 3 years ago

@JupellyRakesh, log4j2-logstash-layout is superseded by log4j-template-layout-json. It shouldn't take much effort to migrate to that – replace the artifact name and adapt the template. Is migration to log4j-template-layout-json an option for you?