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
- info
- error
- debug
- fatal
- trace
- off
- warn
- 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"]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.