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'
}
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 excludespring-boot-starter-logging
and includespring-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).Gradle equivalent:
(Thanks to whoever posted that as an edit.)
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:I had the same problem and excluding spring-boot-starter-logging, logback, log4j-over-slf4j and adding spring-boot-starter-log4j worked for me.
I am using Gradle 4.4.1, and apparently
will not exclude the default spring-boot-starter-logging, but
will exclude it.
Therefore I got it working with