?
Solved

Where to actually read java's Log4j properties

Posted on 2012-09-06
3
Medium Priority
?
370 Views
Last Modified: 2012-09-07
I have an java app that needs to email out a log file.  I can hard-code the file name ("logs/Info.log") and it works, but would rather use the file name referenced in log4j.appender.DAILY.file from the log4j property file.

My question is, how can I find the reference to "log4j.appender.DAILY.file" ?  I don't want to re-read the log4j.properties. This is already loaded somewhere. How can I obtain its value ?

Sample Log4j below ...

# Log4j configuration file.
# http://stackoverflow.com/questions/1140358/how-to-initialize-log4j-properly
# ALL, NONE, DEBUG, INFO, WARN, ERROR

log4j.rootLogger=DEBUG,DAILY
log4j.logger.Error=DEBUG,ERROR
log4j.logger.Event=DEBUG,EVENT
log4j.logger.Resources=DEBUG,RESOURCES

log4j.appender.DAILY=org.apache.log4j.RollingFileAppender
log4j.appender.DAILY.file=logs/Info.log
0
Comment
Question by:Sarge516
3 Comments
 
LVL 21

Accepted Solution

by:
Amitkumar P earned 2000 total points
ID: 38374974
Check with below code...

Enumeration allAppenders = Logger.getRootLogger().getAllAppenders();
while (allAppenders.hasMoreElements()) {
    Object object = (Object) allAppenders.nextElement();
    if (object instanceof FileAppender) {
        FileAppender appender = (FileAppender) object;
        System.out.println(appender.getFile());
    }
}


Refer http://logging.apache.org/log4j/1.2/apidocs/index.html
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38375328
You should be able to get that from your logger object
0
 

Author Closing Comment

by:Sarge516
ID: 38376699
Thanks! That works for me and can understand it.  I only had to make a minor mod for this to work in my setup.
Enumeration allAppenders = LogManager.getRootLogger().getAllAppenders();
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

839 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