Need help - logs are getting mixed between 2 different applications running in same JVM
I am facing issues with logging of 2 applications deployed in same JVM.
I have 2 applications say A & B running in Websphere application server.
A is EJB project having log4j.jar in EJB jar
B is also EJB project but doesn't have log4j.jar as part of EJB jar and it refers to log4j jar available in Websphere lib.
A & B have independent Logger classes say ALogger.java and BLogger.java resp.
ALogger.java having static Logger logger = Logger.getLogger("A.class)");
BLogger.java having static Logger logger = Logger.getLogger("B.class)");
log4j.properties fot both are different and logs are written to diff file
App A:
log4j.rootLogger=ALL,ErrorAppender,file
log4j.appender.ErrorAppender.Threshold=WARN
log4j.appender.ErrorAppender.layout.ConversionPattern=%-5p\t%d{ddMMyyyy-HH\:mm\:ss}\t%m%n
log4j.appender.ErrorAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.File=**AErrorLog.log**
log4j.appender.ErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ErrorAppender.MaxFileSize=1MB
log4j.appender.ErrorAppender.MaxBackupIndex=5
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout.ConversionPattern=%-5p\t%d{ddMMyyyy-HH\:mm\:ss}\t%m%n
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.File=**ALog.log**
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=5
App B:
log4j.rootLogger=ALL,ErrorAppender,file
log4j.appender.ErrorAppender.Threshold=WARN
log4j.appender.ErrorAppender.layout.ConversionPattern=%-5p\t%d{ddMMyyyy-HH\:mm\:ss}\t%m%n
log4j.appender.ErrorAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.File=**BErrorLog.log**
log4j.appender.ErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ErrorAppender.MaxFileSize=1MB
log4j.appender.ErrorAppender.MaxBackupIndex=5
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout.ConversionPattern=%-5p\t%d{ddMMyyyy-HH\:mm\:ss}\t%m%n
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.File=**BLog.log**
log4开发者_JAVA技巧j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=5
log4j works fine when B is invoked after processing of A.
Problem : While A is running if B gets triggered,App A logs are getting written to App B's log file. How do I fix this issue?
I have tried changing appender names for App B but it didn't work.
Log4J reads its configuration exactly once (unless instructed programmatically to do otherwise) within the scope of one classloader.
Your packaging strategy made WebSphere's Log4J load first, within WebSphere's classloader which is higher in the classloader hierarchy. Log4J initializes and loads; later, when your application (which has log4j embedded in it) makes a logging call, Log4J finds out that it is already initialized, so it doesn't even read the second property file.
The rule of thumb that must be followed to avoid similar cases is this: If you are using a utility JAR that reads configuration and stores it statically at the classloader level (for example: Log4J; Apache FOP; and many others), then you must bundle that JAR with your application and not have your code refer to AppServer-provided copies.
Follow that rule and you'll be golden... Even under WebSphere, which, for years, has been notoriously known for causing some mess with its classloading strategies.
B is also EJB project but doesn't have log4j.jar as part of EJB jar and it refers to log4j jar available in Websphere lib
Try to change that. Have both projects bundle their own log4j. As of now, log4j is probably getting confused.
Because of log4j is initialed by using static class, so you should not use share library location such as glassfish/lib. Try to bundle log4j library at your application.
精彩评论