Using a XML File (log4j2.xml) to configure Log4j 2

后端 未结 7 1123
逝去的感伤
逝去的感伤 2020-12-24 07:09

I want to use the new Log4J 2 - Java Logging Framework. Everything work fine, but I tried since a hour to load a custom configuration file to configure the logging (like log

相关标签:
7条回答
  • 2020-12-24 07:22

    Besides to use the default configuration file, log4j2.xml (or log4j2-test.xml) in the class path root folder, you can use this system property to specify the configuration file path (use the -D in the java command line)

    log4j.configurationFile='xxxx/xxx/xxx'  
    
    0 讨论(0)
  • 2020-12-24 07:27

    This is more a valuable tip for solving the "I don't if log conf file is being read" problem. Start your jvm with the option -Dlog4j.debug. When it starts it will print all the log configuration file that it tries to read. It will list one by one. The last one displayed is the one the system uses.

    If you are in a JEE server, you will probably need to put it in a "jvm.options" file somewhere. If its a jar, just run java with -Dlog4j.debug.

    0 讨论(0)
  • 2020-12-24 07:28

    Put the LOG4J2.XML in the project´s root folder src/main.

    0 讨论(0)
  • 2020-12-24 07:29

    TIP : To use custom log4j files. (Rather default "log4j2.xml" thingie). It might be useful to have multiple configurations in some instances.

    Just in case if any one using maven and wish to configure log4j2, Here is what you need in your pom.xml :

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

    By default, "log4j2.xml" is the file name expected. But however, we can override that by setting log4j.configuration system property. In the above example, I have used custom configuration file as "Foo-log4j.xml".

    You can do the same via CLI :

    -Dlog4j.configuration="Foo-log4j.xml"
    
    0 讨论(0)
  • 2020-12-24 07:29

    My 2 cents:

    Software system: log4j 2, NetBeans and TestNG.

    I had the same problem, but with the testing environment.

    In default, (that is, in log4j2.xml file located in src/) the logging level is set to error. But when running the tests, I want the logging to be trace. And, of course logged to some more or less hardcoded file.

    That is, originally I did something like:

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

    The problem is that by the time the System.setProperty instruction will be executed, the log4j will be already be set (That is, the static final Logger log =... will be executed first.)

    What I did, is use the @BeforeClass like this:

     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, resources/ can be placed in test packages so you don't ship the "test" log4j setup.

    Hope it helps

    0 讨论(0)
  • 2020-12-24 07:37

    Important: make sure the name of the configuration file is log4j2.xml (note the 2 before the period) as opposed to log4j.xml

    My guess is nothing's happening because nothing is logged using the error level. You may want to try adding another logger like so:

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

    Looks like the Configuration section might be a good resource.

    To elaborate further, you're specifying a logger with the level set to "error":

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

    This means that only the messages logged using Level.ERROR will show up in the log. Adding a logger with a less restrictive level will allow for more messages to appear in the log. I recommend taking a look at the Architecture section of the manual (if you scroll down the page you'll see the table that explains logging levels). Alternatively, you could just change the level of the root logger to trace (instead of adding a new logger)

    Given the configuration you specified, executing the code below yields something like 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");
        }
    }
    
    0 讨论(0)
提交回复
热议问题