I have a java app. My development enviourment is a Windows OS and my production OS is linux. The app needs to read/write some files in the OS, among these - log files (I'm using logback lib). In my windows dev enviourment, I configured the log path to be at absolute position: C://logger/my-app.log
Following is the relevant logback.xml config file code:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>C://logger/my-app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>C://logger/my-app-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
But, as mintioned, my prod enviourment is a linux OS. I want to create the file at absolute path also in the linux machine, with a single config xml file. I don't want the file to be created in the context of the deployed jar file but in some absolute path. How can it be done?
To make the web application portable and the log file should be generated irrespective of any OS. You have to use user.home
environment variable property. I provide below the logback snippet which will work on both Windows and Linux.
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${user.home}/logs/my-app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>C://logger/my-app-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>