Gradle SpringBoot中的Log4j

Gradle SpringBoot中的Log4j,gradle,log4j,spring-boot,log4j2,Gradle,Log4j,Spring Boot,Log4j2,我是SpringBoot的新手,正在使用SpringBoot制作一个简单的log4j演示。我使用了gradle项目,并且具有SpringBootStarterWeb和groovy依赖项。下面是我的log4j.properties文件内容。我所需要的是,当我执行主程序并使用annotation@Log4J时,我必须能够将log.perflog保存到本地(windows)中的一个文件中 我的示例groovy类: package sample.actuator.log4j import groovy

我是SpringBoot的新手,正在使用SpringBoot制作一个简单的log4j演示。我使用了gradle项目,并且具有SpringBootStarterWeb和groovy依赖项。下面是我的log4j.properties文件内容。我所需要的是,当我执行主程序并使用annotation@Log4J时,我必须能够将log.perflog保存到本地(windows)中的一个文件中

我的示例groovy类:

package sample.actuator.log4j

import groovy.util.logging.Log4j;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j
@RestController
@EnableAutoConfiguration
class HelloGroovy {

    static Logger perfLog = Logger.getLogger("perfLog")

    @RequestMapping("/logger")
    String logger() {
        log.info "created a new item named  identifier"
        log.error "created a new item named  identifier"
        log.warn "created a new item named  identifier"

        System.out.println("Test")
        perfLog.trace("Test")
        return "Logger Called."

    }

    static main(args) {

        SpringApplication.run(this, args)
    }

}
所有get都是在控制台中打印前3行,然后是“Test”,在log4j.properties中提到的文件中没有显示任何内容

我的build.gradle文件

buildscript {
    repositories {
            maven {
                url 'http://artifactory.myorg.com:8081/artifactory/plugins-release'
            }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.5.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'log4jOwn'
}

repositories {
            maven {
                url 'http://artifactory.myorg.com:8081/artifactory/plugins-release'
            }
    }

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.3'
    compile 'org.springframework.boot:spring-boot-starter-web'

    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

您需要将
perfLog
appender添加到rootLogger,以便默认日志消息发送到该文件。log4j.properties文件的第一行应为:

log4j.rootLogger = WARN , stdout, perfLog

您有
springbootstarterweb
作为直接依赖项,它不使用log4j进行日志记录,因此log4j不在您的类路径上。您需要排除
spring-boot-starter日志记录
并包括
spring-boot-starter-log4j
(就像这里-这是Maven,但如果您了解Gradle,您可以想出如何做同样的事情)


(多亏了那个编辑发帖的人。)

我也遇到了同样的问题,不包括spring boot starter日志、logback、log4j-over-slf4j和添加spring-boot-starter-log4j对我有效

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"

我使用的是Gradle4.4.1,显然

compile('org.springframework.boot:spring-boot-starter-web'){
    exclude module: 'org.springframework.boot:spring-boot-starter-logging'
}
不会排除默认的spring启动程序日志记录,但是

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

所以我让它和我一起工作

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

在Gradle 3.4之后,您应该这样配置它:

// exclude spring-boot-starter-logging module globaly
configurations.all {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // add log4j2 dependency
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}

仍然没有得到它。可能是一个愚蠢的错误,但挣扎太久,你需要显示你的构建配置。如果在类路径上没有明确的log4j,您的
log4j.properties
将被忽略。是的,我已将resources文件夹添加为源文件夹,其中包含log4j.properties。非常感谢它的工作。我写了gradle的等价物,它成功了,但这对我来说不起作用。实际上,我必须从log4j2启动器中删除slf4j!编译('org.springframework.boot:spring-boot-starter-log4j2'){排除组:'org.apache.logging.log4j',模块:'log4j-slf4j-impl'}
compile('org.springframework.boot:spring-boot-starter-web'){
    exclude module: 'spring-boot-starter-logging'
}
dependencies {
  compile ('org.springframework.boot:spring-boot-starter-web'){
    exclude module: 'spring-boot-starter-logging'
  }
  compile ('org.springframework.boot:spring-boot-starter-log4j2')
}
// exclude spring-boot-starter-logging module globaly
configurations.all {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // add log4j2 dependency
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}