步骤:
1.在webcontent/WEB-INF/lib下添加log4j-api-.jar,以及log4j-core-.jar;
2.新建log4j.properties配置文件,写入配置信息
log4j.rootLogger=DEBUG, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
3.在web.xml中添加添加配置
<!-- 配置log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
4.在代码中使用
- 导入log4jpackage
- 使用
import org.apache.log4j.Logger;
private static Logger logger = Logger.getLogger(ShoppingCartController.class);
logger.info("infoMessage");