Yogen Rai
Yogen Rai

Reputation: 3033

Slf4j with log4j2 not working Spring boot 1.4.3.RELEASE

I was using Spring boot 1.3.6.RELEASE and log4j2.yml as below:

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

The logging with below code was working perfectly.

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

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

But when I upgraded to Spring Boot 1.4.3.RELEASE, the logger is not working.

My new build.gradle after upgrading to 1.4.3 is (just a version change)

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

What thing I'm missing here? TIA

I want to add more details: there are log4j-api:2.6.2, log4j-core:2.6.2 and log4j-slf4j-impl:2.2.6 on classpath after upgrading to Spring Boot 1.4.3. The thing is appender is not working for trace level because I am getting log for info or above level.

Upvotes: 3

Views: 1965

Answers (1)

Yogen Rai
Yogen Rai

Reputation: 3033

I resolved this problem by moving log4j2.yml directly into resource directory.

Earlier, with Spring Boot 1.3.6, I had put log4j2.yml under resources/logging directory with configuration.yml as

logging:
  config: classpath:logging/log4j2.yml

I don't know why it didn't work for 1.4.3.RELEASE.

Upvotes: 3

Related Questions