简体   繁体   English

在应用程序属性弹簧文件中指定相对路径

[英]Specify a relative path in application properties spring file

I have not found any question with the same issue, but I'm sorry if the question is a duplicate. 我没有发现与同一问题有关的任何问题,但是如果该问题是重复的,我感到抱歉。

I have this application.properties file: 我有这个application.properties文件:

## Logback
#logging.level.root=error
#logging.level.com.myapp.test=error
#logging.console=true
#logging.path=%AppData%/MyFolder/log
#logging.file=${logging.path}/logfile.log

And this is my logback.xml file: 这是我的logback.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] [%F:%L] - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${logging.file}</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] [%F:%L] - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="com.myapp.test" level="ERROR" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

    <root level="ERROR">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

The problem is I don't know how to specify the relative path %AppData% in my application.properties file. 问题是我不知道如何在我的application.properties文件中指定相对路径%AppData%。 I want to use %AppData% or similar because the application will run on differents servers and I want to use the path: C:\\Users\\$USERNAME\\AppData\\Roaming\\MyFolder\\log 我想使用%AppData%或类似的名称,因为该应用程序将在不同的服务器上运行,并且我想使用以下路径: C:\\Users\\$USERNAME\\AppData\\Roaming\\MyFolder\\log

Is that possible? 那可能吗?

Environment variables are automatically mapped into your Spring configuration. 环境变量会自动映射到您的Spring配置中。 So you should be able to use them like any other configuration variable: 因此,您应该能够像使用其他任何配置变量一样使用它们:

# Logback
logging.level.root=error
logging.level.com.myapp.test=error
logging.console=true
logging.path=${APPDATA}/MyFolder/log
logging.file=${logging.path}/logfile.log

See Spring Boot - 24. Externalized Configuration 参见Spring Boot-24。外部化配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM