log4j example

This Example shows you how to create a log in a Servlet.

log4j example


This Example shows you how to create a log in a Servlet.

Description of the code:

Logger.getLogger(): Logger class is used for handling the majority of log operations and getLogger method is used for return a logger according to the value of the parameter. If the logger already exists, then the existing instance will be returned. If the logger is not already exist, then create a new instance.

log.info(): This method is used to check that the specified category is INFO enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.



import org.apache.log4j.Logger;

class CustomerOrder {

  private String productName;
  private int productCode;
  private int productPrice;

  public int getProductCode() {
  return productCode;

  public void setProductCode(int productCode) {
  this.productCode = productCode;

  public String getProductName() {
  return productName;

  public void setProductName(String productName) {
  this.productName = productName;

  public int getProductPrice() {
  return productPrice;

  public void setProductPrice(int productPrice) {
  this.productPrice = productPrice;

  public CustomerOrder(String productName, int productCode, 
   int productPrice) {

  this.productName = productName;
  this.productCode = productCode;
  this.productPrice = productPrice;

public class Log4jExample {

  private static Logger logger = Logger.getLogger("name");

  public void processOrder(CustomerOrder order) {

  public static void main(String args[]) {
  CustomerOrder order1 = new CustomerOrder("Beer"10120);
  CustomerOrder order2 = new CustomerOrder("Lemonade"9510);
  CustomerOrder order3 = new CustomerOrder("Chocolate"2235);

  Log4jExample demo = new Log4jExample();


log4j.rootLogger=debug, stdout
log4j.appender.stdout.layout.ConversionPattern=%t %-5p %c{2} - %m%n


main INFO name - Beer
main INFO name - Lemonade
main INFO name - Chocolate

Download code


Share on Google+Share on Google+

log4j example

Posted on: September 10, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: log4j example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
December 23, 2011
automatic flushing the output to the file

Hi, im having troubles to make the log4j to flush the output to the file, i have a desktop java app and the log only flushes when i close the app, it can be automaticly? thanks
February 25, 2012
About above example

I am new to log4j. When i am executing above example getting output like log4j:WARN No appenders could be found for logger (name). log4j:WARN Please initialize the log4j system properly. When i import org.apache.log4j.BasicConfigurator and inside the main method after writing like BasicConfigurator.configure(); this then only i am getting output like 0 [main] INFO name - Beer 0 [main] INFO name - Lemonade 0 [main] INFO name - Chocolate What is happening in the program.
September 13, 2012

Hi this is sivasankar.I use to watch your site.i have one doubt here Logger.getLogger("name"); What is "name" here where it mention.I think we have to pass Class name instead of "name" .