[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 760
  • Last Modified:

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
0
ronandersen
Asked:
ronandersen
  • 4
  • 3
  • 3
1 Solution
 
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
 
Jim CakalicSenior Developer/ArchitectCommented:
Partially correct. The change doesn't become effective until appender.setOptions();

Jim
0
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.

 
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
 
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
 
ronandersenAuthor Commented:
Jim!!

It works - yo da Man!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now