Search code examples
logbacklogback-classicspring-logback

Logback - Different log file path base on the application profile


I am trying to use logback.xml for my project and for development i am using windows env and for deployment i am using unix env so i have created below xml file.

However when my application starts look like it's trying to validate the path specified. and it end up with below error.

how do i achieve this using logback?

java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[UNIX_FILE] - Failed to create parent directories for [C:\as\Users\satishkn\apps\workspaceslogs\carats-api-2019-02-18.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[UNIX_FILE] - openFile(null,true) call failed. java.io.FileNotFoundException: \as\Users\satishkn\apps\workspaceslogs\carats-api-2019-02-18.log (The system cannot find the path specified)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:169)

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="windows_log_dir" value="./logs/" />
    <property name="unix_log_dir" value="/as/Users/satishkn/apps/workspaceslogs/" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="WIN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${windows_log_dir}carats-api-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
            </Pattern>
        </encoder>
    </appender>

    <appender name="UNIX_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${unix_log_dir}carats-api-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
            </Pattern>
        </encoder>
    </appender>

    <springProfile name="default">
        <root level="info">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="WIN_FILE" />
        </root>
    </springProfile>

    <!-- <springProfile name="dev">
        <root level="info">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="UNIX_FILE" />
        </root>
    </springProfile>

    <springProfile name="prod">
        <root level="info">
            <appender-ref ref="UNIX_FILE" />
        </root>
    </springProfile> -->

</configuration>

Solution

  • Using your configuration both appenders will be instanciated in both unix and windows environnement. You should use conditional configuration to achive your requirements.

    I suggest you define a log_dir param this way :

    <if condition='property("os.name").contains("win")'>
            <then>
                <property name="log_dir" value="./logs/" />
            </then>
            <else>
                <property name="log_dir" value="/as/Users/satishkn/apps/workspaceslogs/" />
            </else>
     </if>
    

    Then create a single appender that uses the log_dir property.