如何使用BasicConfigurator设置文件位置

时间:2013-06-02 15:28:18

标签: java log4j

我正在尝试将我的应用配置为不使用log4j.properties,而是使用BasicConfigurator来配置我log4j.properties中的所有内容。我无法弄清楚如何设置文件的位置 我试了这个没有运气。

Logger log = Logger.getLogger(pjp.getSignature().getDeclaringTypeName());
PatternLayout layout = new PatternLayout();
FileAppender familyservices = new FileAppender();

familyservices.setLayout(layout);
familyservices.setName("com.cambiahealth.engine.rest.family");
familyservices.setFile("/usr/regence/logs/datasvc/v1.0/familyservice.log");
log.addAppender(familyservices);

BasicConfigurator.configure(familyservices);

1 个答案:

答案 0 :(得分:1)

ConsoleAppender console = new ConsoleAppender(); //create appender
  //configure the appender
  String PATTERN = "%d [%p|%c|%C{1}] %m%n";
  console.setLayout(new PatternLayout(PATTERN)); 
  console.setThreshold(Level.FATAL);
  console.activateOptions();
  //add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(console);

  FileAppender fa = new FileAppender();
  fa.setName("FileLogger");
  fa.setFile("mylog.log");
  fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
  fa.setThreshold(Level.DEBUG);
  fa.setAppend(true);
  fa.activateOptions();

  /add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(fa)
  //repeat with all other desired appenders