jsosnowski
jsosnowski

Reputation: 1590

Log4j2 doesn't see configuration xml

Probably, I searched all Internet, but I must miss something.

I configure Log4j2 and Commons-logging according to documentation. I add log4j2.xml, but its configuration has no impact of application output. I looks like Log4j doesn't read this file. Here is my config:

pom.xml

<properties>
    <log4j2.version>2.3</log4j2.version>
</properties>

<dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
</dependencies>

\src\main\resources\log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">

    <Properties>
        <Property name="filename">fileName</Property>
    </Properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="Pattern Pattern %d{DEFAULT} %-5level %t : %logger{1} - %m%n"/>
        </Console>
        <RollingFile name="RollingFile"
                     fileName="${filename}.log"
                     filePattern="${filename}-%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="%d{DEFAULT} %-5level %t : %logger{1} - %m%n"/>
            <TimeBasedTriggeringPolicy/>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Logger name="com.mydomain" level="TRACE"/>
        <Root level="TRACE">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

In any class - for example class A

private static final Log logger = LogFactory.getLog(A.class);

Application has main() function, so could be run by IDE or by mvn exec:java.

Problem:

Logging is working - but with default way.

As you can see in log4j2.xml file I change levels to TRACE and in pattern I type: Pattern Pattern to check if log4j2 is using my custom configuration.

No logger.trace() appears. No text Pattern Pattern. No error/warning from log4j in console.

Please help, and tell me what I made wrong?

EDIT:

@BrunoMarcoVisioli told me to use org.apache.logging.log4j.LogManager instead of LogFactory. So I add new logger in main() method:

private static final Logger logger_new = LogManager.getLogger(CalculationApp.class);

And logging through this logger_new works fine and so I set TRACE level, I see also some logs at application startup, first:

Starting configuration XmlConfiguration[location=target\classes\log4j2.xml]

But logging through LogFactory still doesn't work.

Upvotes: 1

Views: 3330

Answers (2)

Bruno Marco Visioli
Bruno Marco Visioli

Reputation: 418

You need to add the log4j-jcl dependency to your POM, it'll work as bridge between commons-logging and log4j2.

Upvotes: 5

Jim Ward
Jim Ward

Reputation: 559

Its usually a problem with where the file is located..

From the documentation

By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath.

Upvotes: -2

Related Questions