Grails 3 uses logback
as the default system of logging.
I need a example of how use logback
in grails 3 in this form
log.info("some Info")
Grails 3 uses logback
as the default system of logging.
I need a example of how use logback
in grails 3 in this form
log.info("some Info")
You have to set the fourth argument of the logger config to false
:
logger('grails.app.controllers', INFO, ['STDOUT'], false)
then the logging is not displayed twice.
I think that's because the root
logger catches INFO
too.
I was just looking into this, because I had similar a similar question. Grails 3 does use Logback for logging configuration, but the AST transform still uses Apache Commons Logging.
It then uses the jcl-over-slf4j bridge to map it all back into all back into Logback.
According to the mailing list, http://grails.1312388.n4.nabble.com/Grails-2-1-1-Is-it-possible-to-replace-the-injected-log-object-td4638834.html, to replace the injected log
globally, you have to exclude the logging dependencies and replace the AST Transformer with your own.
The Logback plugin that the blog post in the comment above mentions provides this: https://github.com/grails-plugins/grails-logback/blob/master/src/java/org/codehaus/groovy/grails/compiler/logging/Slf4jTransformer.java. However, it seems like a lot of redundancy, as Grails 3 already has Logback support.
If you don't want to go through that work to change the log injection, you can override the logger by annotating the class with @Slf4j
and that will override the logger for that class.
import org.slf4j.Logger
import org.slf4j.LoggerFactory
static Logger log = LoggerFactory.getLogger(SomeClass.class)
This works but I'm also wondering if grails 3 can auto inject log
.
I tried like this and it works fine. This is from petclinic example.
Additional details avalable at logback docs
import grails.util.BuildSettings
import grails.util.Environment
appender('STDOUT', ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "%level %logger - %msg%n"
}
}
root(ERROR, ['STDOUT'])
if(Environment.current == Environment.DEVELOPMENT) {
def targetDir = BuildSettings.TARGET_DIR
if(targetDir) {
appender("FULL_STACKTRACE", FileAppender) {
file = "${targetDir}/stacktrace.log"
append = true
encoder(PatternLayoutEncoder) {
pattern = "%level %logger - %msg%n"
}
}
logger("StackTrace", ERROR, ['FULL_STACKTRACE'], false )
}
}
Needed this as well in Grails 3. Found this post by mrhaki:
I tested this on a Quartz job. I added the following line in logback.groovy
logger("grails.app.jobs", INFO, ['STDOUT'])
Using test log in the quartz job:
log.info "test " + new Date()
shows log info in the console
INFO grails.app.jobs.myapp.TestJob - test Tue Oct 20 10:07:31 CEST 2015
Funny thing is happening. I see 2 duplicate lines for each log.info
log.info "test"
log.info "test2"
System.out.println("test"3)
Gives me:
INFO grails.app.jobs.myapp.TestJob - test
INFO grails.app.jobs.myapp.TestJob - test
INFO grails.app.jobs.myapp.TestJob - test2
INFO grails.app.jobs.myapp.TestJob - test2
test3