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 = []
}
}
}
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.
Spring Boot provides a number of logback configurations that be included from your own configuration.
Native implementation of slf4j is logback, thus using both as logger framework implies zero memory and computational overhead.
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.
First, your build.gradle
looks strange to me:
spring-boot-gradle-plugin
sourceSets
options you define settings which are the default values of the Groovy plugin, see Project layout
dependencies
section you are using simple dependencies instead of Spring Boot starters (see also the reference doc)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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With