Search code examples
springlogginggrailslog4j

log4j with Grails 3.2.3: I don't get any logging


I am not able to get log4j to work in a project with Grails 3.2.3. My aim is to get detailed logs of the spring security plugin, because I have some problems with it and want to know whats going on (I cannot login although I provide correct credentials).

I added the following lines to grails-app/conf/application.groovy:

log4j = {
    all 'org.springframework'
        'org.grails.plugins:spring-security-core'
    debug 'grails.app'

    appenders {
        file name: 'file', file:'logs/logging.log'
    }

    root {
        all 'stdout', 'file'
    }
}

But when I run the application with grails run-app or run-app in the interactive mode, no file is created, and there is no output in the console, even if I try to log in and the login fails (which should of course create some log messages, especially because of the log level "all").

I am no expert with log4j. I never used it before. I read through some tutorials and documentation, but they are all related to plain java programmes, not Grails.

Most of my Grails configuration is in YAML (application.yml), only the spring and log4j settings are in a separate application.groovy. As far as I understand, Grails merges them into one Configuration Object.

What do I miss in my configuration?


Solution

  • Per default logback is used in Grails 3.x for logging. In order to activate it, you must have a logback.groovy in your grails-app/conf. Usually it looks like:

    import grails.util.BuildSettings
    import grails.util.Environment
    import ch.qos.logback.classic.encoder.PatternLayoutEncoder
    import ch.qos.logback.core.ConsoleAppender
    import ch.qos.logback.core.FileAppender
    
    // See http://logback.qos.ch/manual/groovy.html for details on configuration
    appender( 'STDOUT', ConsoleAppender ) {
      encoder( PatternLayoutEncoder ){ pattern = '%d{HH:mm:ss.SSS} [%thread] %level %logger{36} - %msg%n' }
    }
    
    root INFO, ['STDOUT']
    
    logger 'org.springframework.security.web.authentication.rememberme', DEBUG
    
    def targetDir = BuildSettings.TARGET_DIR
    if( Environment.developmentMode && targetDir ){
      appender( 'FULL_STACKTRACE', FileAppender ){
        file = "${targetDir}/stacktrace.log"
        append = true
        encoder( PatternLayoutEncoder ){ pattern = '%d{HH:mm:ss.SSS} [%thread] %level %logger{36} - %msg%n' }
      }
      logger 'StackTrace', ERROR, ['FULL_STACKTRACE'], false
    }
    

    Here the Spring Sec's rememberme services are logged to stdout with DEBUG level