62

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.

3
  • 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. Dec 19, 2017 at 7: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. Feb 1, 2018 at 12:50
  • Thanks for the reminder. I found another ways I have posted it as another answer. Really appreciate your help.
    – Alok
    Feb 1, 2018 at 15:27

10 Answers 10

29

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>
2
  • 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, 2022 at 16:07
  • 1
    This is not working for me. can you elaborate more on the solution.
    – Alok
    Nov 24, 2022 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());
5
  • @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 8, 2019 at 17:56
  • @Alok is log4j.configurationFile deprecated ?
    – Gaurav
    Jun 6, 2019 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 6, 2019 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 7, 2019 at 5:45
  • 1
    This comment is old, but log4j2 does support property files now. (log4j2.properties) logging.apache.org/log4j/2.x/manual/configuration.html
    – Omeri
    Aug 20, 2021 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.

5

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

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

2
  • 5
    What does Spring have to do with this?!?
    – Zordid
    Nov 30, 2021 at 11:03
  • I had a related error using log4j2 in a spring project
    – rupweb
    Dec 3, 2021 at 12:55
2

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

1
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

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);

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

-2

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.