3

I'm newbie to Spring Boot and working on a simple log4j demo using Spring Boot. I used the gradle project and have spring-boot-starter-web and groovy dependencies. Below is my log4j.properties file content. All I need is , when i execute the main program and use annotation @Log4J i must be able to save the log.perflog to a file in my local (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

My sample 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)
    }

}

All get is the first 3 lines print in the console and then "Test" , post that nothing shows up in the file i have mentioned in the log4j.properties.

My build.gradle file

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'
}
beresfordt
  • 5,088
  • 10
  • 35
  • 43
Vinodh Thiagarajan
  • 758
  • 3
  • 9
  • 19

5 Answers5

18

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.)

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • Thanks a lot it worked. i wrote the gradle equivalent and it worked. – Vinodh Thiagarajan Aug 28 '14 at 18:57
  • 2
    This did not work for me. I actually had to remove slf4j from the log4j2 starter! compile('org.springframework.boot:spring-boot-starter-log4j2'){ exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'} – szxnyc Oct 10 '17 at 15:40
2

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"
JCasso
  • 5,423
  • 2
  • 28
  • 42
1

After Gradle 3.4, you should configure it like this:

// 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'
}
Reed Chan
  • 525
  • 6
  • 16
0

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')
}
lok77chan
  • 55
  • 5
-1

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
Mark Vieira
  • 13,198
  • 4
  • 46
  • 39