问题
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.
回答1:
If you use Linux or Mac OS, you can use logging.path=${HOME}/logs
.
${HOME}
is substituted by the environment variable HOME
.
回答2:
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.
回答3:
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>
来源:https://stackoverflow.com/questions/30432473/getting-the-user-home-path-in-application-properties-in-spring-boot