zoukankan      html  css  js  c++  java
  • SpringBoot添加对Log4j2的支持

    1、在添加对Log4j2的支持前,需要先把SpringBoot默认使用的Logback日志框架排除,修改pom.xml文件:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                    <exclusions> 
                        <exclusion>  
                            <groupId>org.springframework.boot</groupId>  
                            <artifactId>spring-boot-starter-logging</artifactId>  
                        </exclusion>  
                    </exclusions>
            </dependency>

    红色字体的内容是指排除默认的日志框架

    需要注意的是,这个spring-boot-starter-web依赖配置需要放在所有依赖配置的前面,这样的目的是很多依赖配置都使用了默认日志,放在最前面下面的依赖都会默认排除这个日志框架。请仔细阅读红色部分内容,不然会报错,导致项目无法启动!

    添加对log4j2的支持:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>

    2、修改springboot配置文件application.properties,添加如下内容:

    #log4j2 configuration
    logging.config=classpath:log4j2.xml

    3、在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 %l %msg%n"/>
        </Console>
        
      </Appenders>
      
      <Loggers>
        <Root level="INFO">
          <AppenderRef ref="Console"/>
        </Root>
      </Loggers>
    </Configuration>

    通过以上三步,就完成了在SpringBoot中对log4j2的支持了。

  • 相关阅读:
    List of the best open source software applications
    Owin对Asp.net Web的扩展
    NSwag给api加上说明
    'workspace' in VS Code
    unable to find valid certification path to requested target
    JMeter的下载以及安装使用
    exception disappear when forgot to await an async method
    Filter execute order in asp.net web api
    记录web api的request以及response(即写log)
    asp.net web api的源码
  • 原文地址:https://www.cnblogs.com/modou/p/10040193.html
Copyright © 2011-2022 走看看