0
votes

I have the following in my config.groovy

// default for all environments
log4j = { root ->
    appenders {
        rollingFile name:'stacktrace', file:"${logDirectory}/app_stack.log".toString(), maxFileSize:'100KB'
    }

    error   'org.codehaus.groovy.grails.web.servlet',  //  controllers
            'org.codehaus.groovy.grails.web.pages', //  GSP
            'org.codehaus.groovy.grails.web.sitemesh', //  layouts
            'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
            'org.codehaus.groovy.grails.web.mapping', // URL mapping
            'org.codehaus.groovy.grails.commons', // core / classloading
            'org.codehaus.groovy.grails.plugins', // plugins
            'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
            'org.springframework', 'org.hibernate'
    debug  'com.my.code'
    root.level = org.apache.log4j.Level.INFO
}

I get debug statements printed to the logs for all classes except for my groovy files placed in package com.my.code I don't get the debug statements printed. Only the info statements are being printed to the log.

Here is an example for one of the groovy classes in src/groovy

@Log4j
class SomeTest {
  def someMethod() {
    log.info("This will print")
    log.debug("This will not print")
    println log.isDebugEnabled() //prints false
    print log.isInfoEnabled() //prints true
  }
}

Question

How can I turn on debugging for all class under package com.my.code ? I'm on grails 2.3.5. When I change root.level to org.apache.log4j.Level.DEBUG then the debug statements do show up but that turns on DEBUG for ALL other classes as well

2

2 Answers

0
votes

Here's a configuration that will log all code in packages com.my.code at the DEBUG level, and all other packages at the ERROR level.

The logs will be sent to the console and a file named appLog.txt.

log4j = {
    appenders {
        def logPattern = '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{2} - %m%n'
        console name: 'consoleAppender', layout: pattern(conversionPattern: logPattern)

        file name: "fileAppender", file: "appLog.txt"
    }

    root {
        // define the root logger's level and appenders, these will be inherited by all other loggers
        error 'consoleAppender', 'fileAppender'
    }

    def appNamespaces = [
            'com.my.code',
            'grails.app.conf.com.my.code',
            'grails.app.filters.com.my.code',
            'grails.app.taglib.com.my.code',
            'grails.app.services.com.my.code',
            'grails.app.controllers.com.my.code',
            'grails.app.domain.com.my.code'
    ]

    appNamespaces.each { debug it }
}
0
votes

In grails 2.x apps I used next format (with regards to your config):

 debug rollingFile 'com.my.code'

Furthermore, if you need to set different log level for a bunch of packages:

debug rollingFile: ['com.example','com.otherpackage']