Seperate logs for different jars in war file

2019-06-04 17:09发布

问题:

I have a war file which is deployed on jboss server. This war contains some jar files. What i need is to create seperate logs for different jars. I am using log4j for logging purpose and also intergrated log4j properties file in each jar. My log4j for every jar have different paths. Still the logs are generated in server.log of jboss. I need help to generate seperate logs for every jar.

回答1:

If you want to make different log file for different jar, you will have to configure it in your log4j.properties file related to every jar(I assume that every jar have different package naming convention).

Here is an example like if you want to log message in different file whose package name starts with org.jar1 and org.jar2.

log4j.debug=true
log4j.logger.org.jar1=DEBUG,jar1logger
log4j.logger.org.jar2=DEBUG,jar2logger

log4j.additivity.org.jar1=false
log4j.additivity.org.jar2=false
log4j.appender.jar1logger=org.apache.log4j.RollingFileAppender
log4j.appender.jar1logger.layout=org.apache.log4j.PatternLayout
log4j.appender.jar1logger.layout.ConversionPattern=%d [%t]<%-5p> %c -> %m%n
log4j.appender.jar1logger.File=/var/log/mylog/jar1.log
log4j.appender.jar1logger.Append=true
log4j.appender.jar1logger.MaxFileSize=1024KB
log4j.appender.jar1logger.MaxBackupIndex=5

log4j.appender.jar2logger=org.apache.log4j.RollingFileAppender
log4j.appender.jar2logger.layout=org.apache.log4j.PatternLayout
log4j.appender.jar2logger.layout.ConversionPattern=%d [%t]<%-5p> %c -> %m%n
log4j.appender.jar2logger.File=/var/log/mylog/jar2.log
log4j.appender.jar2logger.Append=true
log4j.appender.jar2logger.MaxFileSize=1024KB
log4j.appender.jar2logger.MaxBackupIndex=5

Based on your requirement whether you want to create log on daily basis or on size basis you have to change your configuration.

Keep in mind if any jar whose package name in any jars start with org.jar1 then all logs will be written into jar1.log file.



标签: java log4j