I'm using log4j2, and running multiple instances of the same code in different processes (ie different JVMs) at the same time. I'd like all processes to log to the same file, interleaved How can I configure (via log4j2.xml) to output the PID, so that the different processes can be distinguished in the logs?
Asked
Active
Viewed 1.3k times
10
-
I would be surprised if you can log from multiple JVMs to the same file. Happy to be proven wrong. Chronicle Logger does this, but it's not trivial to implement. – Peter Lawrey Sep 10 '14 at 00:29
-
@PeterLawrey - Doing it right now, works fine out of the box. The log lines are interleaved in the file. – SRobertJames Sep 10 '14 at 01:32
-
Are you asking how your Java program can get its PID, so it can include it in log mrssages you generate. Or are you asking how to configure log4j so *it* inserts the PID in log messages? – Raedwald Sep 10 '14 at 07:04
-
I agree with Peter. This may (seem to) work but may turn out to be fragile if you do things like rollover. Log4j2 (as of v2.0.2) is not designed for this usage. – Remko Popma Sep 10 '14 at 09:04
-
@Raedwald - Yes, how to configure log4j so that _it_ inserts the PID, or some other way of identifying the process, in the log message – SRobertJames Sep 10 '14 at 18:02
-
@RemkoPopma It's okay if the log doesn't handle rollover – SRobertJames Sep 10 '14 at 18:03
-
Rather than plaving clarifications in comments, edit your question so it is clearer. – Raedwald Sep 10 '14 at 19:53
-
Interesting to hear this works. Are you using FileAppender or RandomAccessFileAppender? – Remko Popma Sep 11 '14 at 00:27
4 Answers
11
There is a plugin ProcessIdPatternConverter in log4j2-core since version 2.9 that does exactly this.
just setting %pid or %processId in the pattern layout logs it.
log4j documentation: https://logging.apache.org/log4j/2.x/manual/layouts.html

Ricard Nàcher Roig
- 1,271
- 12
- 14
-
Adding %pid is printing the level info since the meta character for level info is "%p" so if i use %pid it prints "ERRORid" rather than printing the process id. Any alternatives for this? – arunb2w Aug 27 '18 at 05:27
-
-
Works as expected. Tested with version 2.14.1 and %processId dumps the process id of the process. – apatniv Mar 16 '21 at 23:11
9
Perhaps MDC can help you. Try this:
Java:
import java.lang.management.ManagementFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
public class TestPID {
private static final Logger LOG = LogManager.getLogger(TestPID.class);
static {
// Get the process id
String pid = ManagementFactory.getRuntimeMXBean().getName().replaceAll("@.*", "");
// MDC
ThreadContext.put("pid", pid);
}
public static void main(String[] args) {
LOG.info("Testing...");
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %5X{pid} %-5p %c#%M - %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Output:
2014-09-10 00:13:49,281 7164 INFO TestPID#main - Testing...
↑↑↑↑
That's the PID
You may want to see:

Community
- 1
- 1

Paul Vargas
- 41,222
- 15
- 102
- 148
1
If you are using spring framework, you just need to append " ${PID}" inside pattern value, that's it. No need to add a new function to fetch your processId as mentioned in other answers.

Priyanka Khatri
- 11
- 1
-
It would be helpful if you provided detailed example of configuration with ${PID} variable. – Petr Aleksandrov Nov 29 '20 at 22:35
1
log4j.appender.consoleAppender.layout.ConversionPattern=%-25d %-5p ${PID} [%-10t] %c{3} - %m%n
resulted in
2021-03-23 11:42:57.281 INFO 896 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/v1] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2021-03-23 11:43:02,766 INFO 896 [http-nio-8080-exec-4] springboot.controller.MyController - greeting(/v1)

Visv M
- 431
- 4
- 13