No log4j2 configuration file found. Using default configuration: logging only errors to the console
Asked Answered
F

17

81
$ java -Dlog4j.configuration=file:///path/to/your/log4j2.xml -jar /path/to/your/jar_file.jar

Written to the console, you get

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

But, it also looks like the configuration file has been found and was not parsable:

    log4j:WARN Continuable parsing error 2 and column 31
    log4j:WARN Document root element "Configuration", must match DOCTYPE root "null".
    log4j:WARN Continuable parsing error 2 and column 31
    log4j:WARN Document is invalid: no grammar found.
    log4j:ERROR DOM element is - not a <log4j:configuration> element.
    log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Flaviaflavian answered 17/2, 2015 at 22:57 Comment(1)
make sure you don't have multiple log4j version on your build path.. it looks like on first line is about log4j 2.x and the last log4j:WARN and log4j:ERROR is made from log4j 1.x... they have different configurations..Neslund
B
10

I have been dealing with this problem for a while. I have changed everything as described in this post and even thought error occured. In that case make sure that you clean the project when changing settings in .xml or .properties file. In eclipse environment. Choose Project -> Clean

Bracteole answered 12/7, 2016 at 10:28 Comment(4)
excellent point. In my particular setup, a Maven -> Install does not remove previously created resource files. So, log4j2 config files may hang around even after dependencies have been changed.Flaviaflavian
@LPD Please read answer correctly. It has been said that if non of those anwsers are helpfull, problem can be caused by cached files. Note that same problem may have different causes. So there may not be only one solution. As in this case.Bracteole
In my case, I had filtering enabled in my pom file under build/resource. I had to add an include clause for XML files. If anyone is still having this issue, you might want to check that as well.Pentecostal
@enissay, thanks. your comment saved me. Totally forgot to check about the includesKris
F
89

Problem 1

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

Solution 1

To work with version 2 of log4j aka "log4j2"

-Dlog4j.configuration=

should read

-Dlog4j.configurationFile=

Problem 2

log4j:WARN ....

Solution 2

In your project, uninclude the log4j-1.2 jar and instead, include the log4j-1.2-api-2.1.jar. I wasn't sure how exactly to exclude the log4j 1.2. I knew that what dependency of my project was requiring it. So, with some reading, I excluded a bunch of stuff.

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.10</artifactId>
    <version>0.8.2.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>                
        </exclusion>
        <exclusion>
            <groupId>org.apache.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>          
    </exclusions>
</dependency>

I am not sure which of the exclusions did the trick. Separately, I included a dependency to the 1.2 api which bridges to 2.x.

<!--
    http://logging.apache.org/log4j/2.0/manual/migration.html
    http://logging.apache.org/log4j/2.0/maven-artifacts.html
    Log4j 1.x API Bridge
    If existing components use Log4j 1.x and you want to have this logging
    routed to Log4j 2, then remove any log4j 1.x dependencies and add the
    following.
-->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.2</version>
</dependency>

Now, the 1.2 logs which were only going to the console actually flow to our 2.x appenders.

Flaviaflavian answered 17/2, 2015 at 22:57 Comment(8)
Looks like you already found the answer. Be aware that the log4j:WARN output is produced by a log4j-1.2 jar that is on the classpath. You may want to remove that jar from the classpath, and instead use the log4j-1.2-api-2.1.jar (the 1.2 bridge that is part of log4j2). See also logging.apache.org/log4j/2.x/faq.html#which_jarsIgnatz
Hey, @RemkoPopma I discovered that you contribute to the API. ;-)Curriery
I do my best. Haven't had time recently though... If you feel like helping out, contributions are welcome!Ignatz
@MaasSql, is this issue resolved now or are your still experiencing issues?Ignatz
@RemkoPopma, your comments make sense, however, I am very new to Java / Maven, so I don't know how to substitute the bridge api for log4j-1.2.Flaviaflavian
I would recommend creating a small maven project and experiment with that, then after you've learned how to do this, copy dependencies to your actual project.Ignatz
Remember if migrating a maven project from log4j 1 to log4j2 and you're using a local container (like maven-tomcat-plugin or maven-cargo-plugin) that you update the property passed into the plugin to enable logging in your local workspaceRhine
You've just saved the rest of my nerves... I've spent a couple of hours making it ignoring log4j2.json file which is used only on production env where graylog exists. It failed on my local machine since there's no graylogBaryton
C
18

