22

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.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

Pasted sample log message

2011-01-07 11:48:21,940 [main] INFO  Testing1
2011-01-07 11:48:21,942 [main] INFO  Test.common.ApplicationProperties - Used log4j 

log4j.properties
"log4j.properties" [Read only] 26 lines, 884 characters
log4j.rootCategory=DEBUG, stdout, A1

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=WARN
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss}  %-5p  (%c) %m%n


log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.Threshold=DEBUG
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.A1.File=/homw/cus/logs/ccl.02.log
log4j.appender.A1.MaxFileSize=5MB
log4j.appender.A1.MaxBackupIndex=40


log4j.category.test.common.DBConnectionPool=WARN
log4j.category.test.common.DataBaseHandler=WARN
log4j.category.test.cttg.tables=WARN
log4j.category.test.middleware.tables=WARN

log4j.logger.org.apache.axis=ERROR
log4j.logger.org.apache.catalina=ERROR
ceving
  • 21,900
  • 13
  • 104
  • 178
Arav
  • 4,957
  • 23
  • 77
  • 123
  • 1
    possible duplicate of [How to log Process id using Log4cxx or log4j](http://stackoverflow.com/questions/4286089/how-to-log-process-id-using-log4cxx-or-log4j) – skiphoppy Aug 30 '12 at 17:16
  • Are you wanting to have the java process ID printed, or the current thread ID printed? – armstrhb Feb 01 '13 at 17:45

3 Answers3

22

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);
ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • I am trying to run your code in wso2 esb in script mediator like this but it is giving an error. importPackage(Packages.java.net); importPackage(java.lang.management); importPackage(Packages.org.apache.log4j); try { RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean(); String pid = rt.getName(); MDC.put("PID", pid); } } catch (e) { } – omer khalid Jan 15 '15 at 11:23
  • WSo2 EsB does not allow me to save the change. it gives a proxy admin exception,if i remove this script mediator and change something else, the service works fine. I think there is something wrong with code, maybe i am not including the right packages or "script" mediator takes "javascript" as input and this may be does not allowed in java script. – omer khalid Jan 15 '15 at 11:29
  • I don't know at all WSo2 EsB. In a regular java file, here are the import : `import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import org.apache.log4j.MDC;` – ToYonos Jan 15 '15 at 11:35
  • its not that , i have to use javascript to pick the value, then use it for logging because there is not pre defined log4j specifier to pick up the pid... – omer khalid Jan 15 '15 at 12:22
  • 2
    @omerkhalid I think you should create a question of your own, specifying exactly what you are doing and what you want to do. – flup Jan 21 '15 at 18:52
  • 1
    @ToYonos, since Log4j 2.9.0 (cf. [LOG4J2-1884](https://issues.apache.org/jira/browse/LOG4J2-1884)) there is a pattern to print the process id: `%pid` (cf. [patterns documentation](https://logging.apache.org/log4j/2.x/manual/layouts.html#patterns)) – Piotr P. Karwasz May 22 '23 at 03:52
2

There is no way of doing it using standard Java classes. Generally process ID is appended at file level not at the log level. And here (archived here) is an example of doing it.

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
1

With the following pattern, showing threadID and class. If you want to see the Process ID, might check here

log4j.appender.SYSLOG.layout.conversionPattern=%-5p %d{ddMMyyyy HH:mm:ss.SSS} [%t:%c] %m%n

I managed to do so, but I have several appenders, one for each part of the application, like the following:

log4j.rootCategory=ERROR, SYSLOG2
log4j.logger.com.myself.logic=DEBUG, SYSLOG
log4j.logger.com.myself.database=DEBUG, SYSLOG3
log4j.logger.com.myself.other=DEBUG, SYSLOG

Here are some examples of generated logs, to check if this is what you require:

INFO 20012015 11:56:17.318 [pool-1-thread-1:com.myself.logic.MonitoringTask] 10602: Validation of orders before UTC=2015-01-20T16:56:17
INFO 20012015 11:56:34.200 [main:com.gmv.pazgs.mus.mpu.CLibWrapper] 10402: MPU Library folder: xxxxx
INFO 20012015 11:56:34.209 [main:com.gmv.pazgs.mus.mpu.CLibWrapper] 10402: MPU Configuration folder: xxxxx
INFO 20012015 11:56:34.773 [main:com.gmv.pazgs.mus.mpu.CLibWrapper] 10402: Calling InitLeap()
INFO 20012015 11:56:34.786 [main:com.gmv.pazgs.mus.mpu.CLibWrapper] 10402: Calling InitFourier()
INFO 20012015 11:57:10.151 [CRON_Thread:com.myself.other.DTOLDispatcher] 10202: Times to send: [11:00, 23:00] - current time = Tue Jan 20 11:57:10 UTC 2015
INFO 20012015 11:58:10.165 [pool-1-thread-4:com.myself.logic.MonitoringTask] 10602: Executing Monitoring Task UTC=2015-01-20T11:58:10
INFO 20012015 11:58:10.171 [pool-1-thread-5:com.myself.logic.OrderValidationTask] 10602: Executing OrderValidationTask UTC=2015-01-20T11:58:10
INFO 20012015 11:58:10.291 [CRON_Thread:com.myself.other.DTOLDispatcher] 10202: Times to send: [11:00, 23:00] - current time = Tue Jan 20 11:58:10 UTC 2015
INFO 20012015 11:58:10.684 [pool-1-thread-4:com.myself.logic.MonitoringTask] 10602: Expiration of orders before UTC=2015-01-20T11:58:10
INFO 20012015 11:58:11.218 [pool-1-thread-4:com.myself.logic.MonitoringTask] 10602: checking orders for suppresed status before UTC=2015-01-20T11:58:11
INFO 20012015 11:58:11.244 [pool-1-thread-4:com.myself.logic.MonitoringTask] 10602: Validation of orders before UTC=2015-01-20T16:58:11
Community
  • 1
  • 1
Alexcocia
  • 128
  • 9