Solved

Log4j - Specifying the Log location from the code

Posted on 2011-02-28
17
387 Views
Last Modified: 2012-05-11
Can I dynamically decide the path of a log file and apply it throgh code rather than predefining it in the log4j property file . Or will be able to overite the logfile path that is in the property file from teh code ?
0
Comment
Question by:FranklinRaj22
  • 6
  • 6
  • 2
  • +1
17 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34999219
0
 
LVL 92

Expert Comment

by:objects
ID: 35001262
you can create the appender at runtime

SimpleLayout layout = new SimpleLayout();  
FileAppender appender = new FileAppender(layout,"your filename",false);  
logger.addAppender(appender);

0
 

Author Comment

by:FranklinRaj22
ID: 35012604
I tried this but this log file has only log info from the class in which this appender is created. If this class calls a method from another class , the logs from that method r still written to the logFile mentioned in the log4j.properties. My requirment would be once I set up this new log file in runtime all teh subsequent logs should be written to this . Not sure how to acheive this .
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 92

Expert Comment

by:objects
ID: 35012695
you wouldn't specify it in your config file if you needed to add it programatically
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35012748

Perhpas this could help as alternative:

http://osdir.com/ml/jakarta.log4j.user/2002-04/msg00324.html


One way that i use to specify the filename dynamically is setting this in
the properties file:

# Appender definition
log4j.appender.appender_name = org.apache.log4j.FileAppender
log4j.appender.appender_name.File = ${file.name}
log4j.appender.appender_name.ImmediateFlush = true
log4j.appender.appender_name.layout = org.apache.log4j.PatternLayout
log4j.appender.appender_name.layout.ConversionPattern = %d{HH:mm:ss,SSS}
[%c] [%p] (%F:%L) - %m%n

Then in the code, before executing the PropertyConfigurator.configure write
this line:

System.setProperty("file.name", "C:\\apps\\logs\\file1.log");

Hope this helps
0
 

Author Comment

by:FranklinRaj22
ID: 35184735
Yan,

I tried this and it wrked fine , thank you .
One more thing i need to do is , i would like to write to 2 log files. the same content . In that case how can I alter my code. Let me tell you what I am tryin hence It will be bit easier for you .

I hav rocess A ,B,C ... I hav to log each of these process A in LOGA and B in LOGB an dC in LOGC ... and all together in a generic log .
So before the start of Process A i dynamically configure the LOGA and before process B i configure LOGB but I also want a log wic will log all the three process log in teh same program . How can I implement that ??
0
 
LVL 92

Expert Comment

by:objects
ID: 35714293
accept 35012695
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35714457

Accept http:#35012748
Author explicitly stated that this posting resolved the issue  (see http:#35184735)
0
 
LVL 92

Expert Comment

by:objects
ID: 35714503
> Accept http:#35012748

that repeats what I posted in previous post ie. you'll need to set the filename programatically.
You can't claim posts for repeating someone elses suggestion.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35714577


The author explicitly stated that posting http:#35012748 resolved the issue  (see http:#35184735) . Moreover,  it was also
explicitly stated that other postings did not  help him
(see http:#35012604).
  http:#35012748 should be accepted


0
 
LVL 92

Expert Comment

by:objects
ID: 35714590
> explicitly stated that other postings did not  help him
(see http:#35012604).

that was referring to your post
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35714608
http:#35012604 states that previous posts did not help.

post http:#35012695 represnts nothing more than restatement of the question, replacing the words "apply it through the code" with words "add it programmatically".
And we are lucky that the author explicitly
stated whose post help him in http:#35184735
Nothing can be more clear than the situation with this question.

 
0
 
LVL 92

Expert Comment

by:objects
ID: 35714618
> http:#35012604 states that previous posts did not help.

its talking about log4j config file, its got nothing to do with my post

> stated whose post help him in http:#35184735

which as I already stated repeated what I had already suggested.
you can't just use other peoples suggestions at ee sorry
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35714632
Repetition does not make any false statement truthful.
It is sufficient to look at http:#35012748 to see that it does not repeat
anything in the trail above,
whereas posting http:#35012695 does nothing more
than re-states the question.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35872636
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
t-sql sp_addlinkedserver possible risks 3 66
even odd program using while loop 3 40
Which non-HTML GUI front end to use with Java? 3 19
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now