2014-09-30 58 views
0

我有一個控制器,我想根據級別登錄到不同的appender。因此,對於大量的常規用法將寫入INFO usage.log。我確定的某些事件應該轉到WARN warning.log。如何從一個控制器登錄到不同的appenders w/Grails&log4j

這裏是我的log4j配置的一部分...

appenders { 
    file name: "usageAppender", 
     file: " /usr/local/logs/cs-wst/usage.log" 

    file name: "warningAppender", 
     file: " /usr/local/logs/cs-wst/warning.log" 
} 

info usageAppender: 'grails.app' 

當我做到這一點,在我的控制器log.info("request method fired")運行這樣的語句行不會在usage.log顯示出來,但我有麻煩其他級別的工作。我想在控制器中運行這兩條語句

log.info("request method fired") 
… 
log.warn("invalid credentials") 

並讓它們記錄到相應的文件中。誰能幫忙?

回答

1

請參考下面的代碼;

appenders { 

    appender new DailyRollingFileAppender(
      name: 'dailyerrorAppender', 
      threshold: org.apache.log4j.Level.DEBUG, 
      datePattern: "'.'yyyy-MM-dd", // See the API for all patterns. 
      fileName: "./error", 
      layout: pattern(conversionPattern:'%d [%t] %-5p %c{2} %x - %m%n') 
    ) 

    appender new DailyRollingFileAppender(
      name: 'dailywarnAppender', 
      threshold: org.apache.log4j.Level.WARN, 
      datePattern: "'.'yyyy-MM-dd", // See the API for all patterns. 
      fileName: "./warn", 
      layout: pattern(conversionPattern:'%d [%t] %-5p %c{2} %x - %m%n') 
    ) 

} 
environments { 
    development{ 
     debug dailyerrorAppender:"errors", additivity: false 
     warn dailywarnAppender:"warns", additivity: false 
    } 
    qa{ 
     debug dailyerrorAppender:"errors", additivity: false 
     warn dailywarnAppender:"warns", additivity: false 
    } 
    production{ 
     debug dailyerrorAppender:"errors", additivity: false 
     warn dailywarnAppender:"warns", additivity: false 
    } 
} 

def errorLog = Logger.getLogger("errors") 

errorLog.debug("your error messages") 

def warnLog = Logger.getLogger("warns") 

warnLog.warn("your warn messages") 
相關問題