好的,所以我正在尝试制作一个内存附加程序(只是一个记录器,它记录到 ArrayList 而不是控制台或文件)但现在我想禁用它打印到控制台。
到目前为止,我读过的问题和网站(但我仍然无法弄清楚)..
- StackOverFlow 问题 log4j:将特定类的输出记录到特定附加程序
- StackOverFlow 问题 log4j:禁用 log4j 控制台日志记录并启用文件日志记录
- 编码器启动:log4j:停止记录到控制台
它包含了我想要实现的所有部分,但我仍然有点困惑。
我还从Logback 或 Log4j Additivity Explained which states 中读到了这个片段。
但是,如果 logger X 的 aditivity 标志设置为 false 或禁用,则调用 x.debug() 将仅记录到文件。
所以理论上我的log4j.properties
文件
log4j.rootLogger=ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %5p %c (%F:%L) - %m%n
#hide the Log4jMemoryAppender from console
log4j.logger.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender= ERROR, MEMORY_APPENDER
log4j.appender.MEMORY_APPENDER=nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender.Log4jMemoryAppender
log4j.additivity.rootLogger = false
log4j.additivity.console = false
log4j.additivity.MEMORY_APPENDER=false
应该只打印***Hello World
和排除MEMORY_APPENDER
,rootLogger
和console
.
package nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender;
import java.util.ArrayList;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log4jMemoryAppender extends AppenderSkeleton {
ArrayList<LoggingEvent> eventsList = new ArrayList();
public static void main (String [] args) {
PropertyConfigurator.configure("Lib/log4j.properties");
Log4jMemoryAppender app = new Log4jMemoryAppender();
Logger logger = Logger.getLogger(Log4jMemoryAppender.class);
logger.setLevel(Level.INFO);
logger.addAppender(app);
logger.info("Hello World");
logger.debug("Level DEBUG Is SET");
for (LoggingEvent le: app.eventsList) {
System.out.println("***" + le.getMessage());
}
}
@Override
protected void append(LoggingEvent event) {
eventsList.add(event);
}
public void close() {
}
public boolean requiresLayout() {
return false;
}
}
但它不...
(来源:iforce.co.nz)