tags:

views:

775

answers:

2

Hi I have a log4j RollingFileAppender configured like this

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="MaxBackupIndex" value="10" />
    <param name="File" value="${java.io.tmpdir}\\myLogFile.log" />
</appender>

my problem is that java.io.tmpdir is mapped to C:\Temp and log4j does not escape the backslash. When I start the process I see the following error

java.io.FileNotFoundException: c:   emp\myLogFile.log (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(Unk

it is not possible for me to modify the Environment Variables on all my users' machines and I do not want to hard-code the log path.

I could write my own extension to RollingFileAppender to fix this, but I was hoping that there would be a pre-existing way around this.

What are my best options ?

+1  A: 

It looks like this might not happen if you use a properties file instead of an XML file. Looking at the log4j source code, it seams that DOMConfigurator is making the TAB substitution that is causing the error. PropertyConfigurator doesn't perform this substitution. Give it a shot maybe?

You could also change java.io.tmpdir to C:/Temp and use / instead of \ where appropriate. That doesn't sound like an option for you though?

laz
I'm reluctant to complicate my configuration file setup any further. It sounds like a customized extension to RollingFileAppender is the best way to go.
Ben Hammond
+1  A: 

Have you tried this?

<param name="File" value="${java.io.tmpdir}/myLogFile.log" />

You shouldn't need to escape backslashes within the XML file, and log4j should be perfectly capable of transforming that path into whatever platform-specific path (backslashes on Windows) you need.

matt b
Java handles the / to \ conversion perfectly fine, so I find it's better to always use / so you don't have to worry about escaping it.
Herms
Oh, wait, that wouldn't help. The problem is the "C:\" in the env variable definition, not the / before the file name.
Herms
I don't think that should matter though. log4j escapes things like ${catalina.home}/logs/app.log fine for me on a Windows machine, in which catalina.home starts with "C:\"
matt b
But that's what's happening in his example. The \T in C:\Temp is being converted into a tab, and that's what's in the env variable.
Herms
changing the slashes doesn't make any difference.It works fine in the development environment, its the deployed jars that have the problem.
Ben Hammond