I am reading a header from a text file and adding it to file using log4j. The header is getting inserted successfully but it inserts header on every restart of my application, but I want the header to be inserted only when the file is created. I am using DailyRollingFileAppender, this would roll every midnight creating a new file.
I am overriding the getHeader method from PatternLayout class to add header to a file.
Can anyone help me out in inserting header only once (i.e. only when file is created)?
log4j.appender.ErrFileAppender=com.logger.ErrorFile
log4j.appender.ErrFileAppender.Threshold=WARN
log4j.appender.ErrFileAppender.File=${logdir}/Error.log
log4j.appender.ErrFileAppender.layout=com.logger.header.ErrHeader
log4j.appender.ErrFileAppender.DatePattern='.' yyyy-MM-dd
log4j.appender.ErrFileAppender.layout.ConversionPattern= %m%n
I figured out the solution. What I am doing is I am overriding the setFile() method of log4j.FileAppender and there I have added the logic of inserting header only when the file is empty.
This works perfectly. But I am not sure is there any better solution for the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With