9

I want to create and enable an appender for a particular method call MyMethod(), whose log output is supposed to go to a file present at "logFilePath".

I do not want to include this appender in the xml configuration file, so I thought to create it at run time.

First, I tried to modify the logger properties at runtime and then calling activateOptions, eg. setting level to DEBUG before and setting it to Off in the finally block, so that output is logged only while the method is in use. That didn't work.

My problem here is that appender recreates a file everytime, and does not append to the same file. This is inspite of setAppend being true.

I am not very familiar with log4j, so please feel free to suggest an alternative approach. The following is sample code to explain what I am trying.

private static FileAppender createNewAppender(String logFilePath) {
    FileAppender appender = new FileAppender();
    appender.setName("MyFileAppender");
    appender.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
    appender.setFile(logFilePath);
    appender.setAppend(true);
    appender.setThreshold(Level.INFO);
    appender.activateOptions();
    Logger.getRootLogger().addAppender(appender);
    return appender;
}

private static void removeAppender() {
    Logger.getRootLogger().removeAppender(fileAppender) ; // ("MyFileAppender");
}

I call the above methods in the following way:

private static FileAppender fileAppender = null;

private static void myMethod(String logFilePath) {        
    try {
        fileAppender = createNewAppender();
        someOperation();
    }
    finally {
        removeAppender();
        fileAppender=null; 
    }
}
4

2 回答 2

7

非常简单,只需创建一个方法并添加它

String targetLog="where ever you want your log"

FileAppender apndr = new FileAppender(new PatternLayout("%d %-5p [%c{1}] %m%n"),targetLog,true);    
logger.addAppender(apndr);
logger.setLevel((Level) Level.ALL);

然后在任何需要记录的方法中,只需执行以下操作: logger.error("your error here");

于 2012-08-02T13:43:13.327 回答
0

我从 scala 执行以下操作(基本相同):

将我的根日志记录级别设置为 TRACE,但将我的全局附加程序的阈值设置为 info。

# Root logger option
log4j.rootLogger=TRACE, file, stdout

# log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log.log
log4j.appender.file.MaxFileSize=100MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{HH:mm:ss} %m%n
log4j.appender.file.Threshold=INFO

# 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{HH:mm:ss} %m%n
log4j.appender.stdout.Threshold=INFO

然后在我要登录的课程中:

private def set_debug_level(debug: String) {
  import org.apache.log4j._
  def create_appender(level: Level) {
    val console_appender = new ConsoleAppender()
    val pattern = "%d %p [%c,%C{1}] %m%n"
    console_appender.setLayout(new PatternLayout(pattern))
    console_appender.setThreshold(level)
    console_appender.activateOptions()
    Logger.getRootLogger().addAppender(console_appender)
  }
  debug match {
    case "TRACE" => create_appender(Level.TRACE)
    case "DEBUG" => create_appender(Level.DEBUG)
    case _ => // just ignore other levels
  }
}

所以基本上,因为我将新附加程序的阈值设置为 TRACE 或 DEBUG 它实际上会附加。如果我将根更改为另一个级别,它将不会记录较低级别。

于 2014-05-23T06:59:40.050 回答