wood burning stoves 2.0*
The moose likes Other Application Frameworks and the fly likes Inserting Header Text in Log file - Log4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Inserting Header Text in Log file - Log4j" Watch "Inserting Header Text in Log file - Log4j" New topic
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: 193
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?
 
wood burning stoves
 
subject: Inserting Header Text in Log file - Log4j
 
Similar Threads
SECJ0306E: LTPA Token expiration, why? (WAS 5.1.1.4)
getting apache 2 to work with tomcat 4.1.27
Inserting Headers in log file using log4j
Log4j - Could not get Thread number in log
EJB30-JBOSS sample tutorial doesn't works!!