Spring Boot中的Log4j

时间:2014-08-28 00:04:39

标签: gradle log4j spring-boot log4j2

我是Spring Boot的新手,使用Spring Boot进行简单的log4j演示。我使用了gradle项目并拥有spring-boot-starter-web和groovy依赖项。下面是我的log4j.properties文件内容。我需要的是,当我执行主程序并使用注释@ Log4J时,我必须能够将log.perflog保存到本地(windows)中的文件中。

log4j.rootLogger = WARN , stdout, cslLog

log4j.logger.perfLog = WARN, perfLog
log4j.additivity.perfLog = false

log4j.appender.perfLog = org.apache.log4j.RollingFileAppender
log4j.appender.perfLog.File = ${GRAILS_HOME}/logs/csl.log
log4j.appender.perfLog.Append = true
log4j.appender.perfLog.ImmediateFlush = true

log4j.appender.perfLog.MaxFileSize=200MB
log4j.appender.perfLog.MaxBackupIndex = 1

我的样本groovy Class:

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

5 个答案:

答案 0 :(得分:15)

您有spring-boot-starter-web作为直接依赖项,并且它不使用log4j进行日志记录,因此log4j不在您的类路径中。您需要排除spring-boot-starter-logging并添加spring-boot-starter-log4j(例如此处https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-actuator-log4j/pom.xml#L36 - 这是Maven,但如果您了解Gradle,则可以了解如何执行相同操作)。

    <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等价物:

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')
}

(感谢任何人将其作为编辑发布。)

答案 1 :(得分:1)

我有同样的问题,不包括spring-boot-starter-logging,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"

答案 2 :(得分:0)

我使用的是Gradle 4.4.1,显然是

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

不会排除默认的spring-boot-starter-logging,而是

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')
}

答案 3 :(得分:0)

在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'
}

答案 4 :(得分:-1)

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

log4j.rootLogger = WARN , stdout, perfLog