2013-12-19 101 views
0

Groovy ConfigSlurper http://groovy.codehaus.org/ConfigSlurper舉例說明了如何使用ConfigSlurper定義Log4j配置。如何將Configslurper配置轉換爲Log4j?

E.g.

log4j { 
    appender.stdout = "org.apache.log4j.ConsoleAppender" 
    appender."stdout.layout"="org.apache.log4j.PatternLayout" 
    rootLogger="error,stdout" 
    logger { 
     org.springframework="info,stdout" 
    } 
    additivity { 
     org.springframework=false 
    } 
} 

基本上ConfigSlurper只是還給了Java類型嵌套的地圖(如String),布爾等

的是,真正使用這些配置設置,並將其應用到底層的Log4j系統的最佳方法是什麼?

注:我沒有使用Grails,而是香草泉。

+0

不知道,但確實:'PropertyConfigurator.configure(新ConfigSlurper()解析(新文件(」 config.groovy').toURL()).toProperties())'工作? –

回答

0

像蒂姆·耶茨說:

org.apache.log4j.PropertyConfigurator.configure(
    new ConfigSlurper().parse(new File("config.groovy").toURL()).toProperties()) 

出色的作品!

謝謝Tim!

0

您可以從代碼直接配置log4j的,而不需要創建一個屬性文件是這樣的:

def log4jConfig = """ 
log4j { 
    appender.stdout = "org.apache.log4j.ConsoleAppender" 
    appender."stdout.layout"="org.apache.log4j.PatternLayout" 
    rootLogger="error,stdout" 
    logger { 
     org.springframework="info,stdout" 
    } 
    additivity { 
     org.springframework=false 
    } 
} 
""" 

def config = new ConfigSlurper().parse(log4jConfig) 
PropertyConfigurator.configure(config.toProperties()) 
相關問題