Description
When logging messages larger than FastFileManager.DEFAULT_BUFFER_SIZE with appenders using FastFileManager, the logging fails internally with a BufferOverflowException and no log output is produced by these appenders.
Test case
import java.util.Arrays; import org.apache.logging.log4j.LogManager; public class TestBufferOverflow { public static void main(String[] args) { final String longMessage; { char[] chars = new char[384 * 1024]; Arrays.fill(chars, 'x'); longMessage = new String(chars); } LogManager.getLogger("TestBufferOverflow").info(longMessage); } }
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appenders> <FastFile name="file" fileName="test.log" suppressExceptions="false"> <PatternLayout> <pattern>%m%n</pattern> </PatternLayout> </FastFile> </appenders> <loggers> <root level="info"> <appender-ref ref="file" /> </root> </loggers> </configuration>
Exception thrown
Exception in thread "main" org.apache.logging.log4j.core.appender.AppenderRuntimeException: An exception occurred processing Appender file at org.apache.logging.log4j.core.appender.DefaultErrorHandler.error(DefaultErrorHandler.java:73) at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:104) at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:424) at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:405) at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:366) at org.apache.logging.log4j.core.Logger.log(Logger.java:110) at org.apache.logging.log4j.spi.AbstractLogger.info(AbstractLogger.java:1010) at TestBufferOverflow.main(TestBufferOverflow.java:16) Caused by: java.nio.BufferOverflowException at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:165) at org.apache.logging.log4j.core.appender.FastFileManager.write(FastFileManager.java:89) at org.apache.logging.log4j.core.appender.OutputStreamManager.write(OutputStreamManager.java:129) at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:113) at org.apache.logging.log4j.core.appender.FastFileAppender.append(FastFileAppender.java:84) at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:102) ... 6 more