Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Logback Appender - Prepending file header and making it rollover

The functionality that I need is writing a header line at the beginning of the configured log file. The log file should, in addition, get rolled over based on a time pattern (I'm talking logback 1.0.7).

So, I'm thinking of writing an Appender - although I'm not sure whether it's not a custom Layout that I actually need.

1) Appender

Per logback's documentation, the right approach is to extend AppenderSkeleton, but then how would I combine this with the RollingFileAppender (to make the file rollover?)

On the other hand, if I extend RollingFileAppender, what method do I override to just decorate the existing functionality? How do I tell it to write that particular String only at the beginning of the file?

2) Layout

Analogously, the approach seems to be extending LayoutBase, and providing an implementation for doLayout(ILoggingEvent event). But again, I don't know how to just decorate the behaviour - just adding a new line in the file, rather than disrupting its functionality (because I still want the rest of the logs to show up properly).

The getFileHeader() in LayoutBase looks promising, but how do I use it? Is it even intended to be overridden by custom layouts? (probably yes, since it's part of the Layout interface, but then how?)

Thank you!

like image 523
teo Avatar asked Nov 08 '12 18:11

teo


People also ask

What is Logback RollingFileAppender?

Logback RollingFileAppender appends log events into a file with the capability to rollover ( archive the current log file and resume logging in a new file) based on a particular schedule, such as daily, weekly, monthly or based on log file size. For quick reference, this is configuration file we will be discussing further in the post.

How to extend the RollingFileAppender in Log4j 2?

The RollingFileAppender in Log4j 2.x is final, so you can not extend it. However you can obtain the functionality of your custom Log4j 1.x appender using:

What are the different types of custom Appenders for Logback?

The generic type is either ILoggingEvent or AccessEvent, depending on if we're using logback-classic or logback-access, respectively. Our custom appender should extend either AppenderBase or UnsynchronizedAppenderBase, which both implement Appender and handle functions such as filters and status messages.

What is the default rollover pattern for log files?

fileNamePattern: defines the name of the rolled-over (archived) log files. The rollover period is inferred from the date pattern specified in its value. The default pattern ‘yyyy-MM-dd’. maxHistory (optional): controls the maximum number of archive files to keep, asynchronously deleting older files.


1 Answers

Here I am answering my own question, just in case someone else comes across the same problem. This is how I eventually did it (don't know however if it's the orthodox way):

Instead of extending AppenderSkeleton, I extended RollingFileAppender (to keep the rollover functionality), and overrode its openFile() method. In here I could manipulate the log file and write the header in it, after letting it do whatever it needed to do by default. Like this:

 public void openFile(String fileName) throws IOException {
        super.openFile(fileName);
        File activeFile = new File(getFile());
        if (activeFile.exists() && activeFile.isFile() && activeFile.length() == 0) {
            FileUtils.writeStringToFile(activeFile, header);
        }
  }

I configured the header in logback.xml, as simple as this: <header> value </header>. This injects it in the header field of my new appender.

Seems to work without problems, but please do post if you know a better way!

like image 65
teo Avatar answered Oct 24 '22 14:10

teo