Link to home
Start Free TrialLog in
Avatar of FranklinRaj22
FranklinRaj22

asked on

Log4j - Specifying the Log location from the code

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 ?
Avatar of for_yan
for_yan
Flag of United States of America image

Avatar of Mick Barry
you can create the appender at runtime

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

Avatar of FranklinRaj22
FranklinRaj22

ASKER

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 .
you wouldn't specify it in your config file if you needed to add it programatically
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ??
accept 35012695

Accept http:#35012748
Author explicitly stated that this posting resolved the issue  (see http:#35184735)
> 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.


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


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

that was referring to your post
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.

 
> 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
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.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.