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 over 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 thing work :
...
<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.
From http://www.baptiste-wicht.com/2010/08/do-not-use-relative-path-with-logback
- Login or register to post comments
- 3608 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




Comments
Matthias Seidel replied on Sun, 2010/08/15 - 8:09am
Joern Huxhorn replied on Mon, 2010/08/16 - 3:19am
A relative path is evaluated against user.dir not user.home.
You should check where user.dir is pointing to in case of your application. I guess you'll find some log files there - at least if your app has the right to write into that directory.
Edit: Ok, I reread your article and you are actually using user.dir. This is very strange... relative path was working for me all the time..
Change your config to <configuration debug="true"> and evaluate the output. There might be some hints about what's going wrong.
Cheers,
Joern.
Alois Cochard replied on Mon, 2010/08/16 - 5:43am
Relative path are relative to 'something' and you need to understand what is the current directory at application startup. Your log file are there ...
Good luck
Alois Cochard
http://aloiscochard.blogspot.com
http://www.twitter.com/aloiscochard
Stephane Vaucher replied on Mon, 2010/08/16 - 4:18pm
I'm not sure I like your title "Do not use Relative Path with LogBack". It indicates that something is broken, and you present a hack to avoid the problem. From my limited knowledge of LogBack, I would agree with Alois: your log files should be relative to your cwd.
If you think it is a bug, please report it first, and then, after confirmation, post here. If that is the case, please refer to your bug report. If you did not confirm that it is a bug, this is post is useless and a waste of time (except maybe delegating your debugging).