Solved

Log4j - Specifying the Log location from the code

Posted on 2011-02-28
17
386 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java / Linux and Regular Expressions 11 71
Programatically extract date from website 8 67
Oracle SQL syntax check  without executing 6 37
even odd program using while loop 3 29
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

867 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

22 Experts available now in Live!

Get 1:1 Help Now