The only way I can get the logging to work in my service is to add this to my service:
class MyService {
def log = LogFactory.getLog(getClass())
...
My log4j settings in Config.groovy:
log4j = {
PatternLayout patternLayout = new PatternLayout("%d [%t] %-5p %c %x - %m%n")
debug 'grails.app.controllers',
'grails.app.controller',
'grails.app.domain',
'grails.app.service',
'grails.app.filters',
'com.mycompany'
// 'org.springframework.security'
'org.hibernate.SQL'
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'
appenders {
appender new org.apache.log4j.ConsoleAppender(name: "console",
threshold: org.apache.log4j.Level.DEBUG,
layout: patternLayout
)
}
root {
error 'stdout'
additivity = true
}
}
I would have thought the logging would work in a service without having to add def log... at the top.
grails.app.service should be grails.app.services. This changed in version 2.0.
You can easily find out the logger name in an artifact by adding
println log.name
to a test method and calling it.
Related
I use Grails 2.4.4 and Oracle JDK 1.7.45
The below is my logger configuration in Config.groovy:
log4j.main = {
appenders {
console name:'stdout', layout:pattern(conversionPattern: '%d{yy/MM/dd HH:mm:ss.SSS} %p %c{1}.%M(%L): %m%n')
}
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'
debug 'grails.app'
}
My controller is :
class HomeController {
def index() {
log.debug("HELLO, WORLD")
render "HELLO, WORLD"
}
}
The log result looks like below:
15/04/08 13:15:26.723 DEBUG HomeController.debug(128) : HELLO, WORLD
How do I config Config.groovy so that the log result can correctly display method and line number like below:
15/04/08 13:15:26.723 DEBUG HomeController.index(3) : HELLO, WORLD
All my logging statements from Domain Classes never end up in the my 'stdout'.
I thought this line should do the trick:
stdout: 'grails.app.domain',
It works for all other stuff Servics, Cotrollers but not for Domain classes.
log4j = { root->
appenders {
console name:'stdout'
rollingFile name:'file', maxFileSize: 5000000, maxBackupIndex:10, file:"${catalinaBase}" + File.separator + "logs" + File.separator + "tao.log"
appender new EventLogAppender(source:'SC', name: 'eventLogAppender', layout:new EnhancedPatternLayout(conversionPattern: '%d{DATE} %5p %c{1}:%L - %m%n %throwable{500}'), threshold: org.apache.log4j.Level.ERROR)
}
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'
all file: 'grails.app.controllers',
stdout:'grails.app.controllers',
eventLogAppender:'grails.app.controllers'
all file: 'grails.app.services',
stdout:'grails.app.services',
eventLogAppender:'grails.app.services'
all file: 'grails.app.domain',
stdout: 'grails.app.domain',
eventLogAppender:'grails.app.domain'
all file: 'grails.app.jobs',
stdout:'grails.app.jobs',
eventLogAppender: 'grails.app.jobs'
all file: 'grails.app.conf.Config',
stdout:'grails.app.conf.Config',
eventLogAppender:'grails.app.conf.Config'
}
For me the class/package "wildcard"-definition were always not clear, till I found a simple trick. In your class add:
println log.name
and the output is the desired class name to be included into the log4j config
In my grails app I have lg4j config as follows
log4j = {
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,
'net.sf.ehcache.hibernate'
warn 'org.hibernate', 'grails.app', 'org.springframework', 'org.springframework.security'
info 'org.springframework', 'org.springframework.security', 'grails.app'
appenders {
def patternLayout = new PatternLayout()
patternLayout.setConversionPattern("[%r] %c{2} %m%n")
def mailAppender = new SMTPAppender()
mailAppender.setFrom("from#webbfontaine.am")
mailAppender.setTo("to#webbfontaine.am")
mailAppender.setSubject("[Error in application] [host:test.am]")
mailAppender.setSMTPHost("smtp.test.am")
mailAppender.setLayout(patternLayout)
mailAppender.setThreshold(org.apache.log4j.Level.WARN)
appender name:'mail', mailAppender
}
root {
warn 'mail', 'stdout'
}
}
It works fine and sends mail when I run my app with grails run-app, but when I deploy war under tomcat, I get emails with empty body. Is there some specific jar that should be under tomcat/libs? Any ideas?
it seems that my grails (2.1) log4j config isn't used properly and i can't find the problem.
The log file is created but empty and the stdout doesn't follow my pattern.
log4j = {
appenders {
rollingFile name: "myFileAppender", file: "LogFile.log", maxFileSize: 1024, layout:pattern(conversionPattern: '%d (%t) [%24F:%-3L:%-5p]%x %m%n')
console name: 'myStdoutAppender', layout:pattern(conversionPattern: '%d (%t) [%24F:%-3L:%-5p]%x %m%n')
}
warn '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'
debug 'org.my.stuff',
'grails.test',
'grails.app'
root = {
debug 'myFileAppender', 'myStdoutAppender'
additivity = true
}
}
Anyone an idea what i did wrong?
Don't assign to the root logger. Just call it with the supplied closure:
// No equals sign
root {
debug 'myFileAppender', 'myStdoutAppender'
additivity = true
}
I have an issue when i war my grails project to deploy on my production server there is a limitation that i only log to /var/log/tomcat5/catalina.out. This means that i have to make every log including stacktrace write to that one file. I've tried following other examples and it just doesn't seem to work i still get the error "permission denied on stacktrace.log"
This is my log4j config
log4j = {
// Example of changing the log pattern for the default console
// appender:
//
appender.stacktractLog = "org.apache.log4j.FileAppender"
appender.'stacktraceLog.File'="/var/log/tomcat5/catalina.out"
appenders {
rollingFile name:'catalinaOut', maxFileSize:1024, fileName:"/var/log/tomcat5/catalina.out"
}
root {
error 'catalinaOut'
additivity = true
}
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.mortbay.log'
}
Just modify your configuration:
log4j = {
// Example of changing the log pattern for the default console
// appender:
//
appenders {
rollingFile name:'catalinaOut', maxFileSize:1024, fileName:"/var/log/tomcat5/catalina.out"
file name: 'stacktrace', file: "/var/log/tomcat5/catalina.out", layout: pattern(conversionPattern: '%c{2} %m%n')
}
root {
error 'catalinaOut'
additivity = true
}
error stacktrace: "StackTrace"
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.mortbay.log'
}
Can't help with your current problem but I suggest you change the rollingFile properties. Current setting will only give a 1kb file that over writes it's self.
try:
rollingFile name:'catalinaOut', maxFileSize:${10*1024*1024},maxBackupIndex:100,fileName:"/var/log/tomcat5/catalina.out"
This will give you 100 10MB files or about a gig worth of logs.