2

I have installed a java batch process using the version of procrun that ships with tomcat 5.5.33:

   Commons Daemon Service Runner version 1.0.5.0/Win32 (Jan  5 2011) 
   Copyright (c) 2000-2011 The Apache Software Foundation.

In the installation, I specify (among other JVM options):

  --JvmOptions="-Duser.dir=C:\LOCAL\serverapps"

My log4j.properties configuration includes:

   log4j.appender.InfoLogFile.File=../logs/info.log

However, the info.log file is being written to:

   C:\WINDOWS\logs

I've checked the value of user.dir at many different points and it's always C:\LOCAL\serverapps.

But, log4j is behaving as if user.dir=C:\Windows\System32 (or some other subir of C:\Windows).

From what I can tell from the log4j source (1.2.16), the FileAppender deals only with the java.io.FileOutputStream and File classes which claim to make paths relative from the user.dir location.

I've worked around the issue, but I am curious: has anyone else has encountered this type of behaviour? If so, what's really going on?

3 Answers 3

0

FileAppender, when given a relative path, creates a file withing the current working directory - not the user home directory.

You need to pass the ${user.dir} within the filename.

SRC: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/FileAppender.java?view=markup

EDIT: see comment below for correction - user.dir != user.home

https://bugs.java.com/bugdatabase/view_bug?bug_id=4117557

3
  • user.home = User's home directory user.dir = User's current working directory Aug 26, 2011 at 18:21
  • Whoops. Sorry about that. I just found this: bugs.sun.com/view_bug.do?bug_id=4117557 It seems Oracle (...) is aware of some bugs, as it seems that user.dir is not always respected. Looks like you're better of using a non-standard system property (as you write in your comment below).
    – PhilW
    Aug 28, 2011 at 12:13
  • PhilW, your comment has the correct reason for the failure (the Sun/Oracle bug), but your original answer is a bit misleading. You should not have to pass the ${user.dir} as a prefix to the filename because FileAppender simply hands off to FileInputStream and that process should respect user.dir, but does not. If you can update your original answer to point directly to the bug, I'll remove my accepted answer and accept yours, since you correctly brought forward the Java bug - which is the explanation I was after. Mar 13, 2012 at 18:42
0

I have used ${user.dir} in the lo4j.properties and it has worked. Have you tried?

log4j.appender.InfoLogFile.File=${user.dir}/logs/info.log
1
  • I had worked around with a very similar idea. Added a new property ${logging.base} and put it in the log4j.properties file. To test your idea, I used ${user.dir} and it places the files in the correct directory (i.e. not under C:\WINDOWS). However ... that still doesn't answer the question. In fact, it adds more intrigue. What's actually going on that make relative paths NOT interpreted relative to ${user.dir}? Aug 26, 2011 at 19:09
0

PhilW's comment points to the correct answer to the original question. That is, Oracle/Sun declares an issue https://bugs.java.com/bugdatabase/view_bug?bug_id=4117557 when user.dir is set via the command line. That is the reason why the relative path is not properly understood when logging files are written out.

By using a an absolute path (even prefixing with ${user.dir} -- which can be trusted at that point - even if the JVM gets the value wrong internally) as Phil, Amir and I all suggest, you avoid the issue altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.