Where to actually read java's Log4j properties

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
Sarge516Asked:
Who is Participating?
 
Amitkumar PConnect With a Mentor Sr. ConsultantCommented:
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
 
Sharon SethCommented:
You should be able to get that from your logger object
0
 
Sarge516Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.