Solved

Log4j - Specifying the Log location from the code

Posted on 2011-02-28
17
381 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

13 Experts available now in Live!

Get 1:1 Help Now