Configuring Log4J for logging level specific logs of different packages into file using appenders

09 / Jun / 2010 by Tarun Pareek 0 comments

Recently i worked upon redirecting all the log related to a specified package in my project to a specific file. It seem very helpful as it provide precise context about run of your application and make it easier to debug as compare to low level debugging method such as println. Here we can also disable certain logs statement as per our need as it follow level Inheritance.

There are several logging levels like:
ALL < DEBUG < INFO < WARN < ERROR < FATAL

The two basic things you need to specify to store log level specifically into the File : (example all logs related to INFO for com.cc package stored into file infoLog)

1. Add appender to the config.groovy, you can have more than one appender assigned respective to various level.

log4j = {
    appenders {
         file name:'infoLog',
         file:'logs/infoLog.log',
         layout:pattern(conversionPattern:'%r [%t] %-5p %c - %m%n'
    }
}

*Note : Here Layout specify conversionPattern which is a format in which log stored in file. Keywords for conversion pattern

2. Now specify the level you assigned to appender. So that only that level logs are written to the specified file in appender for particular package.
syntax : loglevel filename:’package name’
*note : Here filename as specified in appender.

log4j = {
    appenders {
         file name:'infoLog',
         file:"logs/${appName}.log",
         layout:pattern(conversionPattern:'%r [%t] %-5p %c - %m%n'
    }

    info infoLog:'grails.app.controller.com.cc'
    //similarly for error, debug, warn etc.
}

Hope this code will help !:)

Regards,
Tarun Pareek
Intelligrape Software

FOUND THIS USEFUL? SHARE IT

Tag -

Grails logging

Leave a Reply

Your email address will not be published. Required fields are marked *