61

I am trying to implement log4j 2 but it keeps throwing the following error.

> ERROR StatusLogger Log4j2 could not find a logging implementation.
> Please add log4j-core to the classpath. Using SimpleLogger to log to
> the console...  
> ERROR LogExample This Will Be Printed On Error 
> FATAL LogExample This Will Be Printed On Fatal

I have tried the solution given on the net. But the don't seem to be working for me.

This is the code that I am trying to run.

package demo;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogExample {

    private static final Logger LOG = LogManager.getLogger(LogExample.class);

    public static void main(String[] args) {

        LOG.debug("This Will Be Printed On Debug");
        LOG.info("This Will Be Printed On Info");
        LOG.warn("This Will Be Printed On Warn");
        LOG.error("This Will Be Printed On Error");
        LOG.fatal("This Will Be Printed On Fatal");
        LOG.info("Appending string: {}.", "Hello, World");
    }

}

Project and dependency added in the pom.xml:

enter image description here

enter image description here

Any help is appreciated.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Alok
  • 1,441
  • 4
  • 20
  • 31
  • 2
    You need to add the log4j-core jar to the classpath. I suppose you are using maven so basically you can do that by creating a jar with all the dependencies (instead of a jar containing only your code). You can take a look at the maven shade plugin to do so. – Cristian Ramon-Cortes Dec 19 '17 at 07:29
  • 1
    Please consider accepting one of the answers if it was useful or post your own if you managed to solve it in another way. – Cristian Ramon-Cortes Feb 01 '18 at 12:50
  • Thanks for the reminder. I found another ways I have posted it as another answer. Really appreciate your help. – Alok Feb 01 '18 at 15:27

10 Answers10

25

This dependency help to avoid this error from lambda.

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.8.2</version>
</dependency>
Sandun Susantha
  • 1,010
  • 1
  • 10
  • 11
  • I tried to do the same to my build.sbt file, but unfortunately still not working: libraryDependencies += "org.apache.logging.log4j" % "log4j-to-slf4j" % "2.9.1" – Mee Sep 21 '22 at 16:07
  • This is not working for me. can you elaborate more on the solution. – Alok Nov 24 '22 at 22:27
11

Solved the error message by setting the system property for log4j2 configuration file. below is the below code.

System.setProperty("log4j.configurationFile","./path_to_the_log4j2_config_file/log4j2.xml");

Logger log = LogManager.getLogger(LogExample.class.getName());
Alok
  • 1,441
  • 4
  • 20
  • 31
  • @cody.tv.weber can you provide more details or the error that you are getting. Just re-check if you have followed all the steps. – Alok Mar 08 '19 at 17:56
  • @Alok is log4j.configurationFile deprecated ? – Gaurav Jun 06 '19 at 13:33
  • 2
    @gaurav what do you mean by deprecated? log4j2 is upgraded version of log4j. And log4j2 unlike its previous version does not support .properties file as configuration file. – Alok Jun 06 '19 at 15:31
  • 2
    @Alok I did not say log4j2 is deprecated. I said that the property "log4j.configurationFile" is deprecated. It is recommended to use "log4j2.configurationFile" – Gaurav Jun 07 '19 at 05:45
  • 1
    This comment is old, but log4j2 does support property files now. (log4j2.properties) https://logging.apache.org/log4j/2.x/manual/configuration.html – Omeri Aug 20 '21 at 22:01
7

if you already added log4j-core to maven pom, you dont need anything more it should work. Problem could be with your IDE, that does not see your maven dependency. Try reloading or reimporting maven dependencies, or restarting your IDE also can help.

engKocer
  • 237
  • 3
  • 6
4

In intellij on mac the log4j dependency was not added to the classpath for some reason. So What I did was

After adding the following to the pom.xml file

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.20.0</version>
        </dependency>

I manually downloaded the log4j zip file from https://www.apache.org/dyn/closer.lua/logging/log4j/2.20.0/apache-log4j-2.20.0-bin.zip

and after unzipping the file I added log4j-core and log4j-api to the project path like below

enter image description here

Dr. Mian
  • 3,334
  • 10
  • 45
  • 69
3

To get around a similar issue with Spring Boot I added

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>   

to my POM

rupweb
  • 3,052
  • 1
  • 30
  • 57
1

In my case the error was related with the version of maven-shade-plugin (3.2.2).

It works when I set this plugin to version 3.2.1

dvillaj
  • 780
  • 1
  • 9
  • 8
1

I haved this problem in NetBeans 8.2 when I added the jar log4j-api-2.12.2.jar.

Netbeans print this message in the console:

StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

I resolved this problem when I added log4j-core-2.12.2.0001L.jar in my project.

I supposed in Spring, you just most add the dependency log4j-core in maven.

Link download JAR

https://jar-download.com/?search_box=log4j-core

Andronicus
  • 25,419
  • 17
  • 47
  • 88
0

I was facing the same issue for upgrading log4j from 1.x to Log4j-2.17.1. Was getting this error while deploying to GlassFish server.

I took the following steps to correct this:

  1. Disbled the autoInitialization of Log4j2. Added to web.xml

    <context-param> <param-name>isLog4jAutoInitializationDisabled</param-name> <param-value>true</param-value> </context-param>

  2. Initialized Log4j2 programmatically:

InputStream inputStream = new FileInputStream("path of Log4j2.xml"); ConfigurationSource source = new ConfigurationSource(inputStream); Configurator.initialize(null, source);

Debasis
  • 1
  • 1
0

For my case, if you added the dependencies in pom.xml file then just go to the Project Settings - Modules in your IDE and there change the Scope from Test to Compile.No need to download the zip files.

enter image description here

ZAJ
  • 793
  • 3
  • 23
  • 50
-2

Issue resolved by simply adding log4j-core-2.12.2.0001L.jar to the project.
Thanks.

Community
  • 1
  • 1