我正在使用的技术是:
Maven
Spring
Oracle Weblogic
Eclipse
项目文件结构如下:
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
每节课开始时我都有
private Logger log = LoggerFactory.getLogger(MyClass.class);
我可以用
记录项目log.debug("Some message");
我的Log4j.properties看起来很正常:
# 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
当我在eclipse中手动运行代码时,日志记录工作正常,并打印到控制台和文件。
但是,当我尝试将其部署到weblogic时,weblogic日志会给我:
<log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).>
现在我明白这是因为Log4j不知道在哪里可以找到Log4j.properties文件。
谷歌搜索可以提供大量结果,例如
将日志配置context-param添加到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>
这会产生同样的错误。
将日志配置和侦听器添加到web.xml
org.springframework.web.util.Log4jConfigListener
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/main/resources/log4j.xml</param-value>
</context-param>
这给出了
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>
有人可以给我一个简单的英语解释,说明Log4j正在做什么来查找Log4j.properties,以及我如何配置我的应用程序告诉它它在哪里?
答案 0 :(得分:0)
我最终在Java的入口点使用了这段代码。
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/log4j.properties"));
PropertyConfigurator.configure(props);