Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Log4j installation


May 10, 2021 Java


Table of contents


Log4j Tutorial - Log4j Installation


The Log4j API package is distributed under the Apache software license.

The latest version of log4j, including full source code, class files and documents, can be found http://logging.apache.org/log4j/ source code.

We can download the apache-log4j-x.x.x.tar.gz or zip file from the link above.

Support libraries

We can use log4j to record information to various destinations, such as sending e-mail, databases or files.

There is a list of libraries that we need to put in classpath so that log4j can pick it up and use it.

For example, when sending an e-mail message from log4j, we need an e-mail library jar file.

Libraries are optional and depend on the features we're going to use with the log4j framework.

  • JavaMail API (mail.jar): Https://glassfish.dev.java.net/javaee5/mail/ for e-mail-based logging.

  • JavaBeans Activation Framework: .jar from http://java.sun.com/products/javabeans/jaf/index.jsp.

  • Java Message Service: for JMS and JNDI.

  • XML Parser:.jar from http://xerces.apache.org/xerces-j/install.html.

Maven and Log4j

First, use the following maven command to create an empty Maven project.

C:\mvn_test>mvn archetype:generate -DgroupId=com.w3cschool.ide -DartifactId=MyTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Then, go to the project folder and find the .xml, adding the following dependencies.

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

After that, add the following code to log4j.properties created under the resources folder.

  
MyTest
 |
 +-src
    |
    +-main
       |
       +-java
       |  |
       |  +-com
       |    |
       |    +-w3cschool
       |       |
       |       +-ide
       |
       +-resources
          |
          +- log4j.properties         
            
  

As shown in the folder structure above, the resources are at the save level of the java folder.

For Java web applications, store the log4j.properties file in the WEB-INF/classes directory and save the following configuration to the log4j.properties file.

# Root logger option
log4j.rootLogger=DEBUG, stdout, file
 
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log4j.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

The last %m%n log4j to add line breaks.

%L sets the line number requested from the record.

%c {1} reference the logging name set by getLogger()

%-5p sets logging priorities, such as DEBUG or ERROR.

Finally, add the following code .java and run the application.

package com.w3cschool.ide;
 
import org.apache.log4j.Logger;
 
public class App{
 
  final static Logger logger = Logger.getLogger(App.class);
 
  public static void main(String[] args) {
 
    App obj = new App();
    obj.runMe("w3cschool");
 
  }
 
  private void runMe(String parameter){
 
    if(logger.isDebugEnabled()){
      logger.debug("This is debug : " + parameter);
    }
 
    if(logger.isInfoEnabled()){
      logger.info("This is info : " + parameter);
    }
 
    logger.warn("This is warn : " + parameter);
    logger.error("This is error : " + parameter);
    logger.fatal("This is fatal : " + parameter);
 
  }
 
}

The following code shows how to log exceptions.

import org.apache.log4j.Logger;

public class App {

  final static Logger logger = Logger.getLogger(App.class);

  public static void main(String[] args) {

    App obj = new App();

    try {
      obj.divide();
    } catch (ArithmeticException ex) {
      logger.error("Sorry, something wrong!", ex);
    }
  }

  private void divide() {
    int i = 10 / 0;

  }

}