I currently have the below pattern layout in log4j. I want to add the Process id to the log file. How can I do it?
log4j.appender.A1.layout=org.apache.log4j.Pa
You should use MDC to do it
In the config file :
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p (%c) %m%n %X{PID}
%X{PID}
is used to match the context value PID
And then, in the code, before the logging begins :
log4j 1.x
RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
String pid = rt.getName();
MDC.put("PID", pid);
log4j 2.x
RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
String pid = rt.getName();
ThreadContext.put("PID", pid);