Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:49:11+00:00 2026-06-09T14:49:11+00:00

For some circumstances I need to force flushing in logback’s file appender immediately. I’ve

  • 0

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.

  1. Event of logging occurred
  2. Event is passed into file appender
  3. Event is serialized with defined pattern
  4. Serialized message of event is passed to the file appender and is
    about to write out to output stream
  5. Writing to the stream is finished, output stream is flushed (I’ve
    checked the implementation). Note that immidiateFlush is true by
    default so method flush() is invoked explicitly
  6. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:49:12+00:00Added an answer on June 9, 2026 at 2:49 pm

    I decided to bring my solution to everybody.
    Let me clarify first of all that this is not a logback issue and not a JRE problem. This is described in the javadoc and generally shouldn’t be an issue until you are faced with some old school integration solutions over the file syncing.

    So this is a logback appender implemented to flush immediately:

    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);
        }
    
    }
    

    This is corresponding output stream utility class. Because of some methods and fields of original ResilientOutputStreamBase that was supposed for extending initially have packaged access modifiers I had to extend OutputStream instead and just copy the rest and unchanged of ResilientOutputStreamBase and ResilientFileOutputStream to this new one. I just display the changed code:

    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);
                }
            }
        }
    
    }
    

    And finally the config:

    <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Under some circumstances I need to be able to tear down all my activities
I've found myself with a need to move some functionality into the service layer.
Under what exact circumstances do @SessionAttributes get cleared? I've discovered some confusing behaviour when
Some setup background first: I have a cronjob which runs a PHP file called
Some reasons i am experiencing this issue on my website. When the website loads
Some related questions I found from searching: How does the 'fx' queue in jquery
Some context for this code, what I am trying to do is create a
I need to serialize some data in a binary format for efficiency (datalog where
I've got a UINavigationController with a series of UIViewControllers on it. Under some circumstances,
I'm using the following piece of code and under some mysterious circumstances the result

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.