【问题标题】:Spring-Boot logging with log4j2?使用 log4j2 进行 Spring-Boot 日志记录?
【发布时间】:2014-10-30 05:59:39
【问题描述】:

我正在使用spring-boot-starter,并想将log4j2.xml 配置为将异步+不同的内容记录到不同的日志文件中。

我创建了 log4j2 文件,但 Spring 仍然使用 spring-boot 默认日志记录。如何切换日志记录?

【问题讨论】:

    标签: java spring log4j spring-boot log4j2


    【解决方案1】:

    我有更好的办法:

    1. 排除 logback 记录器:

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      
    2. 添加log4j2启动启动器:

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

    来源:http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#howto-configure-log4j-for-logging

    享受吧!

    【讨论】:

    • 我想你的意思是:“1.排除 logback 框架”而不是“排除 slf4j 记录器”对吗?
    • 如果你使用 log4j2,还要确保你使用 spring-boot-starter-log4j2,因为 spring-boot-starter-log4j 是有效的,但是不适用于 log4j2
    • 对我来说,我必须添加这两个 logback-classicch.qos.logbacklog4j-over-slf4jorg.slf4j
    • 对于使用 Gradle 的我来说,我只是这样做了:configurations.all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging',然后在我的依赖项{}部分我添加了runtime 'org.springframework.boot:spring-boot-starter-log4j2'
    • 对我来说它不起作用:所有日志都打印到控制台。
    【解决方案2】:

    这对我有用。有两个额外的排除。否则应用程序没有获取 log4j 并且发生冲突

    实际绑定的类型是 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
            <exclusion>
                <artifactId>logback-classic</artifactId>
                <groupId>ch.qos.logback</groupId>
            </exclusion>
            <exclusion>
                <artifactId>log4j-over-slf4j</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>            
        </exclusions>           
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    

    【讨论】:

      【解决方案3】:

      试试这个:

      1. 排除 spring-boot-starter-logging 例如

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
      2. 为您的日志接口添加依赖项,例如slf4j

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.0.2</version>
        </dependency>
        
      3. 添加指向所选日志接口的其他日志实现,例如

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
        </dependency>
        
      4. 添加您的目标日志记录实现,例如

        <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>
        

      它应该可以工作。

      【讨论】:

      • 现在也有spring-boot-starter-log4j2,因为 spring-boot-1.2.0.RELEASE。仅供参考。
      • 这对我也不起作用:所有日志都打印到控制台。
      猜你喜欢
      • 2017-12-20
      • 2021-11-19
      • 2015-09-02
      • 2020-11-05
      • 2019-02-09
      • 2017-10-09
      • 2019-08-31
      • 2023-04-04
      • 2017-05-18
      相关资源
      最近更新 更多