Solved

Log4j - Specifying the Log location from the code

Posted on 2011-02-28
17
392 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
[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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

627 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