根据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
提前致谢!
答案 0 :(得分:0)
由simplelogger.properties
将SimpleLogger
作为普通Java Properties文件读取,因此无法解析对环境变量的引用。
但是,您可以在名为org.slf4j.simpleLogger.logFile
的环境变量中提供路径,该变量也由SimpleLogger
读取,并优先于simplelogger.properties
的配置。