Skip to content

Instantly share code, notes, and snippets.

@steinwaywhw
Created June 27, 2014 14:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save steinwaywhw/ff46c692a50c8dd4cdc3 to your computer and use it in GitHub Desktop.
Save steinwaywhw/ff46c692a50c8dd4cdc3 to your computer and use it in GitHub Desktop.
Sample log4j configuration for Grails application

Configuring log4j in Grails

Special thanks to this blog. This is the best blog on log4j configuration for Grails I've ever read.

Tips

  • If you wanna reference your own configuration in log4j config section, use Holders as shown
  • If you wanna use DailyRollingFileAppender or any other appenders that are not pre-loaded, please import org.apache.log4j.*
  • You can define variables like that
  • You can use different environment settings
import org.apache.log4j.*
import grails.util.Holders
myconfig {
myvariable {
workdir = 0
}
}
log4j = {
def pattern = new PatternLayout("[%p] [%c{3}] %m%n")
appenders {
appender new DailyRollingFileAppender(
name:"file",
file:"${Holders.config.myconfig.myvariable.workdir}/app.log",
layout: pattern,
datePattern: "'.'yyyy-MM-dd")
rollingFile name:"stacktrace",
file:"${Holders.config.myconfig.myvariable.workdir}/stacktrace.log",
maxFileSize:'100KB'
console name:"stdout",
layout: pattern
}
root {
environments {
production {
error "file"
}
development {
error "file", "stdout"
}
}
}
error 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'org.codehaus.groovy.grails.web.sitemesh', // layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate',
'net.sf.ehcache.hibernate'
warn 'org.springframework',
'org.hibernate',
'grails.plugins.springsecurity',
'groovyx.net.http'
all 'grails.app'
}
@ujjwol05
Copy link

ujjwol05 commented Dec 21, 2017

My config.groovy

def pattern = new PatternLayout('%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{2} - %m%n')
    environments{
        development{
            appenders{
                appender new ConsoleAppender(
                        name: 'stdout',
                        layout: pattern
                )
                appender new DailyRollingFileAppender(
                        name:"file",
                        file:"/home/logs/app.log",
                        layout: pattern,
                        datePattern: "'.'yyyy-MM-dd")            }
        }
        production{
            appenders {
                appender new DailyRollingFileAppender(
                        name:"file",
                        file:"/home/logs/app.log",
                        layout: pattern,
                        datePattern: "'.' yyyy-MM-dd")

                rollingFile name:"stacktrace",
                        file:"/home/logs/stacktrace.log",
                        maxFileSize:'10mb'

                appender new ConsoleAppender(
                        name: 'stdout',
                        layout: pattern
                )
            }
        }
    }
    root {
        info("file" ,"stdout")
           }

Rollover at midnight each day not working . It only saves single file app.log without date ,According to the documentation ,there should be 1 log each day which should roll over at midnight eachday .Is there something that i am missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment