这是我的 bean 类 AppLogger.java
public class AppLogger {
private String logMessage;
public String getLogMessage() {
return logMessage;
}
public void setLogMessage(String logMessage) {
this.logMessage = logMessage;
}
}
我的 log4j.properties
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to stdout
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{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=debug, file, stdout
我的 Log4j.xml
<bean id="appLogger" class="com.sort.model.AppLogger">
<property name="message" value="Logger!"/>
</bean>
我的 Web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/Log4J.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
我的控制器,我试图在其中记录示例消息。
public class BasicFormController {
private static final Logger logger = Logger.getLogger(BasicFormController.class);
@ModelAttribute("evaluation")
protected List<String> referenceData(HttpServletRequest request) throws Exception {
logger.info("creating a message");
List<String> evaluation = new ArrayList<String>();
evaluation.add("Evaluated");
evaluation.add("Not Evaluated");
return evaluation;
}
}
我的 servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="com.sort.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value></value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
</beans>
我的构建路径中有 log4j-1.2.14.jar。现在的问题是,我收到 404 错误。该应用程序运行良好,无需所有这些 Log4j 相关问题。
你能帮我解决这个问题吗?
或者有人可以指导我举一个合适的例子来演示 Spring MVC 项目的日志记录吗?