Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Tutorials Log4j Console Appender in Log4j

Related Tutorials


 
 

Share on Google+Share on Google+

Console Appender in Log4j

Advertisement
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

     

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

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 11, 2008

Related Tutorials

Discuss: Console Appender in Log4j   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com