8

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 = []
        }
    }
}
Sree
  • 746
  • 6
  • 21

1 Answers1

3

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
ChrLipp
  • 15,526
  • 10
  • 75
  • 107