1

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?

Tung
  • 1,579
  • 4
  • 15
  • 32
waldiphil
  • 75
  • 3
  • 8

2 Answers2

3

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

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Thanks a lot! For some reason I missed this information in the Grails doc. Now the log is created. Still some fine tuning to do, but it works thanks to you. – waldiphil Jan 12 '17 at 14:24
1

You didn't mention the version of Log4j, however, I guess it was Log4j2 as you said Grails 3.2.3. Therefore, my recommendation below is based on Log4j2 manual. According to Log4j2 manual, you must have a configuration file in the project classpath. The classpath can be src/main/groovy/resources or grails-app/conf. The configuration file can be a {xml|json|yaml} as guided in the manual.

Below is a very simple content of the configuration file that is working for my project.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Properties>
        <Property name="baseDir">logs</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
        <RollingFile name="RollingFile" fileName="${baseDir}/app.log"
                     filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyyMMdd}.log.gz"
                     filePermissions="rw-------">
            <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
            <CronTriggeringPolicy schedule="0 0 0 * * ?"/>
            <DefaultRolloverStrategy stopCustomActionsOnError="true">
                <PosixViewAttribute basePath="${baseDir}/$${date:yyyy-MM}" filePermissions="r--r--r--">
                    <IfFileName glob="*.gz" />
                </PosixViewAttribute>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="debug" additivity="false">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

Also, there is a question on this community I reckon you might need just in case. The question tried to detail how to configure log4j2 in Grails 3.

I hope this explanation would help clear your issue.

Tung
  • 1,579
  • 4
  • 15
  • 32