Hello Program with Apache Log4j 1.x

Q

How to write a simple Hello program with Apache Log4j 1.x? I need it to test out which JARs are needed to compile and run my Java applications using Log4j 1.x.

✍: FYIcenter.com

A

Below is a simple Hello program that calls the Apache Log4j 1.x API:

// Copyright (c) FYIcenter.com
import org.apache.log4j.*;
public class HelloLog4j {
  private static final Logger logger = Logger.getLogger("fyiLogger");
  public static void main(String[] args) {
    System.out.println("Logger class: "+logger.getClass().getName());
    BasicConfigurator.configure();
    logger.fatal("Hello - fatal");
    logger.error("Hello - error");
    logger.warn("Hello - warn");
    logger.info("Hello - info");
    logger.debug("Hello - debug");
    logger.trace("Hello - trace");
  }
}

This sample program, HelloLog4j.java, does the following:

  • It uses only the Log2j API only.
  • It creates logger object named as "fyiLogger".
  • It uses the default "Basic" configurator.
  • It logs a simple message at each of 6 logging levels: fatal, error, warn, info, debug, and trace.

 

Compile and Run Program with Apache Log4j 1.x

Prerequisites for Apache Log4j 1.x

Using Apache Log4j 1.x in Java Programs

⇑⇑ FAQ for Apache Log4j

2015-12-11, 1806🔥, 0💬