Specify log location using Log4j

I have a war file which uses log4j for logging.
Log4j.properties has the following for log file creation -

log4j.appender.fileAppender.File=C:/temp/MyApplication.log

The problem is that this war file is going to be deployed in Windows and Linux, so I don't want to hard code it to c:/temp.

Is there a way to provide this path in a generic way, say using some environment variables, java home, user directory etc.
LVL 1
ank5Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Try

log4j.appender.fileAppender.File=${user.home}/MyApplication.log

Open in new window

0
Sharon SethCommented:
Such config details should not be part of war file . Instead it should be in an external properties file , which would be read by one of your classes packaged into the war
0
ValeriCommented:
just use this in your Log4j.properties file:

it should be something like that in your properties file

# Define the root logger with appender file
log = /usr/home/log4j

# Define the file appender
log4j.appender.fileAppender.File=${log}/yourLogFile.out
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

ank5Author Commented:
>>on which server you are running this appl?

It's tcserver
0
CEHJCommented:
How is that relevant btw?
0
ank5Author Commented:
log4j.appender.fileAppender.File=${user.home}/MyApplication.log

I tried this but it does not seem to work. Attached is how the log file looks like, is there something wrong in it?


Thanks
log4j.txt
0
ank5Author Commented:
>>Such config details should not be part of war file . Instead it should be in an external properties file , which would be read by one of your classes packaged into the war

This isn't really an option for me. This log4j.properties is provided as a part of the product (Documentum xCP in this case), I can only modify it add logging for my custom classes.
0
ValeriCommented:
if you want a different path for windows and linux then you have to have something like configuration file that is different for windows and linux.
after reading this file at the beginning /before to initialize Log4J/ you can set the path properly in this way:

System.setProperty("logPath", "the path specified in the config file");

then in your Log4J.properties file you have to have this:
log4j.appender.fileAppender.File=${logPath}/yourLogFile.out
0
CEHJCommented:
I tried this but it does not seem to work.
Works fine for me:
package com.m1.m2;

import org.apache.log4j.Logger;

public class LoggerTest {
    public static void main(String[] args) {
	Logger  logger = Logger.getLogger(LoggerTest.class);
	logger.debug("debugging...");
    }
}

Open in new window

goose@vaio:/tmp$ java com.m1.m2.LoggerTest
2013-05-13T09:38:44.421+0100 DEBUG [Tmain] [] [com.m1.m2.LoggerTest:11] debugging...
goose@vaio:/tmp$ cat ~/MyApplication.log
2013-05-13T09:38:44.421+0100 DEBUG [Tmain] [] [com.m1.m2.LoggerTest:11] debugging...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ank5Author Commented:
Hi CEHJ

Can you please try it with the log file that I had posted. I think that my log file may be incorrect.

Thank you
0
CEHJCommented:
That's what i did try it with (hence the same package name as yours) ;)
0
ank5Author Commented:
There was a problem with the product, it was loading the wrong log4j.properties file, hence closing the question.

Thanks for your inputs.
0
ank5Author Commented:
The new package name structure is

com.lowagie.text.pdf.PdfWriter;

It seems that they have completely refactored their API.
0
ank5Author Commented:
-
0
ank5Author Commented:
Please ignore the last two comments, they were posted in this question by mistake. I can't find an option to delete these comments.
0
CEHJCommented:
Never mind :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.