Log4j XML Plugin

  • Authors : Mingfai Ma
2 votes
Dependency :
compile ":log4j-xml:0.2"

Documentation

Summary

Description

Use log4j.groovy or log4j.xml to configure your logger in Grails 1.1

Introduction

  • Grails 1.1 provides a Log4j DSL for configuration. The DSL simplified the old way of configuration and is recommended for new Grails users who don't have experience in log4j.xml. See the Log4j DSL configuration docs.
  • However, some old-school Java developers are more comfortable with log4j.xml even though the configuration file is much large. This plugin provides a way to disable the default Log4j DSL and allow the use of log4j.xml either in the original form, or in Groovy MarkupBuilder style.
Remarks: This plugin only works in Grails run-app mode but not when deploy as war. However, you could use the plugin to generete a log4j.xml for us in your app server.

Usage

Installation

grails install-plugin log4j-xml

Concepts

  • You could either use log4j.groovy or log4j.xml as the master configuration file. For both case, you should learn about log4j.xml configuration, check the following links:
  • log4j.groovy to log4j.xml transformation
    • when log4j.groovy exists under /grails-app/conf, it will be read and be transformed to a log4j.xml.
    • Any existing log4j.xml will be renamed to log4j.xml.bak (and existing log4j.xml.bak will be deleted first)
    • Read about Groovy Markup Builder if you difficulty in understanding how the transformation works
  • other notes
    • there are some mandatory 'println' console logging to let you know the plugin is working before you do any configuration
    • The configure(Closure) method of the default Grails LoggerConfig will be disabled by using Groovy EMC.

Configuration

Display log4j.xml

  • a Log4jController is included that you could read the log4j.xml at http://localhost:8080/${contextPath}/log4j/xml in development mode

Demo

  1. create a new application
    grails create-app logdemo
  2. install the plugin
    cd log4jxmldemo 
    grails install-plugin log4j-xml
  3. perform initial configuration
    • NOTICE if you use log4j.groovy, remove log4j{} block from Config.groovy. see the Issues and Limitation section
    • add a /grails-app/conf/log4j.groovy to the project with the following content
      'log4j:configuration'("xmlns:log4j": "http://jakarta.apache.org/log4j/", debug: "false") { 
       
        appender(name: 'APP_LOG', 'class': "org.apache.log4j.DailyRollingFileAppender") { 
          errorHandler 'class': "org.apache.log4j.helpers.OnlyOnceErrorHandler" 
          param name: "File", value: "logs/app.log" 
          param name: "Append", value: "false" 
          param name: "DatePattern", value: "'.'yyyy-MM-dd" 
          layout('class': "org.apache.log4j.PatternLayout") { 
            param name: "ConversionPattern", value: "%d %-5p [%-30.40c] %2X{tid} %X{sid} %X{uid} %m%n" 
          } 
        } 
       
        appender(name: "CONSOLE", 'class': "org.apache.log4j.ConsoleAppender") { 
          errorHandler 'class': "org.apache.log4j.helpers.OnlyOnceErrorHandler" 
          param name: "Target", value: "System.out" 
          param name: "Threshold", value: "TRACE" 
          layout('class': "org.apache.log4j.PatternLayout") { 
            param name: "ConversionPattern", value: "%d{ABSOLUTE} %-5p [%c{1}][%X{tid}] %m%n" 
          } 
        } 
      
        'category'(name: 'grails.app', additivity: 'false') { 
          'priority'(value: 'INFO'); 'appender-ref'('ref': "APP_LOG"); 'appender-ref'('ref': "CONSOLE") 
        } 
       
        'root' { 
          'priority' value: "WARN" 
          'appender-ref' ref: "CONSOLE" 
        } 
      } 
      
  4. creaet a controller that log
    • run
      grails create-controller logger
    • update the content of the controller as folllows:
      class LoggerController {
          def info = {
            log.info "info() - params: $params"
            render "params are logged"
           }
      }
      
  5. access the logging url and read log4j
    • start the application
      grails run-app
    • access the url http://localhost:8080/logdemo/logger?foo=bar
    • check the console to ensure the log message is shown
    • read the generated log4j.xml at http://localhost:8080/logdemo/log4j/xml

Issues and Limitation

  • You have to delete the 'log4j' node from Config.groovy when you have log4j.groovy under grails-app/conf
    • log4j.groovy is compiled to a class, and when Config.groovy is read, it treats log4j as a class and compliant "you tried to assign a value to the class 'log4j'"
    • Several options are being considered and may be adopted in future release
      • do not call the file as log4j.groovy, e.g. log4jConfig.groovy. However, it's preferable to keep the .groovy extension to allow IDE/editor to recognize it.
      • move log4j.groovy to a sub-directory, e.g. grails-app/conf/log4j/log4j.groovy
      • let Grails change its variable name. As it uses a Logger DSL anyway, it's not suppose to be log4j specific.
    • this issue is low in priority as normal Log4j XML Plugin users do not use Log4j DSL anyway
  • When you have a master application with additional plugin, this log4j xml plugin supports configuration from the master application project only. It is hardcoded to read the ./grails-app/conf/log4j.groovy or .xml
  • Dynamic reload of log4j.groovy is not implemented
    • remark: monitoring log4j.xml could be implemented by changing one line of code

Version and Compatibility

  • Grails 1.1 beta 3
    • This plugin is not designed for Grails 1.0.x

Roadmap and Tasks

  • support multiple /grails-app/conf/log4j/.groovy / .xml and merge them intelligently. this will be very useful. please give me a hand.
Author: Mingfai Ma (mingfai.ma (at-no-spam) gmail.com)