Run Example in


How to run the example program in I have installed at \fyicenter\logging-log4j-1.2.14 folder.


A is an example program provided in the It is used to show you how to use Log4j with the default configuration. Here is the source code of in the \fyicenter\logging-log4j-1.2.14\examples folder:

 * Copyright 1999-2005 The Apache Software Foundation.

package examples;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.NDC;

// ...
public class Trivial {

  static Logger logger = Logger.getLogger(Trivial.class);

  public static void main(String[] args) {
    NDC.push("Client #45890");"Awake awake. Put on thy strength.");;;"Exiting Trivial.");    

  void foo() {
    logger.debug("Now king David was old.");    

  static class InnerTrivial {
    static  Logger logger = Logger.getLogger(InnerTrivial.class);

    void foo() {"Entered foo."); 

You can follow these steps to run the example program:

\fyicenter>cd \local\logging-log4j-1.2.14

\lccal\logging-log4j-1.2.14>java -version
java version "1.7.0_45"

\local\logging-log4j-1.2.14>javac -cp .;dist\lib\log4j-1.2.14.jar 

\local\logging-log4j-1.2.14>java -cp .;dist\lib\log4j-1.2.14.jar 

0 [main] INFO examples.Trivial Client #45890 - Awake awake. 
   Put on thy strength.
0 [main] DEBUG examples.Trivial Client #45890 DB - Now king David was old.
1 [main] INFO examples.Trivial$InnerTrivial Client #45890 - Entered foo.
1 [main] INFO examples.Trivial Client #45890 - Exiting Trivial.

The source code and the output shows that

  • Logger.getLogger(Trivial.class) is used to create a logger object with the class name.
  • BasicConfigurator.configure() is used to set the logger to use the default configuration.
  • NDC.push() and NDC.pop() are used to manage the context identification.
  •"Exiting Trivial.") is used to log a message at the "INFO" level.
  • "0 [main] INFO ..." in the log output gives the execution time, thread name, log level and other log information.


Download and Install

What Is log4j-1.2.14.jar

Downloading Apache Log4j 1.x JAR Packages

⇑⇑ FAQ for Apache Log4j

2015-12-02, 1896🔥, 0💬