Log4j2+spring+maven配置

Posted dongdong-mgd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Log4j2+spring+maven配置相关的知识,希望对你有一定的参考价值。

1,在项目pom.xml下,配置
        <dependency>
        <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-api</artifactId>
      <version>2.0.2</version>
  </dependency>
  <dependency>
     <groupId>org.apache.logging.log4j</groupId>
     <artifactId>log4j-core</artifactId>
      <version>2.0.2</version>
 </dependency>
 <dependency>
     <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.0.2</version>
</dependency>

2,在src/main/resources目录下,配置log4j2.xml
    <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    
    <Loggers>
        <Logger name="org.springframework" level="info">
            <AppenderRef ref="Console" />
        </Logger>
        <Root level="debug">
<!--              这儿为trace表示什么都可以打印出来了,其他几个级别分别为:TRACE、DEBUG、INFO、WARN、ERROR和FATAL -->
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

3,在web.xml中配置监听器与过滤器
      <!-- Log4j2监听器 -->
   <listener>
    <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
  </listener>

<!-- Log4j2过滤器 -->
<filter>
    <filter-name>log4jServletFilter</filter-name>
    <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>log4jServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
</filter-mapping>

4,在类中配置
    private static Logger log = LogManager.getLogger(xxxControlloer.class.getName());

























































以上是关于Log4j2+spring+maven配置的主要内容,如果未能解决你的问题,请参考以下文章

6spring注解+springMVC注解+Mybatis注解+log4j+idea+maven

Spring Boot 2.x 实践记:log4j2.xml

Spring Boot 2.x 实践记:log4j2.xml

Spring Boot 2.x 实践记:log4j2.xml

idea配置mave

idea配置mave