在logback groovy中的变量替换

时间:2015-06-04 10:23:42

标签: java variables groovy logback logback-groovy

我正在使用logback.xml,变量加载到字符串中,如:

.bat

在调用我的jar之前,在set logFileName=foobar 文件中设置了logDirectory和logFileName。

appender("FILE", RollingFileAppender) {
file = "${logDirectory}/${logFileName}.html"
    rollingPolicy(TimeBasedRollingPolicy) {
        fileNamePattern = "${logDirectory}/${logFileName}.%d{yyyy-MM-dd}.%i.html"
        ...
    }
...
} 

但现在,我处理groovy。它比xml更令人敬畏,更可读。但变量不再扩大。

null/null

路径现在是if ("${logConsole}" == "true") { :'(

最糟糕的是:以下测试抛出异常:

/*
 * The configuration use the following variables :
 * logDirectory => The log folder
 * logFileName => The log file name
 * logConsole => true console activation.
 **/

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.html.HTMLLayout
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.encoder.LayoutWrappingEncoder
import ch.qos.logback.core.rolling.RollingFileAppender
import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy

import static ch.qos.logback.classic.Level.INFO
import static ch.qos.logback.classic.Level.OFF

def LOG_DIRECTORY = System.getProperty("logDirectory")
def LOG_FILE_NAME = System.getProperty("logFileName")
def LOG_CONSOLE = System.getProperty("logConsole")

appender("FILE", RollingFileAppender) {
    file = "${LOG_DIRECTORY}/${LOG_FILE_NAME}.html"
    rollingPolicy(TimeBasedRollingPolicy) {
        fileNamePattern = "${LOG_DIRECTORY}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}.%i.html"
        timeBasedFileNamingAndTriggeringPolicy(SizeAndTimeBasedFNATP) {
            maxFileSize = "100MB"
        }
        maxHistory = 5
    }
    encoder(LayoutWrappingEncoder) {
        layout(HTMLLayout) {
            pattern = "%level%date%logger{36}%msg"
        }
    }
}
appender("STDOUT", ConsoleAppender) {
    withJansi = true
    encoder(PatternLayoutEncoder) {
        pattern = "%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{36}) - %msg%n"
    }
}
logger("org", INFO)
logger("net", INFO)
logger("freemarker", INFO)

// We don't care about the bean creation at least it's more than a warn
logger("org.springframework.beans.factory", WARN)

root(OFF, ["FILE"])

if (LOG_CONSOLE == "true") {
    root(OFF, ["FILE","STDOUT"]);
}

任何人都知道如何让它发挥作用?

- 编辑添加完整的logback.groovy

{{1}}

1 个答案:

答案 0 :(得分:2)

System.getProperty()调用检索java命令行中使用-D设置的属性,而在.bat文件中设置shell /环境变量。

尝试使用System.getenv()文件中的logback.groovy