log4j Getting started with log4j

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

This section provides an overview of what log4j is, and why a developer might want to use it.

It should also mention any large subjects within log4j, and link out to the related topics. Since the Documentation for log4j is new, you may need to create initial versions of those related topics.

Log4j Lifecycle

Log4j 1.x is end-of-life as of August 5, 2015. [1][2]. Apache Log4j 2 is the successor to Log4j 1.x.

1 https://logging.apache.org/log4j/1.2/
[2] https://blogs.apache.org/foundation/entry/apache_logging_services_project_announces

Versions

VersionNoticeRelease Date
2.8latest version2017-01-21
2.72016-10-02
2.6.22016-07-09
2.4Log4j 2.4 and greater requires Java 72015-09-20
2.3.6last version that support java 62015-05-15
2.0first stable version of branch 2.x. Breaks api compatibility. Use bridge: log4j-1.2-api.jar2014-07-01
1.2.17EOF log4j branch 1.x2015-08-05

Installation and Setup

Installation

Installation of Log4j2 is as simple as putting log4j2 jar in application classpath. Though you might want to customize logs output through additional config file

Configuration

maven

To add log4j to project in maven, add it's dependency: In pom.xml add following dependency:

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>${log4j2.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j2.version}</version>
  </dependency>
</dependencies>
 

springboot with maven

Spring-boot is commonly used framework for web application. It features support auto-configuration for many features including logging façade like log4j2. To add log4j2 to your spring-boot project make sure you exclude default logging façade: commons-logging. Log4j will be used, when it is only logging façade on classpath.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <!-- exclude spring-boot java commons logging in favour of log4j2 -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- add log4j2 to spring-boot: -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
 

Notice, that there is no version in above snippet. It is because project inherit version from parent. Make sure you also inherit from spring-boot-starter-parent, by adding:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>
 

ivy

In ivy.xml, add following dependency:

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-api" rev="${log4j2.version}" />
  <dependency org="org.apache.logging.log4j" name="log4j-core" rev="${log4j2.version}" />
</dependencies>
 

gradle

In your .gradle file:

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
  compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
}
 


Got any log4j Question?