Use logback in grails 3

Grails 3 uses logback as the default logging system.

I need an example of how to use logback in logback 3 in this form

 log.info("some Info") 
+7
logging grails logback
source share
5 answers

You must set the fourth log configuration argument to false :

 logger('grails.app.controllers', INFO, ['STDOUT'], false) 

then the log is not displayed twice.

I think because the root logger also catches INFO .

+14
source share

I just studied this because I had a similar similar question. Grails 3 uses Logback to log configuration, but AST transformation still uses the Apache log.

https://github.com/grails/grails-core/blob/master/grails-logging/src/main/groovy/org/grails/compiler/logging/LoggingTransformer.java

Then it uses the jcl-over-slf4j bridge to map all the data back to the log again.

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 so that To replace the entered log globally, you should eliminate the logging dependencies and replace the AST Transformer with your own.

The Logback plugin posted in the comment above contains the following: https://github.com/grails-plugins/grails-logback/blob/master/src/java/org/codehaus/groovy/grails/compiler/logging/Slf4jTransformer.java . However, there seems to be a lot of redundancy as Grails 3 already has log support.

If you do not want to go through this work to change the log entry, you can redefine the registrar by annotating the class with @Slf4j , and this will override the registrar for this class.

+4
source share
 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 automatically enter log .

+2
source share

I tried like this and it works great. This is from petclinic .

Further information is available 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 ) } } 
+1
source share

This was also needed in Grails 3. Found this post by mrhaki:

entry in grails 3

I tested this on quartz work. I added the following line to logback.groovy

 logger("grails.app.jobs", INFO, ['STDOUT']) 

Using the test log in a quartz job:

 log.info "test " + new Date() 

shows log information in the console

 INFO grails.app.jobs.myapp.TestJob - test Tue Oct 20 10:07:31 CEST 2015 

A funny thing is happening. I see two repeating 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 
0
source share

All Articles