Logging to temporary directory in a cross-platform manner
-
10-07-2019 - |
Question
I have an application for which log4j logging is configured in a log4j.properties file. Currently, this application runs on UNIX and creates a log file in /tmp. This application needs to run on Windows, and on that platform I would like for it to select the correct temporary directory, which I believe is C:\temp.
How can I change my log4j.properties file to make this happen? Do I need to switch to using an XML configuration file?
Solution
I think you would just use ${java.io.tmpdir}
in place of a hard-coded path.
OTHER TIPS
As of Log4J v1.2.14, I was able to use this in both a log4j.xml file as well as a log4j.properties file. There was some discussion on the web that variables wouldn't parse in the DOMReader, but they do as of this version of log4j.
<appender name="rolling_file_appender_ourapp" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${user.home}/.mycompany/OurApp.log" />
<param name="Append" value="false" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d | %-5p | %c | %m | %t | %x %n" />
</layout>
</appender>
or
log4j.appender.rfile=org.apache.log4j.FileAppender
log4j.appender.rfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rfile.Append=false
log4j.appender.rfile.layout.ConversionPattern=%d [%p] %c %m%n
log4j.appender.rfile.File=${user.home}/.mycompany/OurApp.log