Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot + Groovy + logback.groovy

I am mixing Groovy and Java in my Spring-boot application. Rest controllers and data access is written in Groovy. Configurations are mainly in Java.

As per logback documentation, if there is a logback.groovy file in the classpath, it's supposed to be picked ahead of logback.xml. However only logback.xml is working in my case.

I am running the app as sprint-boot-application.

Also, it's worth noting that spring suggest to inherit some of the logging configuration like shown below

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>

There is no way to do this in Groovy config.

build.gradle:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework:spring-jdbc")
    compile("com.h2database:h2")
    compile("org.hsqldb:hsqldb")
    testCompile("junit:junit")
    compile('org.codehaus.groovy:groovy-all:2.3.10')
    testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.5.0-RC2')
    compile('org.slf4j:slf4j-simple:1.6.1')
}

sourceSets {
    main {
        groovy {
            srcDirs = ['src/main/groovy', 'src/main/java'] 
        }
        java {
            srcDirs = []
        }
    }
    test {
        groovy {
            srcDirs = ['src/test/groovy', 'src/test/java']
        }
        java {
            srcDirs = []
        }
    }
}
like image 630
Sree Avatar asked Aug 04 '15 09:08

Sree


People also ask

What is Logback Groovy?

Logback is designed to be faster and have a smaller memory footprint than the other logging frameworks around. If you are new to Logback, you should checkout my introductory post on Logback: Logback Introduction: An Enterprise Logging Framework. Logback supports configuration through XML and Groovy.

Does spring boot support Logback?

Spring Boot provides a number of logback configurations that be included from your own configuration.

Does slf4j use Logback?

Native implementation of slf4j is logback, thus using both as logger framework implies zero memory and computational overhead.

How do I use Logback in spring boot?

Logback Rolling File Logging via XML Configuration file To configure Logback for a Spring Boot project via XML, create the logback. xml or logback-spring. xml file under the src/main/resources folder. The configuration in XML file will override the logging properties in the application.


1 Answers

First, your build.gradle looks strange to me:

  • you don't include the spring-boot-gradle-plugin
  • in your sourceSets options you define settings which are the default values of the Groovy plugin, see Project layout
  • Note: even if you mix java and groovy files you don't have to separate them (you can if you want). I usally keep them both in the groovy directory.
  • in your dependencies section you are using simple dependencies instead of Spring Boot starters (see also the reference doc)
  • You have 2 DB dependencies (H2 and HSQL)

Try to create a sample project with Spring Initializr - switch to full version. Your build.gradle would look like

buildscript {
    ext {
        springBootVersion = '1.5.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter'
    compile 'org.springframework.boot:spring-boot-starter-logging'
    compile 'org.springframework.boot:spring-boot-starter-jdbc'
    compile 'org.codehaus.groovy:groovy'
    compile 'com.h2database:h2'

    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.0-RC2'
}

With this configuration logback.groovy should work. For specific problems just post your logback.groovy. But as you have noted, the Groovy config is not a full citizen. When you include the spring-boot-starter-logging starter you can also extend the standard logging config with logback-spring.groovy or logback-spring.xml.

For full control you have to use the XML config and for small projects I stopped using logback.groovy and instead just config the logging starter via some settings in the application.properties, see Custom log configuration.

E.g. some settings for application.properties with logs with colored columns (all platforms except windows < 10 and in IDEA even under windows < 10):

logging.file = logs/jira.log
spring.output.ansi.enabled = DETECT
logging.level.root = INFO
logging.level.org.apache.http = WARN
like image 136
ChrLipp Avatar answered Oct 30 '22 14:10

ChrLipp