| Author |
Inserting Header Text in Log file - Log4j
|
Srinath Kannan
Greenhorn
Joined: Jan 11, 2011
Posts: 14
|
|
Hi,
I have semi-colon separated values logged to a csv file using log4j. Now i wanted to write a header in first line [only once, everyday]. How this can be done?
Example
message id, file name, start time, end time, status
MSGID1000;SAMPLE.TXT;01:05:55;07:08:44;OK
MSGID1000;SAMPLE.TXT;01:05:55;07:08:44;NOK
|
Regards
Srinath
|
 |
Joshua Smith
Ranch Hand
Joined: Aug 22, 2005
Posts: 192
|
|
Extend PatternLayout and then override the getHeader() method.
Josh
|
Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
|
 |
Srinath Kannan
Greenhorn
Joined: Jan 11, 2011
Posts: 14
|
|
Hi Josh,
I extended the PatternLayout class and implemented the header. It works.
One change, we have to append line.separator character at the end of the string returned by overidden getHeader() method. if we dont append it, the first line logged comes along with the header line.
Thankyou very much.
|
 |
anjan singh
Greenhorn
Joined: Feb 04, 2012
Posts: 3
|
|
Hi
How can I read the header text dynamically rather than hardcoding it in the "MyPatternLayout " class itself.
The situation is somewhat like this:
I have 50 files on which i need headers. Each file will have unique header.
How can I get this done by just writing one "MyPatternLayout " class and passing it the header text as an argument. Most preferably the header text could be done from log4j.properties itself.
Any ideas would be greatly appreciated...
|
 |
anjan singh
Greenhorn
Joined: Feb 04, 2012
Posts: 3
|
|
I figured it out.
it was very simple. Find below the code.
Just adding a setter for header does the trick.
And in log4j.properties file add header property to your layout:
|
 |
Ingoba Ningthoujam
Ranch Hand
Joined: Dec 04, 2006
Posts: 90
|
|
|
But When the server restart again, the header is added again. Is there a way to avoid to insert the header again until the rolling criteria is made?
|
 |
xie brandon
Greenhorn
Joined: Jul 22, 2012
Posts: 1
|
|
Ingoba Ningthoujam wrote:But When the server restart again, the header is added again. Is there a way to avoid to insert the header again until the rolling criteria is made?
i have the same question,any methods to solve?
|
 |
 |
|
|
subject: Inserting Header Text in Log file - Log4j
|
|
|