grails (11) vaadin (11) meteor (6) java (4) elasticsearch (3) apple (2) centos (1) cloudbees (1) google analytics (1) gradle (1) heroku (1) javafx (1) javascript (1) jdbc (1) jug (1) logback (1) logging (1) mac os (1) management (1) mongodb (1) mongolab (1) mysql (1) twitter (1) ubuntu (1)

Thursday, August 15, 2013

Grails and Logback: Provide default logback configuration for development environment

The main motivation is to have a custom logger setup, which every developer can easily adjust. Also, we don't want to put logback config, which is meant for developers only, on the class path.

We can place logback.xml into the root folder of our project and then pass it when running up the application
grails -Dlogback.configurationFile=logback.xml run-app
The biggest disadvantage is: it is annoying to always include additional properties in the command line and it also means you need to tell that to the new developers. Also, it is not following Grails principle "convention over configuration".

We need to provide the default configuration for the development mode if logback.configurationFile property is not set.

So we hook after compilation event as follows. Create or open scripts/_Events.groovy file in your project folder.

Here is the content of _Events.groovy file.

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.joran.JoranConfigurator
import ch.qos.logback.core.ConsoleAppender
import grails.util.Environment
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.slf4j.LoggerFactory;
Log log = LogFactory.getLog("_Events")
def activateLogbackFromConfigFile = { LoggerContext loggerContext ->
JoranConfigurator configurator = new JoranConfigurator()
configurator.context = loggerContext
configurator.doConfigure('logback-config-dev.xml')
}
def activateLogbackProgrammatically = { LoggerContext loggerContext ->
PatternLayoutEncoder encoder = new PatternLayoutEncoder()
encoder.context = loggerContext
encoder.pattern = "%date{ISO8601} Thread=\"%thread\" %-5level logger=\"%logger{136}\" message=\"%msg\"%n"
encoder.start()
ConsoleAppender consoleAppender = new ConsoleAppender()
consoleAppender.name = 'console'
consoleAppender.context = loggerContext
consoleAppender.encoder = encoder
Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)
rootLogger.level = Level.WARN
rootLogger.addAppender(consoleAppender)
loggerContext.getLogger("com.example").level = Level.INFO
// loggerContext.getLogger("org.codehaus.groovy.grails").level = Level.INFO
// loggerContext.getLogger("org.springframework").level = Level.INFO
// loggerContext.getLogger("org.hibernate").level = Level.INFO
// loggerContext.getLogger("net.sf.ehcache.hibernate").level = Level.INFO
// loggerContext.getLogger("org.springframework.web.client").level = Level.INFO
consoleAppender.start()
}
eventCompileEnd = {
boolean isCurrentEnvDevelopment = Environment.DEVELOPMENT.equals(Environment.currentEnvironment)
if (isCurrentEnvDevelopment) {
String logbackConfig = System.properties["logback.configurationFile"]
if (!logbackConfig) {
logbackConfig = System.env["logback.configurationFile"]
}
boolean isLogbackConfigProvided = logbackConfig != null
if (isLogbackConfigProvided) {
LogFactory.getLog("_Events").info "Logback is using: $logbackConfig"
} else {
log.info 'Logback is set for development mode. Change logback-config-dev.xml in order to adjust logging levels or change logging programmatically in _Events.groovy script.'
// if we are in dev environment and logback is not provided, use this configuration
LoggerContext loggerContext = LoggerFactory.getILoggerFactory()
loggerContext.reset()
// activateLogbackFromConfigFile loggerContext
// or you can do this for dev needs (replace doConfigure method call)
activateLogbackProgrammatically loggerContext
}
}
}
view raw _Events.groovy hosted with ❤ by GitHub
In this example logback-config-dev.xml is ment to be stored in the project root folder.

No comments:

Post a Comment