Do not use relative path with LogBack

A little tip that can be useful and save a lot of time : Do not use relative path with LogBack.

I wondered why this little LogBack configuration didn't work :

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>
    <contextName>JTheque</contextName>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/jtheque.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>logs/jtheque.%i.log.zip</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>5</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>5MB</MaxFileSize>
        </triggeringPolicy>

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

    <root level="DEBUG">
        <appender-ref ref="FILE"/>
    </root>
</configuration>

No file were written. I searched during a long time and after that tested with an absolute path and it worked really well. But absolute path is not very good. But, you can use system properties in the configuration, so I used user.dir to make the think works :

        ...
        <file>${user.dir}/logs/jtheque.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>${user.dir}/logs/jtheque.%i.log.zip</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>5</MaxIndex>
        </rollingPolicy>
        ...

And this time, it works well !

Hope this will be useful to somebody.

Related articles

  • NIO.2 : The new Path API in Java 7
  • Java File Copy Benchmark Updates (once again)
  • Tip Replace an old copyright by a new one
  • Logging with SLF4J
  • Quick Tip : Launch Java Applications From Java applications with Ant
  • Tip : Don’t use shorts for loop indexes !
  • Comments

    Comments powered by Disqus