Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend PatternLayout in Log4j2

Tags:

java

log4j2

Since Log4J2's org.apache.logging.log4j.core.layout.PatternLayout Class is final, I can't extend it to create headers for my CSV.

I referred docs, it doesn't provide info on how to extend existing Layout. http://logging.apache.org/log4j/2.x/manual/extending.html#Layouts

Previous approach of extending PatternLayout doesn't work.

My goal is to add header to log file & headers should be configured through configuration file for below log,

<File name="MttPerformance" fileName="logs\MyLog.csv" append="true">
    <PatternLayout pattern="%d{dd MMM yyyy},%d{HH:mm:ss,SSS},%m%n"/>
</File>
like image 356
Tejas jain Avatar asked Sep 15 '25 11:09

Tejas jain


1 Answers

<PatternLayout pattern="%d{dd MMM yyyy},%d{HH:mm:ss,SSS},%m%n" header="Date,Time,Inf,Time(ms)${sys:line.separator}"/>

Log4j2 has header tag..! https://logging.apache.org/log4j/2.x/manual/layouts.html

like image 151
Tejas jain Avatar answered Sep 17 '25 00:09

Tejas jain