好了,所以我想一个内存附加器(一个简单的记录,它记录到一个ArrayList,而不是控制台或文件),但现在我想从打印到控制台禁用它。
这些问题和网站,我至今读(但我仍然无法弄清楚是)..
- StackOverflow的问题log4j的:登录特定类的输出到一个特定的appender
- StackOverflow的问题log4j的:禁用log4j的控制台记录,并且启用文件日志
- 编码器启动:log4j的:停止记录到控制台
它有什么,我努力实现所有的片段,但我仍然有种困惑。
我也看过此分部的logback或Log4j的加和解释哪些状态..
如果记录仪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;
}
}
但事实并非如此......
附加器http://iforce.co.nz/i/2jypxucr.ilb.png