25 November, 2013

Spring-boot change log-file location

By default spring-boot writes logs to the /tmp/spring.log file.
But if you need another location - just set LOG_FILE variable in logback.xml configuration.

logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 
    <property name="LOG_FILE" value="tmp/application.log"/>
 
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
 
    <logger name="sb" level="DEBUG"/>
 
</configuration> 

Application.java
package sb;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
 
@Configuration 
public class Application {
 
    @Service 
    static class TestBean {
        protected final Logger log = LoggerFactory.getLogger(getClass());
 
        void test() {
            log.debug("TestBean.test");
        } 
    } 
 
 
    public static void main(String[] args) throws Throwable {
        SpringApplication app = new SpringApplication(Application.class);
        ConfigurableApplicationContext ctx = app.run(args); 
 
        TestBean bean = ctx.getBean(TestBean.class);
        bean.test(); 
    } 
} 

build.gradle
buildscript {
    repositories { 
        maven { url "http://repo.spring.io/libs-snapshot" }
    } 
    dependencies { 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6")
    } 
} 
 
apply plugin: "java"
apply plugin: "spring-boot"
 
buildDir = "out"
 
jar { 
    baseName = "sb-log-file"
    version = "0.1"
} 
 
repositories { 
    mavenCentral() 
    maven { url "http://repo.spring.io/libs-snapshot" }
} 
 
dependencies { 
    def springBootVersion = '0.5.0.M6'
    compile("org.springframework.boot:spring-boot-starter:$springBootVersion")
}

No comments: