1.pom.xml文件引入log4j相关依赖
<log4j.version>2.8</log4j.version>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
2.web.xml引入相关加载配置
<!--由Spring载入的Log4j配置文件位置-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<!--Spring log4j Config loader-->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
3.在resource目录下建立log4j.properties配置文件,相关方法
private static Logger logger = Logger.getLogger(WebLog.class);
public static void error(String arg,Exception e){
logger.error(arg, e);
}
public static void debug(String arg,Exception e){
logger.debug(arg, e);
}
public static void info(String arg,Exception e){
logger.info(arg, e);
}
public static void warn(String arg,Exception e){
logger.warn(arg, e);
}
解决的一些问题:
1.The type org.apache.log4j.Category cannot be resolved. It is indirectly referenced from required .class files
在项目的build path里已有 Maven Dependencies ,里面也有 spring的包
但是引用不到jar包,去maven本地仓库删除相关jar文件夹,重新update maven jar,即可解决,可解决引入的问题