SSM搭建請參考:
log4j2好像跟log4j不太一樣,log4j2.properties的配置文件也不同於原始的log4j.properties。具體請參考官網:log4j2官網
1.添加pom.xml依賴
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.11.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>2.11.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-jcl --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-jcl</artifactId> <version>2.11.2</version> </dependency>
2.在web.xml文件中配置監聽器
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener> <context-param> <param-name>log4jConfiguration</param-name> <!--默認是classpath下的log4j2.xml--> <param-value>classpath:log4j2.xml</param-value> </context-param>
3.在指定的log4j2的配置路徑下添加log4j2.xml文件
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Properties> <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t-%L] %-5level %logger{36} - %msg%n</property> </Properties> <Appenders> <!--這個輸出控制台的配置 --> <Console name="Console" target="SYSTEM_OUT"> <!-- 控制台只輸出level及以上級別的信息(onMatch),其他的直接拒絕(onMismatch) --> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" /> <PatternLayout pattern="${PATTERN}" /> </Console> </Appenders> <!--然后定義logger,只有定義了logger並引入的appender,appender才會生效 --> <Loggers> <!-- 配置日志的根節點 --> <root level="debug"> <appender-ref ref="Console" /> </root> </Loggers> </Configuration>
4.搞定!
參考文章:ssm框架整合log4j2搭建