Custom Logback Appender - Prepending file header and making it rollover
Asked Answered
T

2

8

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!

Trihedral answered 8/11, 2012 at 18:21 Comment(0)
T
11

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!

Trihedral answered 17/11, 2012 at 0:8 Comment(1)
@Trihedral do you know how can we get custom property defined in logback.xml in start method of any custom Appender class ?Tolentino
X
5

Your solution has a problem: it deletes the first line of log of each new file. I think this is because you write the header whereas the file is open by logback. I've found another solution that does not have this problem:

public void openFile(String fileName) throws IOException
{
    super.openFile(fileName);
    File activeFile = new File(getFile());
    if (activeFile.exists() && activeFile.isFile() && activeFile.length() == 0)
    {
        lock.lock();
        try
        {
            new PrintWriter(new OutputStreamWriter(getOutputStream(), StandardCharsets.UTF_8), true).println("your header");
        }
        finally
        {
            lock.unlock();
        }
    }
}
Xanthene answered 19/2, 2015 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.