log4j支持把error的信息输出到日志文件,而把info的信息输出到控制台,怎么解决?
如果支持log4j分别输出,又如何写配置文件了?
------解决方法--------------------------------------------------------
MyAppender.java :
import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.OptionHandler;
public class MyAppender implements Appender, OptionHandler {
private ConsoleAppender consoleAppender = new ConsoleAppender();
private FileAppender fileAppender = new FileAppender();
public static void main(String[] args) {
}
public void doAppend(LoggingEvent arg0) {
if (arg0.getLevel().equals(Level.ERROR)) {
fileAppender.doAppend(arg0);
} else if (arg0.getLevel().equals(Level.INFO)) {
consoleAppender.doAppend(arg0);
} else {
return;
}
;
}
public void setLayout(Layout arg0) {
consoleAppender.setLayout(arg0);
fileAppender.setLayout(arg0);
}
public void addFilter(Filter arg0) {
consoleAppender.addFilter(arg0);
fileAppender.addFilter(arg0);
}
public void clearFilters() {
consoleAppender.clearFilters();
fileAppender.clearFilters();
}
public void close() {
consoleAppender.close();
fileAppender.close();
}
public ErrorHandler getErrorHandler() {
return consoleAppender.getErrorHandler();
}
public Filter getFilter() {
return consoleAppender.getFilter();
}
public Layout getLayout() {
return consoleAppender.getLayout();
}
public String getName() {
return this.getClass().getName();
}
public boolean requiresLayout() {
return consoleAppender.requiresLayout();
}
public void setErrorHandler(ErrorHandler arg0) {
consoleAppender.setErrorHandler(arg0);
fileAppender.setErrorHandler(arg0);
}
public void setName(String arg0) {
consoleAppender.setName(arg0);
fileAppender.setName(arg0);
}
public void setFile(String name) {
fileAppender.setFile(name);
}
public String getFile() {
return fileAppender.getName();
}
public void activateOptions() {
fileAppender.activateOptions();
consoleAppender.activateOptions();
}