Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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 it's located 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, place your log4j2.xml file there and add that folder to your runtime classpath

Since the official website can't help me, I hope you can help me get Log4j 2 working with my configuration file.

like image 275
Rubberducker Avatar asked Nov 05 '12 15:11

Rubberducker


People also ask

Where do I put my log4j2 XML file?

We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.

Where do I put log4j xml configuration file?

This logging system is configured by placing the log4j. xml file in the web application's WEB-INF/classes folder and by specifying the log output path for appenders and the level of logging.

Can we use log4j and log4j2 together?

Log4j 2 provides support for the Log4j 1 logging methods by providing alternate implementations of the classes containing those methods. These classes may be found in the log4j-1.2-api jar distributed with the project.


2 Answers

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");     } } 
like image 109
Andrey Avatar answered Sep 21 '22 14:09

Andrey


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" 
like image 21
Balaji Boggaram Ramanarayan Avatar answered Sep 18 '22 14:09

Balaji Boggaram Ramanarayan