How to add a header on my log files using logback?
<p>I have a class that I need to create a csv file.
So what I've done so far was that:</p>

<p>Created this class: </p>

<pre><code>public class MyPatternLayout extends PatternLayout {


    @Override
    public String getFileHeader() {
            return "message id, file name, start time, end time, status";
    }

}
</code></pre>

<p>And my logback file is configured like this:</p>

<pre><code>    &lt;appender name="METRICSTEST" class="ch.qos.logback.core.rolling.RollingFileAppender"&gt;
            &lt;file&gt;metricstest.csv&lt;/file&gt;
            &lt;append&gt;true&lt;/append&gt;
            &lt;rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"&gt;
                &lt;fileNamePattern&gt;metricstest-%d{yyyy-ww}.csv.zip&lt;/fileNamePattern&gt;
            &lt;/rollingPolicy&gt;
            &lt;encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"&gt;
                &lt;layout class="com.mdw360.actuator.log.layout.MyPatternLayout"&gt;
                    &lt;pattern&gt;%msg%n&lt;/pattern&gt;
                &lt;/layout&gt;
            &lt;/encoder&gt;
        &lt;/appender&gt;
&lt;logger name="com.mdw360.actuator.tasks.MetricsLoggingTask" level="INFO" additivity="false"&gt;
        &lt;appender-ref ref="METRICSTEST" /&gt;
    &lt;/logger&gt;
</code></pre>

<p>Well, what is happening to my right now is:</p>

<ol>
<li><p>On my log file the header is being shown twice. Seems like it tries to create the file twice or something like that because if I use timestamp on the file's name than I have 2 files created.</p></li>
<li><p>If I stop the application and start it again, more 2 headers are shown on my csv file.</p></li>
</ol>

<p>My log is looking like this:</p>

<pre><code>message id, file name, start time, end time, status
message id, file name, start time, end time, status
First Line
Second Line
Stop Application
message id, file name, start time, end time, status
message id, file name, start time, end time, status
First Line
Second Line
Stop Application
</code></pre>

<p>What do I need to do to fix this issue? I want to show the header in the beginning of each file only. If I stop and start it should identify there is already a header on the file and don't add it.</p>