I'm having a hard time to get Grails 2.4.5 to log my custom messages from my controllers into a specific file. This is what I have in the config part:
log4j = {
/*
* Log levels
off
fatal
error
warn
info
debug
trace
all
*/
appenders {
console name:'stdout', threshold: org.apache.log4j.Level.INFO
rollingFile name: 'applog', maxFileSize: 1024, file: (System.getProperty('catalina.base') ?: 'target') + '/logs/' + appName + '.log',
layout: pattern(conversionPattern: "[%d{HH:mm:ss:SSS}] %-5p %c{2}: %m%n")
environments {
development {
rollingFile name: 'grailsfile', maxFileSize: 4096, file: 'target/logs/grails.log'
file name: 'rootlog', file: 'target/logs/root.log'
}
}
}
root { error 'stdout'}
environments {
development {
debug additivity: false, 'grails.app'
root { error 'stdout', 'rootlog'} //override the root
warn additivity: false, grailsfile:
['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 additivity: false, applog:
['grails.app.controllers', 'grails.app.domain', 'grails.app.services']
}
// production {
// root { error() }
// warn "grails.app.controllers"
// warn "grails.app.domain"
// warn "grails.app.services"
// warn additivity: false, applog: 'grails.app'
// }
}
}
in the controller:
println "Debug log enabled?: " + log.debugEnabled
log.debug "First piggy wrote to Debug"
log.info "Second piggy wrote to Info"
log.error "And the third piggy wrote to Error"
That shows me "Debug log enabled?: true" on console. But the message are not logged anywhere.
The Grails internals log to "grailsfile" and also errors go to "rootlog". But appName.log stays empty.
Any hint what I am having wrong?
UPDATE
To simplify my request I have only configured the most important now. I want the lines
log.debug "First piggy wrote to Debug"
log.info "Second piggy wrote to Info"
log.error "And the third piggy wrote to Error"
End up in the appender applog (file)
So the log4j now only has:
log4j = {
appenders {
console name:'stdout'//, threshold: org.apache.log4j.Level.INFO
rollingFile name: 'applog', maxFileSize: 1024, file: (System.getProperty('catalina.base') ?: 'target') + '/logs/' + appName + '.log',
layout: pattern(conversionPattern: "[%d{HH:mm:ss:SSS}] %-5p %c{2}: %m%n")
}
environments {
development {
debug 'grails.app'
debug additivity: false
applog: ['grails.app.controllers', 'grails.app.domain', 'grails.app.services']
}
}
}
But Still I get my log lines (and also errors from other sources) into console while my custom log file stays empty.
So I simply want to get (ONLY) my log lines into the applog appender as a first step. Any hint how to achieve this?
If you are running the app in development mode, then all your logs with error level and higher are going to target/logs/root.log
It is configured here:
file name: 'rootlog', file: 'target/logs/root.log' ...
root { error 'stdout', 'rootlog'} //override the root
That's because development closure overrides your global configuration.
If you want to see all the logs in appName.log, you should set lower logging level and change appender like this:
root { info 'stdout', 'applog'} //override the root
meanwhile I could figure out that this property made the trouble for my custom log messages:
def appName = grails.util.Metadata.current.'app.name'
...
file: (System.getProperty('catalina.base') ?: 'target') + '/logs/' + appName + '.log',
changing to this works:
log4jFileName = (System.properties.'catalina.base') + "/logs/${appName}.log"
...
file: "${config.log4jFileName}",
Related
I have the following in my config.groovy
// default for all environments
log4j = { root ->
appenders {
rollingFile name:'stacktrace', file:"${logDirectory}/app_stack.log".toString(), maxFileSize:'100KB'
}
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'
debug 'com.my.code'
root.level = org.apache.log4j.Level.INFO
}
I get debug statements printed to the logs for all classes except for my groovy files placed in package com.my.code I don't get the debug statements printed. Only the info statements are being printed to the log.
Here is an example for one of the groovy classes in src/groovy
#Log4j
class SomeTest {
def someMethod() {
log.info("This will print")
log.debug("This will not print")
println log.isDebugEnabled() //prints false
print log.isInfoEnabled() //prints true
}
}
Question
How can I turn on debugging for all class under package com.my.code ? I'm on grails 2.3.5. When I change root.level to org.apache.log4j.Level.DEBUG then the debug statements do show up but that turns on DEBUG for ALL other classes as well
Here's a configuration that will log all code in packages com.my.code at the DEBUG level, and all other packages at the ERROR level.
The logs will be sent to the console and a file named appLog.txt.
log4j = {
appenders {
def logPattern = '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{2} - %m%n'
console name: 'consoleAppender', layout: pattern(conversionPattern: logPattern)
file name: "fileAppender", file: "appLog.txt"
}
root {
// define the root logger's level and appenders, these will be inherited by all other loggers
error 'consoleAppender', 'fileAppender'
}
def appNamespaces = [
'com.my.code',
'grails.app.conf.com.my.code',
'grails.app.filters.com.my.code',
'grails.app.taglib.com.my.code',
'grails.app.services.com.my.code',
'grails.app.controllers.com.my.code',
'grails.app.domain.com.my.code'
]
appNamespaces.each { debug it }
}
In grails 2.x apps I used next format (with regards to your config):
debug rollingFile 'com.my.code'
Furthermore, if you need to set different log level for a bunch of packages:
debug rollingFile: ['com.example','com.otherpackage']
I have following log4j configuration:
log4j = {
appenders {
appender new DailyRollingFileAppender(name: 'dailyAppender', ...)
}
root {
error 'dailyAppender'
}
info 'dailyAppender' : 'com.intelli', 'grails.app'
}
the "grails.app" logger is for grails artefacts (service, controllers, ..), while the "com.intelli" is for other custom loggers (like /src/groovy/**, ...) initialized with LogFactory class.
Now the issue is, that with this configuration the custom loggers ('com.intelli') were not logging anything. However grails controllers and services were logging fine!
When I changed the order of loggers:
log4j = {
...
info 'dailyAppender' : 'grails.app', 'com.intelli'
}
The custom loggers were logging fine, however grails services and controllers were not logging at all!
The solution is very tricky, you need to enclose the loggers in list ("[ ]") brackets:
log4j = {
appenders {
appender new DailyRollingFileAppender(name: 'dailyAppender', ...)
}
root {
error 'dailyAppender', additivity: false
}
info 'dailyAppender' : ['com.intelli', 'grails.app']
}
With this config, everything logs as it should.
This is not part of the issue, but to prevent double logging, we need to set additivity to false:
log4j = {
appenders {
appender new DailyRollingFileAppender(name: 'dailyAppender', ...)
}
root {
error 'dailyAppender'
}
info 'dailyAppender' : ['com.intelli', 'grails.app'], additivity: false
}
I have the following in my grails config log4j section:
appenders {
file name: "usageAppender", file: "${logDirectory}/onetract3.log"
}
root { error 'stdout', 'usageAppender' }
info usageAppender: "grails.app.services.com.onetract.onetract.UserService"
The file "onetract3.log" is successfully created, however nothing is written to this file.
I can see in the console that the info is handled correctly.
2014-03-09 20:09:06,912 [http-bio-8080-exec-5] INFO onetract.UserService - New candidateRelations for: com.onetract.onetract.Person : 18
Grails version is 2.3.5
Any ideas on why this isn't getting written to the log file?
Edit: 10.03.1014, set additivity to false.
info additivity: false
usageAppender: "grails.app.services.com.onetract.onetract.UserService"
use this
// Example of changing the log pattern for the default console appender:
//
//appenders {
// console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
//}
appenders {
rollingFile name:'myLogFile',
file:'log/myLogFile.log',
threshold: org.apache.log4j.Level.ALL,
maxFileSize:10485760
}
root {
error 'myLogFile'
additivity = true
}
info "grails.app","com.test"
warn "grails.app","com.test"
debug "grails.app","com.test"
error "grails.app","com.test"
fatal "grails.app","com.test"
trace "grails.app","com.test"
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'
The problem was due to the following entry:
file: "${logDirectory}/onetract3.log"
Before the application is running, this works, afterwards not.
The workaround is to explicitly tell the appender where the file is, i.e.
file: "logs/onetract/onetract3.log"
This then works.
I'm developing a grails 2.3.4 application and had some logging problems.
Actually, I couldn't configure any logging at all (accordingly to http://grails.org/doc/latest/guide/conf.html#logging) - had no output result.
After some brainstorming I figured out, that the grails documentation configs are not working for my project. Nevertheless, some configs variation worked fine (I saw the result on my screen):
log4j = {
appenders {
console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
}
root {
error 'stdout'
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'
debug stdout: ['edu.dm']
}
The configs like:
debug stdout: ['grails.app.services', 'grails.app.services.edu']
debug stdout: ['grails.app.controllers', 'grails.app.controllers.edu']
failed.
I would be extremely thankful, if anyone could explain my mistakes or share a link with an explanation.
The whole project can be found here: https://github.com/AlexDavljatov/EduDM/tree/master/LoggingMiniProject
Many thanks in advance.
Kind regards,
Alexander Davliatov.
Start like this:
log4j = {
root {
debug()
}
}
You should get a lot of logging to your console. Then (if that works), try this:
log4j = {
info "grails.app"
}
Among the output you should see log.info from your controllers and services. Then slowly add to the config.
Don't forget to restart between the changes in Config.groovy.
This configuration works in Grails 2.3.5 project
// log4j configuration
log4j = {
appenders {
// Use if we want to prevent creation of a stacktrace.log file.
'null' name:'stacktrace'
// Use this if we want to modify the default appender called 'stdout'.
console name:'stdout', layout:pattern(conversionPattern: '%d{yyyy-MM-dd HH:mm} -%x- %-5p-%-10c:%m%n')
// Custom log file.
/* rollingFile name:"appLog",
file:"${globalDirs.logDirectory}${appName}.log".toString(),
maxFileSize:'300kB',
maxBackupIndex:1,
layout:pattern(conversionPattern: '%d{[EEE, dd-MMM-yyyy # HH:mm:ss.SSS]} [%t] %-5p %c %x - %m%n')*/
}
// This is for the built-in stuff and from the default Grails-1.2.1 config.
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' // Jetty
error 'grails.app' // Set the default log level for our app code.
// Move anything that should behave differently into this section.
switch(Environment.current) {
case Environment.DEVELOPMENT:
// Configure the root logger to output to stdout and appLog appenders.
root {
error 'stdout'
//,'appLog'
additivity = true
}
error 'grails.plugin.springsecurity.web.filter.DebugFilter'
error "grails.plugins.twitterbootstrap"
debug "it.mypackage"
debug "org.hibernate.SQL"
debug 'grails.app.controllers'
debug 'grails.app.services'
debug 'grails.app.taglib'
debug 'grails.app.conf'
debug 'grails.app.jobs'
break
case Environment.TEST:
// Configure the root logger to only output to appLog appender.
root {
error 'stdout'
//,'appLog'
additivity = true
}
//depend how much code write in console
// debug 'grails.app.controllers'
// debug 'grails.app.domain'
// debug 'grails.app.services'
// debug 'grails.app.taglib'
// debug 'grails.app.conf'
// debug 'grails.app.filters'
break
case Environment.PRODUCTION:
// Configure the root logger to output to stdout and appLog appenders.
root {
error 'stdout'
//,'appLog'
additivity = true
}
error 'grails.app'
break
}
}
For your grails version (2.3.4 http://grails.github.io/grails-doc/2.3.4/guide/conf.html#logging) it seems the problem is you inherit the configuration level from the root.
To avoid this you need to specify additivity: false
log4j = {
...
debug additivity: false, stdout: ['grails.app.services', 'grails.app.services.edu', 'grails.app.controllers', 'grails.app.controllers.edu']
...
}
My Grails application is going to mirgate an existing database to a new database. I got some unformatted email addresses from the existing database which do not pass validation with my Grails application because of a constraint (email:true), so I get a field error.
I want write these field errors in a log file. How can I do that? I tried a Appender in log4J. It will somehow create a log file so-call "migration.log", but it does not write any field error into this log file.
log4j = {
// Example of changing the log pattern for the default console
// appender:
//
appenders {
// console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
appender new FileAppender(
name: "migrationAppender",file : "migration.log", layout: pattern(conversionPattern: "%c{2} %m%n")
)
}
This is configration. I define a FileAppender.
In my service. I just call the following:
def foundation = new Foundation(name: name, foundationName: foundationName).addToAddresses(address).addToCommunicationMedia(email)
foundation.validate()
if (!foundation.hasErrors()) {
foundation.save(flush: true)
}
else {
log.error "${foundation.errors}"
}
In the console, the errors occur and I saw a "migration.log" has been created, but somehow the file in empty.
Error 2011-09-26 09:00:29,543 [main] ERROR service.MasterDataMigrationService - org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'de.rvgmbh.nemesis.migration.domain.partner.participant.IndividualPerson' on field 'communicationMedia[0].address': rejected value [erbelrechtsanwalt-eberl.de];
log4j = {
appenders {
rollingFile name:"file", maxFileSize:(1024*1024), file:"migration.log", maxBackupIndex:10
environments {
development {
console name:'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.mortbay.log'
environments {
development {
root {
info 'file', 'stdout'
}
debug 'grails.app'
}//development
test {
root {
info 'file'
}
info 'grails.app'
}
production {
root {
info 'file'
}
info 'grails.app'
}
}
}
dev: logs to console and file from debug level
test: logs to file from info level
prod: logs to file from info level