17

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.

shyam
  • 1,348
  • 4
  • 19
  • 37

5 Answers5

22

${user.home} is your answer.

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

Spring boot 2.2.6

Adam Ostrožlík
  • 1,256
  • 1
  • 10
  • 16
17

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

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

Dario Seidl
  • 4,140
  • 1
  • 39
  • 55
Chen H
  • 171
  • 1
  • 3
  • 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? – Juh_ Aug 26 '19 at 14:07
  • 2
    @Juh_ 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. – ThomasMX Jan 23 '23 at 07:59
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.

shyam
  • 1,348
  • 4
  • 19
  • 37
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>
Mubasher
  • 943
  • 1
  • 13
  • 36
-2

logging.path=~/logs.

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

SpringBoot 2.2.6

Shannon
  • 54
  • 1
  • 11