Creating custom log with file appender in Grails

Posted By : Ashish Sharma | 19-Jul-2013

There are different type of appenders available in Grails

  • jdbc - this appender is used to append log to JDBC connection
  • console - this appender is used to append log to standard out
  • file - this appender is used to append log to a file
  • rollingFile - this appender is used to append log to a rolling set of files

 

creating a custom appender
in the log4j block of Config.groovy write down the following code to create custom rolling file appender

	appenders {
		def logPattern = '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{2} - %m%n'
		appender new DailyRollingFileAppender(
			name: 'myAppender', datePattern: "'.'yyyy-MM-dd",
			layout: pattern(conversionPattern: logPattern),
			file: 'myLogFile.log'
		)
	}
	

In the above code, the DailyRollingFileAppender is created with name myAppender which is bound to the myLogFile.log. The logPattern is to apply the pattern to be print in the log file.

 

Now the main step is to configure that appender to your project at the desired level.
The level that can be used in grails to configure loggers are

  1. info
  2. error
  3. debug
  4. fatal
  5. trace
  6. off
  7. warn
  8. all

 

The following code will configure the appender to the root of the project

	root {
        debug 'myAppender'
    }
	

 

To configure the appender to selected controller or service, use the following code

	info additivity: false, myAppender: ["grails.app.controllers.com.myPackage.MyController"]
	

 

About Author

Author Image
Ashish Sharma

Ashish is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Ashish likes PC games and works out at Gym in his free time.

Request for Proposal

Name is required

Comment is required

Sending message..