Console Appender in Log4j

In this log4j console appender tutorial you will be introduced with ConsoleAppender which is used in Log4j for appending output on the console each and every time when any info(), debug(), error() method is invoked by the logger.

Console Appender in Log4j

Console Appender in Log4j

     

In this log4j console appender tutorial  you will be introduced with ConsoleAppender which is used in Log4j for appending output on the console each and every time when any info(), debug(), error() method is invoked by the logger.

In our example we have made a class ConsoleAppenderExample. After this we have created a logger object by invoking static method getLogger(). After this we have created an object of ConsoleAppender and to add this logger message we need to add appender to logger. Following code does it for us. 

"logger.addAppender(conappender)" where conappender is ConsoleAppender's object.

Here is the example code for ConsoleAppender example :

 

ConsoleAppenderExample.java

import org.apache.log4j.*;
public class ConsoleAppenderExample {
static Logger logger = Logger.getLogger(
 
"ConsoleAppenderExample.class");
  public static void main(String[] args) {
  ConsoleAppender conappender = 
 
new ConsoleAppender(new PatternLayout());
  logger.debug("Content part 1");
  logger.debug("Content part 2");
  logger.debug("Content part 3");
  logger.addAppender(conappender);
  }
}

Output:

After compilation and execution you will get following output on your console.

16:22:51,406 DEBUG class:6 - Content part 1

16:22:51,421 DEBUG class:7 - Content part 2

16:22:51,421 DEBUG class:8 - Content part 3

Download Source Code