2011-01-07 48 views
13

我目前在log4j中有以下模式佈局。我想將進程ID添加到日誌文件中。我該怎麼做?如何將進程ID添加到LOG4J日誌文件?

log4j.appender.A1.layout=org.apache.log4j.PatternLayout 
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n 

粘貼樣品日誌消息

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 
+1

可能重複(http://stackoverflow.com/questions/4286089/ [如何使用Log4cxx或log4j的登錄進程ID] H ow-to-log-process-id-using-log4cxx-or-log4j) – skiphoppy 2012-08-30 17:16:49

+0

你想要打印java進程ID,還是打印當前線程ID? – armstrhb 2013-02-01 17:45:44

回答

2

沒有使用標準的Java類做它的方式。一般來說,進程ID不是以日誌級別附加在文件級別。而here就是這樣做的例子。

+0

我只有log4j.properites文件。我沒有看到任何XML配置文件。 – Arav 2011-01-07 04:18:24

+0

在您粘貼的網頁鏈接中有一個配置xml文件 – Arav 2011-01-07 04:18:45

10

您應該使用MDC

在配置文件:

log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p (%c) %m%n %X{PID} 

%X{PID}用來匹配上下文值PID

然後,在代碼中,前記錄開始:

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); 
1
隨着

以下方式,示出線程ID和類。如果你想看到的進程ID,可以檢查here

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

我好不容易纔這樣做的,但我有幾個附加目的地,一個用於應用程序的每個部分,如下所示:

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 

下面是生成的日誌的一些例子,來檢查,如果這是你需要什麼:

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