0
我想轉換一個類,它是以前在log4j 1.x中使用AppenderSkeleton寫的,現在我試圖將我的服務遷移到log4j 2.6。從Log4j 1.x遷移到Log4j2
代碼:
package com.amazon.digital.music.purchasing.scheduler.test;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
/**
* A log appender which collects the log messages.
* Automatically appends itself to the root logger.
*
*/
class LogMessagesCollector extends AppenderSkeleton {
private final List<String> messages = new ArrayList<String>();
public LogMessagesCollector() {
final Logger logger = Logger.getRootLogger();
logger.addAppender(this);
}
@Override
public boolean requiresLayout() {
return false;
}
@Override
protected void append(final LoggingEvent loggingEvent) {
messages.add(loggingEvent.getMessage().toString());
}
@Override
public void close() {
final Logger logger = Logger.getRootLogger();
logger.removeAppender(this);
}
public List<String> getMessages() {
return messages;
}
我跟着鏈接: How to Create a Custom Appender in log4j2?瞭解定製的appender是如何寫在log4j2但是不能夠代碼的類將取代這一個。
任何幫助將是偉大的,提前感謝!