How to add the Process id to a LOG4J log file?

后端 未结 3 775
清酒与你
清酒与你 2021-02-12 21:18

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         


        
相关标签:
3条回答
  • 2021-02-12 21:53

    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);
    
    0 讨论(0)
  • 2021-02-12 21:55

    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
    
    0 讨论(0)
  • 2021-02-12 21:58

    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.

    0 讨论(0)
提交回复
热议问题