For some circumstances I need to force flushing in logback's file appender immediately. I've found in docs this option is enabled by default. Mysteriously this doesn't work. As I see in the sources underlying process involves BufferedOutputSream
correctly. Is there any issues with BufferedOutputSream.flush()
? Probably this is rather related to the flushing issue.
Update: I found the issue on Windows XP Pro SP 3 and on Red Hat Enterprise Linux Server release 5.3 (Tikanga). I used these libs:
jcl-over-slf4j-1.6.6.jar
logback-classic-1.0.6.jar
logback-core-1.0.6.jar
slf4j-api-1.6.6.jar
The logback.xml
is:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/somepath/file.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>file.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE"/>
</root>
</configuration>
Updated: I'd provide a unit test but that seems not so simple. Let me describe the issue more clearly.
- Event of logging occurred
- Event is passed into file appender
- Event is serialized with defined pattern
- Serialized message of event is passed to the file appender and is about to write out to output stream
- Writing to the stream is finished, output stream is flushed (I've
checked the implementation). Note that
immidiateFlush
is true by default so methodflush()
is invoked explicitly - No result in the file!
A bit later when some underlying buffer was flowed the event appears in the file. So the question is: does output stream guarantee immediate flush?
To be honest I've already solve this by implementing my own ImmediateRollingFileAppender
that leverages facility of FileDescriptor
of immediate syncing. Anybody interested in can follow this.
So this is not a logback issue.