Configuration of log4j logs in Grails

Source: Internet
Author: User
Tags grails log4j

Grails uses log4j log information and log4j configuration information is written in Config.groovy files.

One, Grails 1.1 can specify different logging levels for the code under different packages, such as:

log4j = {
// 设置控制器和GSP页面的级别为error
error  'org.codehaus.groovy.grails.web.servlet',  //  controllers
'org.codehaus.groovy.grails.web.pages' //  GSP

// 设置插件中的日志级别为warn
warn   'org.codehaus.groovy.grails.plugins'
}

Ii. Define your own Appender

Grails default appender is to output logs to the console

appenders {
console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
}

LOG4J supports many types of appender, such as: Jdbc,console,file,rollingfile

Log4j's appender also supports multiple log output formats: Xml,html,simple,pattern

You can refer to Log4j's documentation for specific purposes:

Http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html

Http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Layout.html

The commonly used Appender is to generate 1 log files per day, configured as follows:

appenders {
appender new org.apache.log4j.DailyRollingFileAppender(name:"dailyAppender",layout:pattern(conversionPattern: '%c{2} %m%n'),fileName:"e:\\grails.log",datePattern:"'.'yyyy-MM-dd")
}

We can also define multiple appender and specify different appender for different levels of logging, such as:

log4j = {
appenders {
appender new org.apache.log4j.DailyRollingFileAppender(name:"dailyAppender",layout:pattern(conversionPattern: '%c{2} %m%n'),fileName:"e:\\grails.log",datePattern:"'.'yyyy-MM-dd")
console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
}

// error级别的日志采用dailyAppender记录到文件中
error  dailyAppender:'org.codehaus.groovy.grails.web.servlet',  //  controllers
'org.codehaus.groovy.grails.web.pages' //  GSP

// error级别的日志则使用stdout直接输出到控制台
warn   stdout:'org.codehaus.groovy.grails.plugins'
}

Third, define root Logger

Although we can define multiple Appender and specify different appender for different levels, we generally want to define only 1 Appender,

The output of all logs uses this appender, which requires the use of root logger.

appenders {
appender new org.apache.log4j.DailyRollingFileAppender(name:"dailyAppender",layout:pattern(conversionPattern: '%c{2} %m%n'),fileName:"e:\\grails.log",datePattern:"'.'yyyy-MM-dd")
}
root{
error 'dailyAppender'
additivity = true
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.