gpt4 book ai didi

java - logback groovy 中的变量替换

转载 作者:行者123 更新时间:2023-11-30 08:12:17 34 4
gpt4 key购买 nike

我正在使用 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/环境变量。

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

关于java - logback groovy 中的变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30641302/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com