2015-07-10 69 views
0

我正在使用Java處理log4j。我重定向我的所有的應用程序記錄的信息到特定文件夾我的項目裏面是這樣的:Java Log4J Logger消息

logs/log4j-MyFirstClass.log 

如果我想重定向特定文件的信息到特定的日誌文件,例如:ABC.java記錄消息AB​​C.log文件,那麼我應該如何更改這個log4J.properties文件?請幫忙。這裏是我的log4j.properties文件:

# Root logger option 
log4j.rootLogger=DEBUG, stdout, file 

# Redirect log messages to console 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.Target=System.out 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 

# Redirect log messages to a log file, support file rolling. 
log4j.appender.file=org.apache.log4j.RollingFileAppender 
log4j.appender.file.File=logs/log4j-MyFirstClass.log 
log4j.appender.file.MaxFileSize=5MB 
log4j.appender.file.MaxBackupIndex=10 
log4j.appender.file.layout=org.apache.log4j.PatternLayout 
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 

回答

0

您需要爲每個文件定義一個appender,然後指定哪些包使用哪個appender。

# Root logger option; everything logs to console at debug 
log4j.rootLogger=INFO, stdout 

# Classes in package com.foo.bar will use the file1 appender 
log4j.logger.com.foo.bar=file1 

# Classes in package com.whizz.bang will use the file2 appender, note we also overrode the default log level 
log4j.logger.com.whizz.bang=DEBUG, file2 

# Redirect log messages to console 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.Target=System.out 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 

# Appender for file 1 
log4j.appender.file1=org.apache.log4j.RollingFileAppender 
log4j.appender.file1.File=logs/log4j-first.log 
log4j.appender.file1.MaxFileSize=5MB 
log4j.appender.file1.MaxBackupIndex=10 
log4j.appender.file1.layout=org.apache.log4j.PatternLayout 
log4j.appender.file1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 

# Appender for file 2 
log4j.appender.file2=org.apache.log4j.RollingFileAppender 
log4j.appender.file2.File=logs/log4j-second.log 
log4j.appender.file2.MaxFileSize=5MB 
log4j.appender.file2.MaxBackupIndex=10 
log4j.appender.file2.layout=org.apache.log4j.PatternLayout 
log4j.appender.file2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 
+0

非常感謝您的快速回復。我會努力工作並讓你知道。 –

+0

我得到一些問題:log4j:錯誤找不到關鍵字log4j.appender.log4j-MediaCenter.log的值 –

+0

這是修改的log4j文件:#程序包ro.ibm.emm.kaltura中的類將使用file2 appender log4j .logger.ro.ibm.emm.kaltura = DEBUG,log4j-MediaCenter.log log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File = logs/log4j-MediaCenter.log –