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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
((FileAppender)logger.getAppender).setFile("x.log");
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??
Jim CakalicSenior Developer/ArchitectCommented:
Partially correct. The change doesn't become effective until appender.setOptions();

Jim
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

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

(FileAppender)logger.getAppender()).setFile("c:\logs\hibernate.log");
CEHJCommented:
Make your backslashes forward ones
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.
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






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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ronandersenAuthor Commented:
Jim!!

It works - yo da Man!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.