i had same problem, but i noticed that i have no log4j2.xml in my project after reading on the net about this problem, so i copied the related code in a notepad and reverted the notepad file to xml and add to my project under the folder resources. it works for me.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
  <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="DEBUG">
  <AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Chinquapin answered 3/8, 2018 at 12:19 Comment(0)
C
16

If you don't have the fortune of the log4j-1.2.jar on your classpath as Renko points out in his comment, you will only see the message no log4j2 configuration file found.

This is a problem if there is an error in your configuration file, as you will not be told where the problem lies upon start-up.

For instance if you have a log4j2.yaml file which log4j2 fails to process, because for example, you have not configured a YAML parser for your project, or your config is simply incorrect. You will encounter the no log4j2 configuration file found message, with no further information. This is the case even if you have a valid log4j2.xml file, as log4j2 will only attempt to process the first configuration file it finds.

I've found the best way to debug the problem is to explicitly state the configuration file you wish to use as per the command line argument mentioned above.

-Dlog4j.configurationFile=

Hopefully this will help you pinpoint if the issue is actually caused by your classloader not finding the log4j2 configuration file or something else in your configuration.

Update

You can also use the below property to change the default level of the status logger to get further information:

-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=<level>
Cloris answered 18/2, 2015 at 23:15 Comment(3)
There is a debug flag, but it is not well documented: - Until a configuration is found, status logger level can be controlled with system property org.apache.logging.log4j.simplelog.StatusLogger.level. - After a configuration is found, status logger level can be controlled in the configuration file with the "status" attribute, for example: <Configuration status="trace">.Ignatz
@RemkoPopma thanks - I actually tried that flat earlier as per the documentation. However there it's stated as log4j2.StatusLogger.level which doesn't work. The property you mentioned does work.Cloris
Yes the docs are confusing. Partly because the StatusLogger behaviour changes during configuration. I'll improve the docs as proposed here: issues.apache.org/jira/browse/LOG4J2-955Ignatz
B
10

I have been dealing with this problem for a while. I have changed everything as described in this post and even thought error occured. In that case make sure that you clean the project when changing settings in .xml or .properties file. In eclipse environment. Choose Project -> Clean

Bracteole answered 12/7, 2016 at 10:28 Comment(4)
excellent point. In my particular setup, a Maven -> Install does not remove previously created resource files. So, log4j2 config files may hang around even after dependencies have been changed.Flaviaflavian
@LPD Please read answer correctly. It has been said that if non of those anwsers are helpfull, problem can be caused by cached files. Note that same problem may have different causes. So there may not be only one solution. As in this case.Bracteole
In my case, I had filtering enabled in my pom file under build/resource. I had to add an include clause for XML files. If anyone is still having this issue, you might want to check that as well.Pentecostal
@enissay, thanks. your comment saved me. Totally forgot to check about the includesKris
K
9

I use hive jdbc in a java maven project and have the same issues.

My method is to add a log4j2.xml file under src/main/java/resources

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyClassName {
    private static final Logger LOG = LoggerFactory.getLogger(MyClassName.class);
}

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="<your_package_name>.<your_class_name>" level="debug">
        <AppenderRef ref="Console"/>
        </Logger>
        <Root level="WARN">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
Kessel answered 15/2, 2018 at 18:8 Comment(0)
F
5

Stuffs I check to verify logging,

1) Check that there're no older log4j versions.

mvn dependency:tree | grep log ## ./gradlew dependencies | grep log
[INFO] +- com.prayagupd:log-service:jar:1.0:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.6.2:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.6.2:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.1:compile

2) make sure I'm setting log4j2.json properly, which is done with -Dlog4j.configurationFile=???

val logConfig: PropertiesConfiguration = new PropertiesConfiguration("application.properties")
System.setProperty("log4j.configurationFile", logConfig.getString("log4j.config.file"))

println("log4j.configurationFile :: " + System.getProperty("log4j.configurationFile"))

or

Configurator.initialize(null, logConfig.getString("log4j.config.file"));

Also if auto detection is happening make sure the file name is log4j2.* not log4j.*

3) Another check is for parser for log4j2.json. Thanks log4j team for not providing parser within the API.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.0</version>
</dependency>

This might throw an exception if can not find the json parser

[Fatal Error] log4j2.json:1:1: Content is not allowed in prolog.
Fibrilliform answered 21/11, 2016 at 7:58 Comment(0)
D
5

