Log4j in SpringBoot

两盒软妹~` 提交于 2019-12-06 09:21:40

You have spring-boot-starter-web as a direct dependency and it doesn't use log4j for logging so log4j is not on your classpath. You would need to exclude spring-boot-starter-logging and include spring-boot-starter-log4j (like here https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-actuator-log4j/pom.xml#L36 - that's Maven but if you know Gradle you can figure out how to do the same thing).

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

Gradle equivalent:

dependencies {
  compile 'org.codehaus.groovy:groovy'
  compile ('org.springframework.boot:spring-boot-starter-web'){
    exclude module: 'org.springframework.boot:spring-boot-starter-logging'
  }
  compile ('org.springframework.boot:spring-boot-starter-log4j')
}

(Thanks to whoever posted that as an edit.)

I had the same problem and excluding spring-boot-starter-logging, logback, log4j-over-slf4j and adding spring-boot-starter-log4j worked for me.

compile("org.springframework.boot:spring-boot-starter-web"){
    exclude module: "org.springframework.boot:spring-boot-starter-logging"
    exclude module: "logback-classic"
    exclude module: "log4j-over-slf4j"
}

compile group: "org.springframework.boot", name: "spring-boot-starter-log4j", version: "1.3.8.RELEASE"

I am using Gradle 4.4.1, and apparently

compile('org.springframework.boot:spring-boot-starter-web'){
    exclude module: 'org.springframework.boot:spring-boot-starter-logging'
}

will not exclude the default spring-boot-starter-logging, but

compile('org.springframework.boot:spring-boot-starter-web'){
    exclude module: 'spring-boot-starter-logging'
}

will exclude it.

Therefore I got it working with

dependencies {
  compile ('org.springframework.boot:spring-boot-starter-web'){
    exclude module: 'spring-boot-starter-logging'
  }
  compile ('org.springframework.boot:spring-boot-starter-log4j2')
}

You need to add the perfLog appender to the rootLogger so that default logging messages go out to that file. The first line of your log4j.properties file should read as:

log4j.rootLogger = WARN , stdout, perfLog
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!