Hello Program with Log4j 2.4.1

Q

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

✍: FYIcenter.com

A

Below is a simple Hello program that calls the Log4j 2 API:

// Copyright (c) 2015 FYIcenter.com
import org.apache.logging.log4j.*;
public class HelloLog4j2 {
   private static final Logger logger 
      = LogManager.getLogger("HelloLog4j2");
   public static void main(String[] args) {
	  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, HelloLog4j2.java, does the following:

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

Back to FAQ for Apache Log4j 2.4.1.

2015-11-18, 1009👍, 0💬