Trouble enabling Grails logging - grails

I have this logging configuration in my Config.groovy file. This is a development environment, started as such. I have verified the file exists and there are 775 perms on the file, but nothing is getting output to the file.
// set per-environment serverURL stem for creating absolute links
environments {
production {
grails.serverURL = "http://www.changeme.com"
}
development {
grails.serverURL = "http://localhost:8080/${appName}"
logFilePath = "/Users/davea/Tomcat/logs/log4j.log"
}
test {
grails.serverURL = "http://localhost:8080/${appName}"
}
}
// log4j configuration
log4j = {
console name:'Appender1',
layout:pattern(conversionPattern: '%-4r [%t] %-5p %c %x - %m%n')
rollingFile name:'Appender2', maxFileSize:1024 * 1024, file:logFilePath,
layout:pattern(conversionPattern: '%-4r [%t] %-5p %c %x - %m%n')
root {
debug 'Appender1', 'Appender2'
}
}
Can anyone tell what's wrong with my configuration? Thanks, - Dave

have you tried to log on different log levels than DEBUG? here is an example with different logging settings for each environment.
log4j = {
appenders {
// for all environments
rollingFile name:"file", maxFileSize:(1024*1024), file:"logs\\logfile.log", maxBackupIndex:100
environments {
development {
console name:'stdout'
}
}
}
// default log level for some loggers
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 {
debug 'file', 'stdout'
}
debug 'grails.app' // <--- maybe you forgot to define this logger for your app!
}//development
test {
root {
info 'file'
}
info 'grails.app'
}
production {
root {
error 'file'
}
error'grails.app'
}
}
}

Related

Configuring log4j grails

I'm new in grails and I need to configuring loggind for my application. I started to configure log4j in my grails application and I got this error described below and I don't know what makes this:
log4j:ERROR Property missing when configuring log4j: Environment
Someone know what causes it? Below is my code!
def myappLogLevel = "DEBUG"
def myappLogPattern = "%d{yyyy-MM-dd/HH:mm:ss.SSS} [%t] %x %-5p %c{2} -
%m%n"
def log4jFileName = System.properties.getProperty('catalina.base', '.') +
"/logs/myapp.log"
environments {
development {
log4jFileName = "logs/myapp.log"
}
test {
log4jFileName = "logs/myapp.log"
}
log4j = {
def logLayoutPattern = new PatternLayout(myappLogPattern)
appenders {
appender new DailyRollingFileAppender(name: "myappLog",
threshold: Level.toLevel(myappLogLevel),
file: log4jFileName,
datePattern: "'.'yyyy-MM-dd", //Rollover at midnight each day.
layout: logLayoutPattern
)
if (Environment.current == Environment.DEVELOPMENT ||
Environment.current == Environment.TEST) {
appender new ConsoleAppender(name: "console",
threshold: Level.toLevel(myappLogLevel),
layout: logLayoutPattern
)
}
}
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
'org.springframework',
'org.hibernate.cache',
'org.hibernate',
'net.sf.ehcache.hibernate'
'grails'
'groovyx.net.http'
warn 'org.springframework',
'org.hibernate',
'grails.plugins.springsecurity',
'groovyx.net.http'
debug 'grails.plugins.springsecurity',
'grails.plugin.springcache',
'com.myapp',
'org.codehaus.groovy.grails.plugins.springsecurity',
'org.apache.http.headers',
'grails.app.services',
'grails.app.domain',
'grails.app.controllers',
'grails.plugin.databasemigration',
'liquibase'
List<String> loggers = []
loggers.add('myappLog')
if (Environment.current.name == "development" ||
Environment.current.name == "test") {
loggers.add('console')
}
root {
error loggers as String[]
additivity = true
}
}
You may be able to use the log config DSL construction to branch on environment without explicitly accessing Environment.
environments {
development { loggers.add('console') }
test { loggers.add('console') }
}
That said, I am not sure what you are trying to accomplish with the rest of the configuration, so there may be a more elegant way to handle things.

Grails logging not working

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']
...
}

Grails service logging does not initialize

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.

Grails empty log file with log4j

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
}

Grails not printing messages to log file using custom appender

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

Resources