log4j - dyncamically changing log file paths

How can the log file path be changed programmtcally or during runtime? I do not want to use a Env System Variable
ronandersenAsked:
Who is Participating?
 
Jim CakalicSenior Developer/ArchitectCommented:
That should be right. logger.getAppender("file") should return the FileAppender instance. You could use Logger.getRootLogger to get a the root logger instance if none other is convenient.
0
 
CEHJCommented:
((FileAppender)logger.getAppender).setFile("x.log");
0
 
ronandersenAuthor Commented:
CEHJ,
public class QueryTest3 {
      static Logger logger =  Logger.getLogger(QueryTest3.class.getName( ));
      
    public static void main(String args[]) throws Exception {
              ((FileAppender)logger.getAppender).setFile("c:\logs\hibernate.log");

              logger.info("test");
    }
}

Receive a compiler error stating getAppender can not be resolved or is not a filed. After looking up this method, it requires an Appender object??
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jim CakalicSenior Developer/ArchitectCommented:
Partially correct. The change doesn't become effective until appender.setOptions();

Jim
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Sorry. I meant appender.activateOptions();
0
 
CEHJCommented:
Sorry - typo

(FileAppender)logger.getAppender()).setFile("c:\logs\hibernate.log");
0
 
CEHJCommented:
Make your backslashes forward ones
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Try
    FileAppender appender = (FileAppender)logger.getAppender(<appender-name>);
    appender.setFile(<file-name>);
    appender.activateOptions();

where <appender-name> is the name of the appender in you logging configuration.
0
 
ronandersenAuthor Commented:
CEHI

Still does not work..

Jim,

I get the appender-name("file") for the log4j.properties file -  please see below..


log4j.rootLogger=warn, file, stdout

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file hibernate.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=J:/hibernate01/logs/hibernate.log
#log4j.appender.file.File=${MY_APP}/testLogs/hibernate.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.file.Threshold=DEBUG






0
 
ronandersenAuthor Commented:
Jim!!

It works - yo da Man!!!
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.