logback 中的变量替换 groovy

Variable Substitution in logback groovy

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

<FileNamePattern>${logDirectory}/${logFileName}.%d{yyyy-MM-dd}.%i.html</FileNamePattern>

在调用我的 jar 之前,在 .bat 文件中设置了 logDirectory 和 logFileName。

set logFileName=foobar

但是现在,我处理groovy。它很棒,而且比 xml 可读性高得多。但是变量不再展开了。

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

路径现在是 null/null :'(

最差:以下测试抛出异常:

if ("${logConsole}" == "true") {

有人知道如何让它工作吗?

-- 编辑添加完整的 logback.groovy

/*
 * 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"]);
}

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

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