Log4J: cannot change bufferSize with bufferedIO = true in FileAppender

I think I have a problem with log4j. I am trying to enable bufferedio, but the default buffer size of 8K is too large for my current needs.

<appender name="MyAppender" class="org.apache.log4j.FileAppender"> <param name="bufferedIO" value="true"/> <param name="bufferSize" value="512"/> <param name="Append" value="true"/> <param name="File" value="C:/MyMonitor.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{dd/MM/yyyy HH:mm:ss}|%m%n"/> </layout> </appender> <logger name="com.mypackage.MyMonitor"> <level value="debug"/> <appender-ref ref="MyAppender"/> </logger> 

I tried different sizes for the buffer, but writing to the file occurs only when the buffer reaches 8 KB.

I cannot find an error in Log4J describing this problem, even when I could find another guy who had the same problem (without any solution).

Turning on Log4J debugging shows that my appender has proper buffering, so I think no one overwrites my configuration.

Any idea what I'm doing wrong? Is this really a bug or limitation in Log4J?

Thanks.

+4
source share
2 answers

I noticed that you wrote bufferedIO starting with lowercase and BufferSize starting with capital. Java is case-sensitive by default, so please make all your parameters look like in the constructor (bufferSize must be reduced).

Also, as I know, FileAppender is deprecated. WriterAppender is the closest replacement.

+1
source

log4j 1.2.17

Dive into the source code, I find the DailyRollingFileAppender to use java.io.OutputStreamWriter to write characters to the log file. Threre is a buffer in OutputStreamWriter . Size 8k (8192) in jdk 1.7

Because of this second cache, you will find: if you set log4j.appender.file.bufferSize = 8192 , even the log data is greater than 8192 (<= 8192 * 2), there is no data in the log file. The reason is that the second cache (OutputStreamWriter) contains the first 8192 characters, and the DailyRollingFileAppender cache contains the remaining.

JDK API java.io.OutputStreamWriter :

Each call to the write () method forces the encoder to be converted to the specified character (s). The resulting bytes are accumulated in the buffer before being written to the base output stream. The size of this buffer can be specified, but by default it is large enough for most purposes. Note that characters passed to write () methods are not buffered.

0
source

All Articles