• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting Header Text in Log file - Log4j

 
Srinath Kannan
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joshua Smith
Ranch Hand
Posts: 193
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extend PatternLayout and then override the getHeader() method.





Josh
 
Srinath Kannan
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic