Okay so I'm trying to make a memory appender (simply a logger, that logs to an ArrayList instead of the console or to a file) but for now I want to disable it from printing to the console.
The questions and websites, I've read so far (but I still cant figure it out is)..
- StackOverFlow Question log4j: Log output of a specific class to a specific appender
- StackOverFlow Question log4j : Disable log4j console logging and enable file logging
- Coder Launch: log4j: stop logging to console
It has all the segments on what I'm trying to achieve, but I'm still kind of confused.
I also read this segment from Logback or Log4j Additivity Explained Which states..
If the aditivity flag of logger X however is set to false, or disabled, then calling x.debug() will only log to the file.
So in theory my log4j.properties
file
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
Should only print ***Hello World
and exclude anything else from the MEMORY_APPENDER
, rootLogger
and 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;
}
}
But it doesn't...
(source: iforce.co.nz)