Logging from tests


Following up on the logging blog post.

When creating and later running tests it could be equally helpful with some good logs.

Configuring test logging is done with either logback-test.groovy or logback-test.xml which should be placed in the resources directory within the test directory.

logback-test.groovy

import org.springframework.boot.logging.logback.ColorConverter
import org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter
import java.nio.charset.StandardCharsets

conversionRule 'clr', ColorConverter
conversionRule 'wex', WhitespaceThrowableProxyConverter

// See http://logback.qos.ch/manual/groovy.html for details on configuration
appender('STDOUT', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        charset = StandardCharsets.UTF_8

        pattern =
                '%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} ' + // Date
                        '%clr(%5p) ' + // Log level
                        '%clr(---){faint} %clr([%15.15t]){faint} ' + // Thread
                        '%-50.50(%logger{35}.%M) %4.4line .' + // Logger with method name
                        '%m%n%wex' // Message
    }
}

root(ERROR, ['STDOUT'])
logger("no.prpr", DEBUG, ['STDOUT'], false)

And then setting up the integration test something like:

@Integration
@Rollback
@Slf4j
class SecurityMaintenanceServiceIntegrationSpec extends Specification {

    final static String TENANT = 'test'
 
 
    def setupSpec() {
        log.debug('setupSpec')
        System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, TENANT)
    }
}

@Slf4j makes logging available for the test.