Using an XML file (log4j2.xml) to configure Log4j 2

I want to use the new Log4J 2-Java Logging Framework. Everything works fine, but I tried an hour later to upload a custom configuration file to set up logging (e.g. log level).

This is my log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
  <appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </appenders>
  <loggers>
    <root level="error">
      <appender-ref ref="Console"/>
    </root>
  </loggers>
</configuration>

I tried the following but nothing works:

  • Move the log4j2.xml file so that it is in the default package.
  • Move the log4j2.xml file anywhere in the project
  • Name the log4j2.xml file as "log4j.xml"
  • Create a folder in your project, put the log4j2.xml file there and add this folder to your path to the runtime class

Since the official site cannot help me, I hope you can help me get Log4j 2 to work with my configuration file.

+23
7

: , log4j2.xml ( 2 ), log4j.xml

, . , :

<logger name="com.foo.Bar" level="trace">
  <appender-ref ref="Console"/>
</logger>

, Configuration .

, , "":

<root level="error">
  <appender-ref ref="Console"/>
</root>

, , Level.ERROR. . Architecture ( , , ). , ( )

, - 13:27:50.244 [main] ERROR com.example.Log4j2Tester - testing ERROR level

package com.example;

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

public class Log4j2Tester {
    private static final Logger LOG = LogManager.getLogger(Log4j2Tester.class);

    public static void main(String[] args) {
        LOG.error("testing ERROR level");

//if you change the level of root logger to 'trace'
//then you'll also see something like
//    13:27:50.244 [main] TRACE com.example.Log4j2Tester - exiting application
        LOG.trace("exiting application");
    }
}
+31

: log4j. ( "log4j2.xml" thingie). , .

, - maven log4j2, pom.xml:

                <systemProperties>
                    <property>
                        <name>/log4j.configuration</name>
                        <value>.../../Foo-log4j.xml</value>
                    </property>
                </systemProperties>

"log4j2.xml" - . , system log4j.configuration. "Foo-log4j.xml".

CLI:

-Dlog4j.configuration="Foo-log4j.xml"
+3

2 :

: log4j 2, NetBeans TestNG.

, .

( log4j2.xml, src/), . , . , , - .

- :

public class someTestClass {
       static final Logger log=LogManager.getLogger("test.someTestClass");
......
@Test
public void SomeMethod(){

System.setProperty("log4j.configurationFile", "resources/log4j2_conf.xml");

log.trace("This will not show because the default logging level is ERROR
and the resources/log4j2_conf.xml is not loaded");

}

, , System.setProperty, log4j ( Logger =...)

, @BeforeClass :

 public class someTestClass {
    Logger log;      
    @BeforeClass
    public void setLogger(){
        SetEnv.SetEnvironment();
        log = LogManager.getLogger("TestRealClientRealServer");
    }
......
    @Test
    public void SomeMethod(){

        log.trace("This will show, because in log4j2_conf.xml
 the root level is set to TRACE");

    }
}

BTW, / , "test" log4j setup.

,

+1

, log4j2.xml( log4j2-test.xml) , , ( -D java )

log4j.configurationFile='xxxx/xxx/xxx'  
+1

eclipse:

"". , log4j2.xml eclipse.

: log4j 2 -

0

" , conf ". jvm -Dlog4j.debug. , . . , .

JEE, , , "jvm.options". , java -Dlog4j.debug.

0

Place LOG4J2.XML in the root folder of the src / main project .

-4
source

All Articles