Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 - configuring

I am trying to adopt Log4j2 in my new project, but I get my logs in catalina.out, and the first one is always: ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger. It seems that I have done everything according to Log4j2 docs, but still.

Here is what I have actually done:

  1. added log4j-api-2.0-beta3.jar to my project
  2. created a log4j2.xml file and put it in a location that is on the classpath (currently, in /usr/local/tomcat/home/lib. In fact, I took a sample file from Log4J2 web-page.
  3. restarted tomcat.

I am using Tomcat 7, MacOS X 10.8, Java 7.

What am I missing?

Just in case, here is the log4j2.xml I am using:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn" name="MyApp" packages="">
  <appenders>
    <File name="MyFile" fileName="logs/app.log">
      <PatternLayout>
        <pattern>%d %p %C{1.} [%t] %m%n</pattern>
      </PatternLayout>
    </File>
  </appenders>
  <loggers>
    <root level="trace">
      <appender-ref ref="MyFile"/>
    </root>
  </loggers>
</configuration>

like image 520
Ibolit Avatar asked Nov 18 '12 09:11

Ibolit


People also ask

What is configuration status in log4j2?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.

How do you implement log4j2?

There are many ways to use Log4j2 configuration in you application. Using a configuration file written in XML, JSON, YAML or properties file. Programmatically, by creating a configuration factory and configuration implementation. Programmatically, by calling APIs exposed in the configuration interface.

How do I reconfigure log4j2 in code with a specific configuration file?

Reconfigure Log4j Using ConfigurationBuilder with the Configurator. An alternative to a custom ConfigurationFactory is to configure with the Configurator . Once a Configuration object has been constructed, it can be passed to one of the Configurator. initialize methods to set up the Log4j configuration.

Where is log4j configuration file?

Sample log4j Configuration Files During Content Engine installation, two log4j sample files are placed on the system in the ContentEngine\config\samples\ folder: log4j. properties. client: A Java format file that contains client configuration settings.


2 Answers

I had the same problem. After adding also log4j-core-2.0-beta3.jar to the classpath it worked.

like image 60
Markus Wilke Avatar answered Sep 29 '22 07:09

Markus Wilke


In Maven you normally have this logging configuration:

<properties>    
    <slf4j-version>1.7.7</slf4j-version>
    <slf4j-log4j2-version>2.0.1</slf4j-log4j2-version>
</properties>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j-version}</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>${slf4j-log4j2-version}</version>
</dependency>

To add the log core lib you have to:

  1. Add the 2.0.1 to the properties section
  2. Add the log core dependency:

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j2-version}</version>
    </dependency>
    
like image 29
Vlad Mihalcea Avatar answered Sep 29 '22 07:09

Vlad Mihalcea