2017-04-26 201 views
0

我是grails新手,需要爲我的應用程序配置loggind。我開始在我的Grails應用程序配置log4j,我得到了下面描述的這個錯誤,我不知道是什麼讓這樣的:配置log4j grails

log4j:ERROR Property missing when configuring log4j: Environment 

有人知道是什麼原因造成的?以下是我的代碼!

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 
    } 

} 
+0

您需要導入grails.util.Environment以使Environment可用。 – railsdog

回答

0

您可能可以使用日誌配置DSL構造在環境中進行分支而不顯式訪問環境。

environments { 
    development { loggers.add('console') } 
    test { loggers.add('console') } 
} 

這就是說,我不知道你想與配置的其餘部分完成的,所以有可能是處理事情更優雅的方式。