Spring boot + Groovy + logback.groovy

I mix Groovy and Java in my Spring-boot application. Break controllers and data access are written to Groovy. The configurations are mostly in Java.

According to the log documentation, if the logback.groovy file is in the classpath, it must be selected before logback.xml. However, in my case only logback.xml works.

I run the application as a sprint-boot application.

In addition, it is worth noting that spring offers to inherit some logging configuration, as 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 = [] } } } 
+8
spring-boot groovy logback logback-groovy
source share
1 answer

Firstly, your build.gradle looks weird to me:

  • you do not enable spring-boot-gradle-plugin
  • in the sourceSets settings you define the parameters that are the default values ​​for the Groovy plugin, see Project Layout
  • Note: even if you mix java and Groovy files, you do not need to separate them (you can if you want). I usually save them in the Groovy directory.
  • in the dependencies section, you use simple dependencies instead of Spring Bootstraps (see also the reference document )
  • You have 2 DB dependencies (H2 and HSQL)

Try creating a sample project with Spring Initializr - go to the full version. Your build.gradle will 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' } 

logback.groovy should work with this configuration. For specific problems, just send logback.groovy . But, as you already noted, Groovy configuration is not a full-fledged citizen. When you enable the spring-boot-starter-logging , you can also extend the standard logging configuration using logback-spring.groovy or logback-spring.xml .

For complete control, you need to use the XML configuration for small projects, I stopped using logback.groovy and instead just configure the registration starter using some settings in application.properties , see Customizing the log .

eg. some settings for application.properties with colored column logs (all platforms except windows <10 and in IDEA, even windows <10):

 logging.file = logs/jira.log spring.output.ansi.enabled = DETECT logging.level.root = INFO logging.level.org.apache.http = WARN 
+3
source share

All Articles