[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Specify log location using Log4j

Posted on 2013-05-10
17
Medium Priority
?
485 Views
Last Modified: 2014-04-22
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.
0
Comment
Question by:ank5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
  • +1
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39155012
Try

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

Open in new window

0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 39155039
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
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 400 total points
ID: 39155142
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ank5
ID: 39156197
>>on which server you are running this appl?

It's tcserver
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39156438
How is that relevant btw?
0
 
LVL 1

Author Comment

by:ank5
ID: 39160355
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
 
LVL 1

Author Comment

by:ank5
ID: 39160357
>>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
 
LVL 16

Expert Comment

by:Valeri
ID: 39160670
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 1600 total points
ID: 39160678
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
 
LVL 1

Author Comment

by:ank5
ID: 39161530
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39161562
That's what i did try it with (hence the same package name as yours) ;)
0
 
LVL 1

Author Comment

by:ank5
ID: 39198504
There was a problem with the product, it was loading the wrong log4j.properties file, hence closing the question.

Thanks for your inputs.
0
 
LVL 1

Author Comment

by:ank5
ID: 39198505
The new package name structure is

com.lowagie.text.pdf.PdfWriter;

It seems that they have completely refactored their API.
0
 
LVL 1

Author Comment

by:ank5
ID: 39198507
-
0
 
LVL 1

Author Comment

by:ank5
ID: 39198511
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39199029
Never mind :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question