aspose file tools*
The moose likes Other Open Source Projects and the fly likes Log4J - Avoid inserting pattern on server restart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Log4J - Avoid inserting pattern on server restart " Watch "Log4J - Avoid inserting pattern on server restart " New topic
Author

Log4J - Avoid inserting pattern on server restart

Ingoba Ningthoujam
Ranch Hand

Joined: Dec 04, 2006
Posts: 90
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

Joined: Dec 04, 2006
Posts: 90
Please anybody help me. It is critical requirement.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18988
    
    8

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

Joined: Dec 04, 2006
Posts: 90
Thank you so much Paul. Your suggestion worked for me. Thanks Code Ranch.....
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18988
    
    8

Excellent! Glad my vague hand-waving worked out!
 
 
subject: Log4J - Avoid inserting pattern on server restart