Getting the user home path in application.properties in Spring Boot
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>
If you use Linux or Mac OS, you can use logging.path=${HOME}/logs
.
${HOME}
is substituted by the environment variable HOME
.
${user.home}
is your answer.
For example: ${user.home}/logs/app/app.log
Spring boot 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). 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.