log4j - outputting header twice for RollingFileAppender

C

crazy fo sheezy

Hi. I have a RollingFileAppender where I’ve created a custom layout.
And, in the custom layout, all I’m doing is adding a header row to my
log file (see below).

public class NoMatchingTranscriptPatternLayout extends PatternLayout {

public NoMatchingTranscriptPatternLayout() {
super();
}

public String getHeader() {
return (new String("Date of Download\tPdf\n"));
}
}

Now, I have a job that’s scheduled to run on a daily basis so the log
files get updated daily. However, when the log file gets updated, the
header gets outputted to the log file each time the job is run. I
have log4j version 1.2.15. Is this a bug? Shouldn’t the header only
get generated if the file is new? Please advise.

Thanks
 
N

Noel

Hi.  I have a RollingFileAppender where I’ve created a custom layout.
[...]
Now, I have a job that’s scheduled to run on a daily basis so the log
files get updated daily.  However, when the log file gets updated, the
header gets outputted to the log file each time the job is run.  I
have log4j version 1.2.15.  Is this a bug?  Shouldn’t the header only
get generated if the file is new?  Please advise.

Are you instantiating a new RollingFileAppender every time the program
runs (e.g., the program itself is re-instantiated for every
execution) ?

A RollingFileAppender / FileAppender will write out the header string
to its file every time it is instantiated.
 
C

crazy fo sheezy

I'm not sure if a new one gets instantiated each time the program gets
run. I know for sure I'm not manually instantiating it because all of
the configuration is in a properties file.

So, how do people get around this problem? In the getHeader() method,
are you checking to see if the log already exists and then returning
null if it does?
 
C

crazy fo sheezy

I took the route of subclassing RollingFileAppender, like you had
said. And, it seems to work:

public class MyAppender extends RollingFileAppender {

protected void writeHeader() {
File f = new File(getFile());
if (f.length() == 0) {
//System.out.println("size of log file = " + f.length());
qw.write(getLayout().getHeader());
} else
System.out.println("log file already exists");

}
}

Thanks for the suggestion!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top