Make sure that you have put a log4j2.* file instead of a log4j.* file under .../src/main/resources folder.

Danica answered 3/3, 2019 at 0:24 Comment(0)
S
3

Answer for Intellij users 2022

If you are using Intellij:

Please follow these steps:

  1. Put "resources" folder under "YourProject" root and mark it as "Resources Root".

  2. Put "log4j2.xml" in resources folder

Sitting answered 8/9, 2022 at 4:52 Comment(0)
T
2

In my case I am using the log4j2 Json file log4j2.json in the classpath of my gradle project and I got the same error.

The solution here was to add dependency for JSON handling to my gradle dependencies.

compile group:"com.fasterxml.jackson.core", name:"jackson-core", version:'2.8.4'
compile group:"com.fasterxml.jackson.core", name:"jackson-databind", version:'2.8.4'
compile group:"com.fasterxml.jackson.core", name:"jackson-annotations", version:'2.8.4'

See also documentation of log4j2:

The JSON support uses the Jackson Data Processor to parse the JSON files. These dependencies must be added to a project that wants to use JSON for configuration:

Tachymetry answered 4/11, 2016 at 11:38 Comment(0)
D
2

I am getting this error because log4j2 file got deleted from my src folder

Simply add xml file under src folder

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
    </Console>

    <RollingFile name="RollingFile" filename="logs/B2CATA-hybrid.log"
        filepattern="${logPath}/%d{yyyyMMddHHmmss}-fargo.log">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
        <Policies>
            <SizeBasedTriggeringPolicy size="100 MB" />
        </Policies>
        <DefaultRolloverStrategy max="20" />
    </RollingFile>

</Appenders>
<Loggers>
   <Logger name="com.pragiti." level="trace" />
    <Root level="info">
        <AppenderRef ref="Console" />
        <AppenderRef ref="RollingFile" />
    </Root>
</Loggers>

Daria answered 9/5, 2018 at 12:23 Comment(1)
I had it in the same base folder as the pom.xml . Moving it to src/ did the trick. Thanks for the config !Anabatic
C
2
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
  <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="DEBUG">
  <AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
  1. Create a new Text document and copy-paste the above code and save it as log4j2.xml.

  2. Now copy this log4j2.xml file and paste it under your src folder of your Java project.

  3. Run your java program again, you will see error is gone.

Calycle answered 30/4, 2020 at 2:12 Comment(0)
R
1

I am working on TestNG Maven project, This worked for me by adding <resources> tag in pom.xml, this happens to be the path of my custom configuration file log4j2.xml.

<url>http://maven.apache.org</url>
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
  <resources>
    <resource>
      <directory>src/main/java/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M3</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>
<dependencies>
  <dependency>
Retain answered 13/9, 2019 at 17:42 Comment(0)
L
1

The issue solved after renaming the xml file name to log4j2.xml

Londalondon answered 28/11, 2019 at 18:3 Comment(0)
E
1

Tested with: log4j-ap 2.13.2, log4j-core 2.13.2.

  1. Keep XML file directly under below folder structure. src/main/java
  2. In the POM:
    <build>   
 <resources>       
             <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>             
            </resource>
 </resources>  
</build>
  1. Clean and build.
Epiphytotic answered 31/12, 2020 at 19:31 Comment(0)
C
0

I have solve this problem by configuring the build path. Here are the steps that I followed: In eclipse.

  1. create a folder and save the logj4 file in it.
  2. Write click in the folder created and go to Build path.
  3. Click on Add folder
  4. Choose the folder created.
  5. Click ok and Apply and close.

Now you should be able to run

Circumfluous answered 6/12, 2017 at 10:20 Comment(0)
R
0

This sometimes can be thrown before the actual log4j2 configuration file found on the web servlet. at least for my case I think so. Cuz I already have in my web.xml

  <context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>classpath:log4j2-app.xml</param-value>
  </context-param>

and checking the log4j-web source; in class

org.apache.logging.log4j.web.Log4jWebInitializerImpl

there is the line;

String location = this.substitutor
                 .replace(this.servletContext.getInitParameter("log4jConfiguration"));

all those makes me think that this is temporary log before configuration found.

Remediless answered 28/5, 2019 at 7:7 Comment(0)
C
0

Just try below steps, it will work,

  1. Keep XML file directly under below folder structure. src/main/java
  2. Make sure to save the pom.xml file in case any changes made..
Colloquialism answered 15/5, 2020 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.