简体   繁体   English

在 Spring Boot 中的 application.properties 中获取用户主路径

[英]Getting the user home path in application.properties in Spring Boot

This should be a quite straight forward task, but after doing quite a bit of research I'm finding it hard to find any way to do this.这应该是一项非常直接的任务,但在做了大量研究之后,我发现很难找到任何方法来做到这一点。

I just want to create a log file in the current user's home directory.我只想在当前用户的主目录中创建一个日志文件。 According to the Official Documentation the variables I should modify are logging.file and logging.path .根据官方文档,我应该修改的变量是logging.filelogging.path But how do I get the value of the user-home into the logging.path ?但是如何将 user-home 的值放入logging.path

I have tried setting it up like:我尝试将其设置为:

logging.path=#{systemProperties['user.home']}

but without any success.但没有任何成功。

If you use Linux or Mac OS, you can use logging.path=${HOME}/logs .如果您使用 Linux 或 Mac OS,则可以使用logging.path=${HOME}/logs

${HOME} is substituted by the environment variable HOME . ${HOME}替换为环境变量HOME

${user.home} is your answer. ${user.home}就是你的答案。

For example: ${user.home}/logs/app/app.log例如: ${user.home}/logs/app/app.log

Spring boot 2.2.6弹簧靴 2.2.6

I believe I have solved the problem.我相信我已经解决了这个问题。 The log file in question was actually being generated in the class path only when run from the IDE (Eclipse Luna FYI).有问题的日志文件实际上是在从 IDE (Eclipse Luna FYI) 运行时才在类路径中生成的。 Later on when I made a jar file and ran that, the log file was being generated in the right location as specified in the application.properties file.稍后,当我制作一个 jar 文件并运行它时,日志文件会在application.properties文件中指定的正确位置生成。 I still have no clue to why it was generated in the class path when I ran it from Eclipse.当我从 Eclipse 运行它时,我仍然不知道为什么它会在类路径中生成。

I faced the same Issue in development environment so I tried another approach.我在开发环境中遇到了同样的问题,所以我尝试了另一种方法。 If you have read official document It also states you can give custom configurations.如果您已阅读官方文档它还说明您可以提供自定义配置。 And logging.path will use as a default if no custom configuration provided IMO.如果没有自定义配置提供 IMO,则 logging.path 将用作默认值。

I want to use log4j2 so I need custom pattern and other stuff.我想使用 log4j2,所以我需要自定义模式和其他东西。 For that I actually put the log4j2.xml configuration file into the class-path.为此,我实际上将 log4j2.xml 配置文件放入类路径中。 Look at my xml conf file for more details which actually worked in both dev and production.查看我的 xml conf 文件了解更多细节,这些细节在开发和生产中都有效。

<?xml version="1.0" encoding="UTF-8"?>
<configuration monitorInterval="30">
    <properties>
        <property name="app.name">my-app</property>
        <property name="pattern">%d{ISO8601} %-5p %c - %m%n</property>
    </properties>
    <appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="${pattern}"/>
        </Console>

        <RollingRandomAccessFile name="my_app" append="false" fileName="${sys:user.home}\.${app.name}\logs\${app.name}.log"
                 filePattern="${sys:user.home}\.${app.name}\logs\$${date:yyyy-MM}/${app.name}-%d{yyyy-MM-dd}-%i.log.zip">
            <PatternLayout>
                <pattern>${pattern}</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="5 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingRandomAccessFile>
    </appenders>
    <loggers>
        <root level="INFO">
            <AppenderRef ref="console"/> <!-- To console -->
            <AppenderRef ref="my_app"/>
        </root>

        <AsyncLogger name="com.rameysoft.streamline.main" additivity="FALSE" level="DEBUG">
            <AppenderRef ref="console"/>
            <AppenderRef ref="my_app"/>
        </AsyncLogger>
    </loggers>
</configuration>

logging.path=~/logs.

Simple solution is use ~ symbol for home directory, works well on linux/mac/windows.简单的解决方案是使用~符号作为主目录,在 linux/mac/windows 上运行良好。

SpringBoot 2.2.6 SpringBoot 2.2.6

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

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