1
votes

The technologies I'm using are:

Maven
Spring
Oracle Weblogic
Eclipse

The project file structure looks like:

src/main/java/...
src/main/resources/Log4j.properties
src/test/java/...
src/test/resources/...
WebContent/META-INF/...
WebContent/WEB-INF/MyServletController-servlet.xml
WebContent/WEB-INF/web-xml
WebContent/WEB-INF/weblogic.xml
WebContent/index.html

At the start of each class I have

 private Logger log = LoggerFactory.getLogger(MyClass.class);

from which I can log items with

log.debug("Some message");

My Log4j.properties looks pretty normal:

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

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

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.TextProcessor.Threshold=debug
log4j.appender.file.File=E:\\logs\\evaluator-log.txt
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.logger.statsLogger=trace, stats
# Direct stats to a log file - this appender should be controlled by a separate logger call in the code.
log4j.appender.stats=org.apache.log4j.RollingFileAppender
log4j.appender.TextProcessor.Threshold=debug
log4j.additivity.statsLogger=false
log4j.appender.stats.append=true
log4j.appender.stats.File=E:\\logs\\evaluator-stats.txt
log4j.appender.stats.MaxFileSize=10MB
log4j.appender.stats.MaxBackupIndex=1
log4j.appender.stats.layout=org.apache.log4j.PatternLayout
log4j.appender.stats.layout.ConversionPattern=%m%n
# TRACE,
# DEBUG,
# INFO,
# WARN,
# ERROR
# FATAL

#Define custom levels by package
log4j.logger.org.springframework=DEBUG

When I run code manually in eclipse, the logging works fine, and prints both to console and to file.

However, when I try to deploy it to weblogic, the weblogic logs give me:

<log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).> 

Now I understand that this is because Log4j doesn't know where to find the Log4j.properties file.

Googling this gives plenty of results such as

Adding log configuration context-param to web.xml

http://www.mkyong.com/spring-mvc/spring-mvc-log4j-integration-example/

<context-param>

<param-name>log4jConfigLocation</param-name>

<param-value>classpath:/main/resources/Log4j.properties</param-value>

</context-param>

This gives the same error.

Adding both log config and a listener to web.xml

Under Spring Framework: WARN: WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader)

org.springframework.web.util.Log4jConfigListener

<context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:/main/resources/log4j.xml</param-value>
</context-param>

This gives

weblogic.application.ModuleException: 

java.lang.Exception: Exception received from deployment driver. See Error Log view for more detail.
    at oracle.eclipse.tools.weblogic.server.internal.DeploymentProgressListener.watch(DeploymentProgressListener.java:188)
    at 
<snip>
Caused by: java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded

    at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:158)
    at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:117)
    at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:46)
    at
<snip>

Can someone please give me a plain english explaination of what Log4j is doing to find Log4j.properties, and how I configure my application to tell it where it is?

1

1 Answers

0
votes

I ended up using this code snippet at the entry point of my Java.

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/log4j.properties"));
PropertyConfigurator.configure(props);