我可以在SLF4J Simple的props文件中指定环境变量吗?

时间:2013-10-09 17:23:45

标签: java logging configuration environment-variables slf4j

根据this link SLF4J可以通过在运行时类路径上放置simplelogger.properties文件来配置Simple。

simplelogger.properties的一个例子:

org.slf4j.simpleLogger.logFile=/home/myuser/.myapp/logs/myapp.log
org.slf4j.simpleLogger.defaultLogLevel=INFO
org.slf4j.simpleLogger.levelInBrackets=false

但是,如果我希望我的应用定义自己的环境变量,例如MYAPP_HOME,并将其设置为值/home/myuser/.myapp/logs,该怎么办?那么我如何从MYAPP_HOME内引用simplelogger.properties变量?这甚至可能吗?

例如,我可以这样做:

org.slf4j.simpleLogger.logFile=${MYAPP_HOME}/logs/myapp.log
org.slf4j.simpleLogger.defaultLogLevel=INFO
org.slf4j.simpleLogger.levelInBrackets=false

注意:以上操作不起作用并导致以下异常:

Could not open [${MYAPP_HOME}/logs/myapp.log]. Defaulting to System.err
Reported exception:
java.io.FileNotFoundException: ${MYAPP_HOME}/logs/myapp.log (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:209)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:99)
    at org.slf4j.impl.SimpleLogger.computeTargetStream(SimpleLogger.java:224)
    at org.slf4j.impl.SimpleLogger.init(SimpleLogger.java:205)
    at org.slf4j.impl.SimpleLogger.<init>(SimpleLogger.java:268)
    at org.slf4j.impl.SimpleLoggerFactory.getLogger(SimpleLoggerFactory.java:57)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:270)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
    ... rest of stack trace omitted for brevity

提前致谢!

1 个答案:

答案 0 :(得分:0)

simplelogger.propertiesSimpleLogger作为普通Java Properties文件读取,因此无法解析对环境变量的引用。

但是,您可以在名为org.slf4j.simpleLogger.logFile的环境变量中提供路径,该变量也由SimpleLogger读取,并优先于simplelogger.properties的配置。