Extend PatternLayout in Log4j2

2019-05-18 22:09发布

问题:

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>

回答1:

<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



标签: java log4j2