Getting the user home path in application.properties in Spring Boot
Asked Answered
R

5

19

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. But how do I get the value of the user-home into the logging.path?

I have tried setting it up like:

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

but without any success.

Redcoat answered 25/5, 2015 at 6:34 Comment(8)
Have you tried ${user.home} instead...Yance
@M.Deinum Still no. It just creates a file in the classpath.Redcoat
Where are you setting those variables?Yance
In the appication.properties fileRedcoat
Try as system or environment properties as setting them in the application.properties file might be too late.Yance
@M.Deinum I tried opening the application.properties file from the target/classes folder, the value appears correct there but I don't see the file being created.Redcoat
@M.Deinum That sounds like that might just be it!Redcoat
Let us continue this discussion in chat.Redcoat
R
1

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). 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. I still have no clue to why it was generated in the class path when I ran it from Eclipse.

Redcoat answered 26/5, 2015 at 10:20 Comment(0)
A
25

${user.home} is your answer.

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

Spring boot 2.2.6

Alkalinity answered 22/4, 2020 at 18:16 Comment(2)
this works for me, while ${HOME} not, i'm on win10.Sammy
@LeiYang see my comment on Chen H's answer to see why this does not work on windows.Contortionist
A
18

If you use Linux or Mac OS, you can use logging.path=${HOME}/logs.

${HOME} is substituted by the environment variable HOME.

Aubarta answered 4/5, 2016 at 3:44 Comment(2)
Strangely enough, it works on (my) windows, but HOME doesn't return the windows home... I have no idea where it takes the value from. Do you know where are those environment variables coming from?Fibrinolysis
@Fibrinolysis AFAIK $HOME is a standard linux environment variable, which does not exist on windows, hence this solution is linux specific and won't work on windows. On windows the $HOME equivalent would be the combination of %homedrive%%homepath%. Instead of relying directly on env vars, I'd look at Adam Ostrozlik's answer.Contortionist
R
1

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). 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. I still have no clue to why it was generated in the class path when I ran it from Eclipse.

Redcoat answered 26/5, 2015 at 10:20 Comment(0)
A
1

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.

I want to use log4j2 so I need custom pattern and other stuff. For that I actually put the log4j2.xml configuration file into the class-path. Look at my xml conf file for more details which actually worked in both dev and production.

<?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>
Aport answered 27/6, 2015 at 2:4 Comment(0)
T
-2

logging.path=~/logs.

Simple solution is use ~ symbol for home directory, works well on linux/mac/windows.

SpringBoot 2.2.6

Thereabouts answered 28/4, 2020 at 13:38 Comment(1)
Doesn't work for me (Linux). I get /home/user/app/~/logs.Bauxite

© 2022 - 2024 — McMap. All rights reserved.