This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Inserting Header Text in Log file - Log4j

 
Greenhorn
Posts: 14
Eclipse IDE Tomcat Server Java
  • 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
 
Ranch Hand
Posts: 193
  • 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 Tomcat Server Java
  • 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.

 
Greenhorn
Posts: 3
  • 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
  • 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:

 
Ranch Hand
Posts: 90
  • 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?
 
Greenhorn
Posts: 1
  • 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?
 
See where your hand is? Not there. It's next to this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!