Search code examples
javaxmlmavenpathlogback

Logback does not write relative to jar file in which it has been compiled for MacOs


The problem is that logback does not behave the same in windows than it does in MacOs. The following setup works correctly on windows and creates the log files relative to where the application is running.

On MacOs however it seems always write the log files relative to the home folder resulting in a path like user/myUsername/log/logExport.txt. This should however be user/myUserName/path/to/app/log/logExport.txt.

This is an issue as this application will be running stand alone on many different computers including MacOs. The application is always started using script files like bash or similar.

the logback.xml is included in the jar file and therefor can't be changed after building the application.(hence why its a relative path) For the ease of the customer I want to avoid having to make them setup a properties file or similar. Is there any way for me to point logback to the correct location on mac? This is a Maven Java project.

this works 100% as intended on windows machines.

<appender name="ROLLING"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>log/logExport.txt</file>
    <rollingPolicy
        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>log/archive/logExport.%d{yyyy-MM-dd}.txt</fileNamePattern>
        <maxHistory>50</maxHistory>
        <totalSizeCap>1GB</totalSizeCap>

    </rollingPolicy>

    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>

Solution

  • This should however be user/myUserName/path/to/app/log/logExport.txt.

    In that case the app needs to be started from user/myUserName/path/to/app. That's a relative path in your config and will be relative to the current directory. Your best bet would be to specify that directory as the starting directory in a shortcut or cd to it in a launching script.