grailslogginglog4jconfigperf4j

How do I set up a log4j appender with 'appender-ref' in grails 2?


Using the Grails config DSL, how can I configure the appender-ref property on an appender?

I am trying to translate a log4j.xml from perf4j into the Grails config DSL (Grails 2.x).

In the log4j.xml file that I am trying to imitate, there is a segment like this:

log4j.xml

<appender name="CoalescingStatistics"
          class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
    <param name="TimeSlice" value="10000"/>
    <appender-ref ref="fileAppender"/>
</appender>

I want to imitate this appender's config in Grails, but I can't figure out the <appender-ref> tag.

Here is my latest attempt:

log4j = {
    appenders {
        //main appender for the app
        rollingFile ...

        //perf4j coalescing stats appender
        appender new AsyncCoalescingStatisticsAppender(name: "coalescingStatsAppender", timeSlice: 10000)

        //perf4j stats file appender
        rollingFile name: "perfLogFileAppender", file: "perfStats.log", layout: new PatternLayout("%m%n")
    }

    root {
        error 'stdout', 'rollingFileAppender'
    }

    info additivity: false, coalescingStatsAppender: [StopWatch.DEFAULT_LOGGER_NAME]
}

Additional details

Here is the source log4j.xml I am imitating:

https://web.archive.org/web/20150508124059/http://perf4j.codehaus.org/devguide.html#Using_the_log4j_Appenders_to_Generate_Real-Time_Performance_Information


Solution

  • I was able to make it work with some Groovy magic:

    log4j = {
        appenders {
            //...regular appenders
    
            //perf4j appenders
            Appender perfLogFileAppender = delegate.rollingFile name: "perfLogFileAppender", file: "perfStats.log", layout: new PatternLayout("%m%n")
            def statsAppender = new AsyncCoalescingStatisticsAppender(name: "coalescingStatsAppender", timeSlice:
                10000)
            statsAppender.addAppender(perfLogFileAppender)
            appender statsAppender
        }
        root {
            error 'stdout', 'rollingFileAppender'
        }
    
        info additivity: false, coalescingStatsAppender: [StopWatch.DEFAULT_LOGGER_NAME]
    }
    

    A more elegant solution is always welcome.