Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid inserting headers to a file on Server Restart using log4j?

Tags:

java

log4j

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
like image 530
user001 Avatar asked Jun 06 '13 10:06

user001


1 Answers

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.

like image 130
user001 Avatar answered Oct 05 '22 03:10

user001