Home > Uncategorized > logback configuration for gradle/springboot type projects …

logback configuration for gradle/springboot type projects …

yet another painful trawl to set up logging, and have it behave like spring boot default.

So to basics.

  1. create your gradle project, i’m using Intellij Idea for that.  You need to include the latest springboot-gradle-plugin.  Then ensure you apply the plugin.  Then fill your dependencies, but include latest log back from maven central and Slf4j-api.  This will provide default mapping for logger directly to logback.
  2. group 'org.softwood'
    version '1.0-SNAPSHOT'
    buildscript {
        repositories {
        dependencies {
    apply plugin: 'idea'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'groovy'
    apply plugin: 'java'
    sourceCompatibility = 1.8
    repositories {
    dependencies {
        compile 'org.apache.camel:camel-spring-boot-starter:2.18.4'
        compile 'org.apache.camel:camel-groovy:2.18.4'
        compile 'org.apache.camel:camel-stream:2.18.4'
        compile 'org.codehaus.groovy:groovy-all:2.4.11'
        //add Logback
        compile 'ch.qos.logback:logback-classic:1.2.3'
        compile 'ch.qos.logback:logback-core:1.2.3'
        compile 'org.slf4j:slf4j-api:1.7.25'
        testCompile group: 'junit', name: 'junit', version: '4.11'
        testCompile group: 'junit', name: 'junit', version: '4.12'
  3. Then put either a logback.groovy or a logback.xml file into the project classpath. By default i’ve used logback.groovy for this groovy project and put it under the resources directory (along with application.yml etc you may want to tailor the spring application)

    I did some digging and if you want coloured output as the spring boot default you need a logback.groovy a bit like this. This does a scan for changes which you can disable, and calls two closures to configure the appenders, and the loggers

    import org.springframework.boot.ApplicationPid
    import java.nio.charset.Charset
    import static ch.qos.logback.classic.Level.*
    import ch.qos.logback.core.ConsoleAppender
    import ch.qos.logback.classic.encoder.PatternLayoutEncoder
    import ch.qos.logback.core.status.OnConsoleStatusListener
    import ch.qos.logback.core.FileAppender
    scan('5 minutes')  // Scan for changes every 5 minutes.
    def displayStatusOnConsole() {
        statusListener OnConsoleStatusListener
    def setupAppenders() {
        //add to converters to handle colour and whitespace
        conversionRule 'clr', org.springframework.boot.logging.logback.ColorConverter
        conversionRule 'wex', org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter
        def consolePatternFormat = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr([%property{PID} - %thread]){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(>){faint} %m%n%wex"
        def filePatternFormat = "%d{yyyy-MM-dd HH:mm:ss.SSS} [%property{PID} - %thread] %-5level %-12logger{12}:[.%M] > %msg%n%wex"
        if (!System.getProperty("PID")) {
            System.setProperty("PID", (new ApplicationPid()).toString())
        //def logfileDate = timestamp('yyyy-MM-dd') // Formatted current date.
        // hostname is a binding variable injected by Logback.
        //def filePatternFormat = "%d{HH:mm:ss.SSS} %-5level [${hostname}] %logger - %msg%n"
        //appender('logfile', FileAppender) {
        //    file = "simple.${logfileDate}.log"
        //    encoder(PatternLayoutEncoder) {
        //        pattern = filePatternFormat
        //    }
        // Add custom converter for %smiley pattern.
        //conversionRule 'smiley', SmileyConverter
        appender('STDOUT', ConsoleAppender) {
            encoder(PatternLayoutEncoder) {
                charset = Charset.forName('UTF-8')
                pattern = consolePatternFormat
                //pattern =
    def setupLoggers() {
        //logger 'com.mrhaki.java.Simple', getLogLevel(), ['logfile']
        logger 'application.Application', DEBUG
        root DEBUG, ['STDOUT']
    def getLogLevel() {
        (isDevelopmentEnv() ? DEBUG : INFO)
    def isDevelopmentEnv() {
        def env =  System.properties['app.env'] ?: 'DEV'
        env == 'DEV'
  4. As this is a groovy project you can just use @SLF4J annotation to your class and it will inject the log variable automatically – less noise that creating the logger. Now use the logh variable in a method as you need it
    @Slf4j //inject logger
    public class Application {
        ApplicationContext ctx
    public someMethod () {
                log.debug "log something "

That will give you a basic config that you can extend as you see fit, and add new classes to log as you go.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: