Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://docs.oracle.com/javase/6/docs/api/java/io/OutputStream.html#flush%28%29">javadoc</a> and generally shouldn't be an issue until you are faced with some old school integration solutions over the file syncing.</p> <p>So this is a logback appender implemented to flush immediately:</p> <pre><code>public class ImmediateFileAppender&lt;E&gt; extends RollingFileAppender&lt;E&gt; { @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); } } </code></pre> <p>This is corresponding output stream utility class. Because of some methods and fields of original <code>ResilientOutputStreamBase</code> that was supposed for extending initially have packaged access modifiers I had to extend <code>OutputStream</code> instead and just copy the rest and unchanged of <code>ResilientOutputStreamBase</code> and <code>ResilientFileOutputStream</code> to this new one. I just display the changed code:</p> <pre><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); } } } } </code></pre> <p>And finally the config:</p> <pre><code>&lt;appender name="FOR_INTEGRATION" class="package.ImmediateFileAppender"&gt; &lt;file&gt;/somepath/for_integration.log&lt;/file&gt; &lt;rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"&gt; &lt;fileNamePattern&gt;for_integration.log.%i&lt;/fileNamePattern&gt; &lt;minIndex&gt;1&lt;/minIndex&gt; &lt;maxIndex&gt;3&lt;/maxIndex&gt; &lt;/rollingPolicy&gt; &lt;triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"&gt; &lt;maxFileSize&gt;5MB&lt;/maxFileSize&gt; &lt;/triggeringPolicy&gt; &lt;encoder&gt; &lt;pattern&gt;%d{HH:mm:ss.SSS} - %msg%n&lt;/pattern&gt; &lt;immediateFlush&gt;true&lt;/immediateFlush&gt; &lt;/encoder&gt; &lt;/appender&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload