开发者

Log4J: cannot change bufferSize with bufferedIO = true at FileAppender

I think I run into a problem using log4j. I'm trying to enable bufferedio, but the default buffer size of 8KB is too big 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="MyAppend开发者_开发百科er"/>
</logger>

I've tried different sizes for the buffer but the writing to the file only happens when the buffer reaches 8KB.

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

Enabling the Log4J debug shows that my appender has the proper buffersize, so I think that no one is overwriting my configuration.

Any idea of what am I doing wrong? Is it really a bug or limitation in Log4J?

Thanks.


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

Also as I know FileAppender is deprecated. WriterAppender is a closest replacement.


log4j 1.2.17

Dive to the source code, I find DailyRollingFileAppender use java.io.OutputStreamWriter to write characters to log file. Threre is a buffer in OutputStreamWriter. The size is 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 more than 8192 ( <=8192*2 ), there is no data in log file. The reason is the second cache(OutputStreamWriter) hold the first 8192 chars and DailyRollingFileAppender cache hold the remaining.

JDK API java.io.OutputStreamWriter:

Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