变量未被读取为系统属性
Variable not being read as a system property
我试图从系统属性中读取我的 logback 配置中的变量,但没有成功。我定义了一个非常简单的 logback.groovy 如下:
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.FileAppender
import static ch.qos.logback.classic.Level.DEBUG
appender("FILE", FileAppender) {
file = "${USER_HOME}/myApp.log"
encoder(PatternLayoutEncoder) {
pattern = "%msg%n"
}
}
root(DEBUG, ["FILE"])
然后一个简单的Test.scala:
import org.slf4j.LoggerFactory
object Test {
val log = LoggerFactory.getLogger(getClass)
def main(args: Array[String]): Unit = {
log.debug("Where does the logging go?")
}
}
然后运行
sbt -DUSER_HOME="/tmp"
但是 logback 不获取系统属性。下面是我运行程序时的输出:
22:03:30,768 |-INFO in ch.qos.logback.classic.LoggerContext[default] -
Found resource [logback.groovy] at
[file:/home/des/workspace/play/target/scala-2.10/classes/logback.groovy]
22:03:31,449 |-INFO in
ch.qos.logback.classic.gaffer.ConfigurationDelegate@5c3286d0 - About
to instantiate appender of type [ch.qos.logback.core.FileAppender]
22:03:31,450 |-INFO in
ch.qos.logback.classic.gaffer.ConfigurationDelegate@5c3286d0 - Naming
appender as [FILE] 22:03:31,515 |-ERROR in
ch.qos.logback.classic.gaffer.AppenderDelegate@7930f0f9 - Appender
[FILE] of type [ch.qos.logback.core.FileAppender] has no appplicable
[USER_HOME] property 22:03:31,556 |-INFO in
ch.qos.logback.core.FileAppender[FILE] - File property is set to
[null/myApp.log] 22:03:31,562 |-INFO in
ch.qos.logback.classic.gaffer.ConfigurationDelegate@5c3286d0 - Setting
level of logger [ROOT] to DEBUG 22:03:31,569 |-INFO in
ch.qos.logback.classic.gaffer.ConfigurationDelegate@5c3286d0 -
Attaching appender named [FILE] to Logger[ROOT]
请注意,当我在我的 logback 文件中定义 USER_HOME 时,它工作正常
您需要执行以下操作
sbt '; set javaOptions += "-DUSER_HOME="/tmp" ; runMain Test'
或
更改您的 build.sbt
以包含
fork := true
envVars := Map("USER_HOME" -> "/tmp")
如果您想从生产环境 运行 不使用 sbt
,您可以直接将其传递(使用 -DUSER_HOME="/tmp")到 运行创建 Play 分发(使用 dist
)命令时生成的脚本。
因为我正在使用groovy。认为这是一个 logback 错误,我这样报告并解决了:http://jira.qos.ch/browse/LOGBACK-1041
我试图从系统属性中读取我的 logback 配置中的变量,但没有成功。我定义了一个非常简单的 logback.groovy 如下:
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.FileAppender
import static ch.qos.logback.classic.Level.DEBUG
appender("FILE", FileAppender) {
file = "${USER_HOME}/myApp.log"
encoder(PatternLayoutEncoder) {
pattern = "%msg%n"
}
}
root(DEBUG, ["FILE"])
然后一个简单的Test.scala:
import org.slf4j.LoggerFactory
object Test {
val log = LoggerFactory.getLogger(getClass)
def main(args: Array[String]): Unit = {
log.debug("Where does the logging go?")
}
}
然后运行
sbt -DUSER_HOME="/tmp"
但是 logback 不获取系统属性。下面是我运行程序时的输出:
22:03:30,768 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.groovy] at [file:/home/des/workspace/play/target/scala-2.10/classes/logback.groovy] 22:03:31,449 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@5c3286d0 - About to instantiate appender of type [ch.qos.logback.core.FileAppender] 22:03:31,450 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@5c3286d0 - Naming appender as [FILE] 22:03:31,515 |-ERROR in ch.qos.logback.classic.gaffer.AppenderDelegate@7930f0f9 - Appender [FILE] of type [ch.qos.logback.core.FileAppender] has no appplicable [USER_HOME] property 22:03:31,556 |-INFO in ch.qos.logback.core.FileAppender[FILE] - File property is set to [null/myApp.log] 22:03:31,562 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@5c3286d0 - Setting level of logger [ROOT] to DEBUG 22:03:31,569 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@5c3286d0 - Attaching appender named [FILE] to Logger[ROOT]
请注意,当我在我的 logback 文件中定义 USER_HOME 时,它工作正常
您需要执行以下操作
sbt '; set javaOptions += "-DUSER_HOME="/tmp" ; runMain Test'
或
更改您的 build.sbt
以包含
fork := true
envVars := Map("USER_HOME" -> "/tmp")
如果您想从生产环境 运行 不使用 sbt
,您可以直接将其传递(使用 -DUSER_HOME="/tmp")到 运行创建 Play 分发(使用 dist
)命令时生成的脚本。
因为我正在使用groovy。认为这是一个 logback 错误,我这样报告并解决了:http://jira.qos.ch/browse/LOGBACK-1041