• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Log4J - Avoid inserting pattern on server restart

 
Ingoba Ningthoujam
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using LOG4J and write logs into a CSV file. I wrote my own pattern layout class to insert a header. But every time the server restarts, LOG4J inserted the header again and again. Please help to configure to insert the header once once until the rolling criteria is achieved even if the server restarts. Thanks in advanced.
 
Ingoba Ningthoujam
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please anybody help me. It is critical requirement.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the header to be written only when a new file is opened (because of date rollover or just because there was no log so far) then you will have to write some code which is only executed at that time. Right now I suppose you have code which is executed every time your server starts, which as you see isn't when you want the header to be written.

So that means you will have to write your own appender which works that way, I think. The appender is the code which knows when it's creating a new log file. But you don't have to write a whole appender. Writing a subclass of RollingFileAppender, or whatever you are already using in your configuration, would be the way to go. Check the API documentation to see what method you need to override (probably one with "roll" in its name, I would guess) and have a look at the source code if you aren't sure. And don't forget to change the configuration to use an instance of your new appender instead of whatever you are using now.

Basically your appender would override one of the existing appenders, and it would override one of its methods rather like this:


 
Ingoba Ningthoujam
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Paul. Your suggestion worked for me. Thanks Code Ranch.....
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! Glad my vague hand-waving worked out!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic