Logback文件appender不会立即刷新

Vik*_*bin 16 java io logging flush logback

在某些情况下,我需要立即强制刷新logback的文件appender.我在docs中发现默认情况下启用此选项.神秘地说这不起作用.正如我在源代码中看到的过程涉及BufferedOutputSream正确.有什么问题BufferedOutputSream.flush()吗?可能这与冲洗问题有关.

更新:我在Windows XP Pro SP 3和Red Hat Enterprise Linux Server 5.3版(Tikanga)上发现了这个问题.我使用了这些库:

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
Run Code Online (Sandbox Code Playgroud)

logback.xml方法是:

<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>
Run Code Online (Sandbox Code Playgroud)

更新: 我提供单元测试,但似乎不那么简单.让我更清楚地描述这个问题.

  1. 发生了记录事件
  2. 事件被传递到文件appender
  3. 事件使用定义的模式序列化
  4. 事件的序列化消息被传递给文件appender并且即将写出到输出流
  5. 写入流完成后,输出流被刷新(我已经检查了实现).请注意,immidiateFlush默认情况下为true,因此flush()显式调用方法
  6. 没有结果文件!

稍后当一些底层缓冲区流动时,事件出现在文件中.所以问题是:输出流是否保证立即刷新?

说实话,我已经通过实现我自己的ImmediateRollingFileAppender利用FileDescriptor立即同步的工具来解决这个问题.任何有兴趣的人都可以这样做.

所以这不是一个回归问题.

Vik*_*bin 9

我决定把我的解决方案带给每个人.首先让我澄清一下,这不是一个回滚问题而不是JRE问题.这在javadoc中有所描述,在您通过文件同步面对一些旧的学校集成解决方案之前,通常不应该是一个问题.

所以这是一个实现立即刷新的logback appender:

public class ImmediateFileAppender<E> extends RollingFileAppender<E> {

    @Override
    public void openFile(String file_name) throws IOException {
        synchronized (lock) {
            File file = new File(file_name);
            if (FileUtil.isParentDirectoryCreationRequired(file)) {
                boolean result = FileUtil.createMissingParentDirectories(file);
                if (!result) {
                    addError("Failed to create parent directories for [" + file.getAbsolutePath() + "]");
                }
            }

            ImmediateResilientFileOutputStream resilientFos = new ImmediateResilientFileOutputStream(file, append);
            resilientFos.setContext(context);
            setOutputStream(resilientFos);
        }
    }

    @Override
    protected void writeOut(E event) throws IOException {
        super.writeOut(event);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是相应的输出流实用程序类.因为一些方法和原来的领域ResilientOutputStreamBase本来应该用于扩展最初都打包访问修饰符我不得不延长OutputStream,而不是和刚才复制的其余部分不变,ResilientOutputStreamBaseResilientFileOutputStream以这个新的.我只显示更改的代码:

public class ImmediateResilientFileOutputStream extends OutputStream {

    // merged code from ResilientOutputStreamBase and ResilientFileOutputStream

    protected FileOutputStream os;

    public FileOutputStream openNewOutputStream() throws IOException {
        return new FileOutputStream(file, true);
    }

    @Override
    public void flush() {
        if (os != null) {
            try {
                os.flush();
                os.getFD().sync(); // this's make sence
                postSuccessfulWrite();
            } catch (IOException e) {
                postIOFailure(e);
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

最后配置:

<appender name="FOR_INTEGRATION" class="package.ImmediateFileAppender">
    <file>/somepath/for_integration.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>for_integration.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} - %msg%n</pattern>
        <immediateFlush>true</immediateFlush>
    </encoder>
</appender>
Run Code Online (Sandbox Code Playgroud)