ERROR StatusLogger Log4j2 could not find a logging implementation
Asked Answered
T

10

67

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.

Testes answered 19/12, 2017 at 7:21 Comment(3)
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.Ruderal
Please consider accepting one of the answers if it was useful or post your own if you managed to solve it in another way.Ruderal
Thanks for the reminder. I found another ways I have posted it as another answer. Really appreciate your help.Testes
T
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());
Testes answered 1/2, 2018 at 15:26 Comment(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.Testes
@Testes is log4j.configurationFile deprecated ?Teriteria
@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.Testes
@Testes I did not say log4j2 is deprecated. I said that the property "log4j.configurationFile" is deprecated. It is recommended to use "log4j2.configurationFile"Teriteria
This comment is old, but log4j2 does support property files now. (log4j2.properties) logging.apache.org/log4j/2.x/manual/configuration.htmlGyrocompass
T
32

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>
Thigmotaxis answered 14/2, 2020 at 6:15 Comment(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"Unpremeditated
This is not working for me. can you elaborate more on the solution.Testes
T
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());
Testes answered 1/2, 2018 at 15:26 Comment(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.Testes
@Testes is log4j.configurationFile deprecated ?Teriteria
@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.Testes
@Testes I did not say log4j2 is deprecated. I said that the property "log4j.configurationFile" is deprecated. It is recommended to use "log4j2.configurationFile"Teriteria
This comment is old, but log4j2 does support property files now. (log4j2.properties) logging.apache.org/log4j/2.x/manual/configuration.htmlGyrocompass
Y
11

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

Yasminyasmine answered 17/4, 2023 at 15:29 Comment(0)
F
8

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.

Farris answered 9/3, 2021 at 10:37 Comment(0)
W
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

Woodring answered 19/12, 2019 at 15:42 Comment(2)
What does Spring have to do with this?!?Deflower
I had a related error using log4j2 in a spring projectWoodring
C
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

Cyclo answered 11/5, 2022 at 18:35 Comment(0)
C
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

Cravat answered 10/12, 2020 at 12:56 Comment(0)
M
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);

Malita answered 8/2, 2022 at 17:56 Comment(0)
I
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

Indolence answered 25/6, 2023 at 7:7 Comment(0)
C
-3

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

Checkers answered 19/9, 2022 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.