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.
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'm trying to use log4j in my webapp, I wrote a log4j configuration based on the exemple in this blog.
Files are created but there is no ouput. I tried different configurations without success
Config.groovy
def log4jFileName = System.properties.getProperty('catalina.base', '.') + "/logs/myapp.log"
def log4jStacktrace = System.properties.getProperty('catalina.base', '.') + "/logs/stacktrace.log"
environments {
development {
log4jFileName = "logs/myapp.log"
}
test {
log4jFileName = "logs/myapp.log"
}
}
log4j = {
def pattern = new PatternLayout("[%p] [%c{3}] %m%n")
appenders {
appender new DailyRollingFileAppender(
name:"file",
file:log4jFileName,
layout: pattern,
datePattern: "'.'yyyy-MM-dd",
threshold: Level.toLevel("DEBUG")
)
rollingFile name:"stacktrace",
file:log4jStacktrace,
maxFileSize:'5MB',
layout: pattern,
datePattern: "'.'yyyy-MM-dd"
console name:"stdout",
layout: pattern,
datePattern: "'.'yyyy-MM-dd"
}
root {
environments {
production {
debug "file", "stacktrace"
}
development {
debug "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'
debug 'grails.app'
}
Controller
log.info("info")
log.error("error")
log.debug("debug")
log.warn("warn")
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.
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 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'
}
}
}