I’m a swiss Master student in Computer Science. I’m very interested in C++, open source projects, Linux, Intel Assembly and Agile. I'm currently working on Eddi, a new programming language that I created to improve my skills in C++. I've also worked a lot on Java technologies (Sprint, Osgi, Play!, ...), but I'm not currently working with Java anymore. Baptiste is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Do not use Relative Path with LogBack

08.14.2010
| 8903 views |
  • submit to reddit

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

1
Your rating: None Average: 1 (2 votes)
Published at DZone with permission of Baptiste Wicht, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Matthias Seidel replied on Sun, 2010/08/15 - 8:09am

Well I use relative paths in logback all the time and it works perfectly fine. I can't really tell whats wrong with your config, but to say it doesn't work in logback at all, well thats just not true.

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

I'm pretty sure your log file was written, but not at the place you was looking ...

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).

King Sam replied on Fri, 2012/02/24 - 10:34am

I am using a similar relative path running in Tomcat and my logs are showing up in %CATALINA_HOME%/logs

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.