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

前端 未结 15 1103
情深已故
情深已故 2020-12-04 22:59
$ java -Dlog4j.configuration=file:///path/to/your/log4j2.xml -jar /path/to/your/jar_file.jar

Written to the console, you get

ERROR          


        
相关标签:
15条回答
  • 2020-12-04 23:49

    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

    0 讨论(0)
  • 2020-12-04 23:50

    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.

    0 讨论(0)
  • 2020-12-04 23:51
    <?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.

    0 讨论(0)
  • 2020-12-04 23:54

    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.
    
    0 讨论(0)
  • 2020-12-04 23:59

    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>
    
    0 讨论(0)
  • 2020-12-05 00:02

    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>
    
    0 讨论(0)
提交回复
热议问题