slf4j与log4j2无法正常工作Spring启动1.4.3.RELEASE

时间:2017-01-03 21:44:57

标签: spring logging spring-boot log4j slf4j

我正在使用Spring boot 1.3.6.RELEASE和log4j2.yml,如下所示:

Configuration:
  status: info

  Properties:
    Property:
      name: log-path
      value: "/dvl-log/pol/apps/logs/api"

  Appenders:
    Console:
      - name: Console
        target: SYSTEM_OUT
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} %-5level %logger{1} - %msg%n"

  Loggers:
    Root:
      level: info
      AppenderRef:
        - ref: Console
    Logger:
    - name: com.company.api
      level: trace
      additivity: false
      AppenderRef:
        - ref: Console
          level: trace

使用以下代码的日志记录工作正常。

@Slf4j
public class LogExample{
      public void logMethods(String className,String methodName){

        log.trace("{} >> {}", className, methodName);
      }
}

但是当我升级到Spring Boot 1.4.3.RELEASE时,记录器无效。

升级到1.4.3后我的新build.gradle是(只是版本更改)

version '1.0'
// dependencies for command line
buildscript {
    ext {
        springBootVersion = '1.4.3.RELEASE'
        dependencyManagementVersion = '0.5.2.RELEASE'
    }
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
        classpath "io.spring.gradle:dependency-management-plugin:${dependencyManagementVersion}"
    }
}

apply plugin: "io.spring.dependency-management"
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'idea'

// JDK 8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

repositories {
    jcenter()
}

ext {
    springCloudVersion = 'Brixton.SR4'
    springBootVersion = '1.4.3.RELEASE'
    swaggerVersion = '2.4.0'
    jodaTimeVersion = '2.9.4'
    jacksonJodaVersion = '2.5.1'
    junitVersion = '4.12'
    springWsTestVersion = '2.2.3.RELEASE'
    lombokVersion = '1.16.10'
    jsonPathVersion = '2.2.0'
    ehcacheVersion = '3.2.0'
    javaxCacheVersion = '1.0.0'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
    }
}

sourceSets {
    test {
        java {
            srcDir 'src/test/unit/java'
        }
        resources {
            srcDir 'src/test/unit/resources'
        }
    }
}

dependencies {

    /* core libraries */
    compile('org.springframework.cloud:spring-cloud-starter-config') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    compile('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    compile("org.springframework.boot:spring-boot-starter-hateoas"){
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    compile 'org.springframework.ws:spring-ws-core'

    // logging
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    compile('com.fasterxml.jackson.dataformat:jackson-dataformat-yaml')
    compile('com.fasterxml.jackson.core:jackson-databind')

    // embedded server
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')


    // https://mvnrepository.com/artifact/org.projectlombok/lombok-maven
    compile "org.projectlombok:lombok:${lombokVersion}"

    // https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path
    // A Java DSL for reading JSON documents
    compile "com.jayway.jsonpath:json-path:${jsonPathVersion}"


    /* plugins */

    /* test libraries */
    // unit
    testCompile "junit:junit:${junitVersion}"
    testCompile "org.springframework.boot:spring-boot-starter-test"
    testCompile "org.springframework.ws:spring-ws-test:${springWsTestVersion}"
}

war {
    archiveName = "${project.name}.war"
}

我在这里失踪了什么? TIA

我想添加更多详情: 升级到Spring Boot 1.4.3后,在classpath上有log4j-api:2.6.2,log4j-core:2.6.2和log4j-slf4j-impl:2.2.6。事情是appender不适用于trace级别,因为我正在获取info或更高级别的日志。

1 个答案:

答案 0 :(得分:2)

我通过将log4j2.yml直接移至resource目录来解决此问题。

早些时候,使用Spring Boot 1.3.6,我已将log4j2.yml放在resources/logging目录下,其中configuration.yml为

logging:
  config: classpath:logging/log4j2.yml

我不知道为什么它不能用于1.4.3.RELEASE。