Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Log4j - Specifying the Log location from the code

Posted on 2011-02-28
17
Medium Priority
?
393 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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