Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to produce a header row in log4j2

In log4j, we sometimes create a second logger and output CSV data to it as sort of a metrics watch of the application. This data is easily imported and graphed, as opposed to the normal log which we use for debugging and the like.

In old log4j we did this by subclassing the PatternLayout, overriding the header property, and specifying it as:

log4j.logger.servicePerformance=INFO, servicePerformance
log4j.appender.servicePerformance=mypackage.log4j.SingleHeaderFileAppender
log4j.appender.servicePerformance.File=logs/performance.log
log4j.appender.servicePerformance.layout=mypackage.log4j.HeaderLayout
log4j.appender.servicePerformance.layout.ConversionPattern=%m%n
log4j.appender.servicePerformance.layout.Header=Start Time, Service Invoked, Elapsed ms, Candidate Count, Asset Count
log4j.additivity.servicePerformance=false

I'm not sure how to do this in log4j 2 which we now have two applications recently converted to use.

like image 851
Entropy Avatar asked Sep 07 '25 01:09

Entropy


1 Answers

Log4j2's PatternLayout also supports the header and footer attributes:

<PatternLayout>
  <pattern>%d %p [%t] %c %m%n</pattern>
  <header>This is a header</header>
  <footer>(c) 1999-2014</footer>
</PatternLayout>

I think attributes work too:

<PatternLayout pattern="%d %p [%t] %c %m%n"
  header="This is a header"
  footer="(c) 1999-2014" />

This is not documented correctly, thanks for pointing this out.

like image 81
Remko Popma Avatar answered Sep 10 '25 12:09

Remko Popma