5

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'
}
2
  • You need to show your build config. Without log4j explicitly on the classpath your log4j.properties will be ignored. Commented Aug 28, 2014 at 9:12
  • Yes I have added the resources folder as a source folder and it has the log4j.properties in it. Commented Aug 28, 2014 at 15:05

5 Answers 5

19

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot it worked. i wrote the gradle equivalent and it worked.
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'}
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"

Comments

2

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

Comments

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

Comments

-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

1 Comment

Still didn't get it.Could be a silly mistake but struggling too long

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.