ares
ares

Reputation: 4413

log4j2 - ERROR loggers Loggers has no parameter that matches element AppenderRef

While using log4j2 following message gets logged to console nothing else gets logged.

2016-05-05 12:28:33,023 ERROR loggers Loggers has no parameter that matches element AppenderRef
2016-05-05 12:28:33,023 WARN No Root logger was configured, creating default ERROR-level Root logger with Console appender

Following is the code and log4j2.xml which is present in the classpath at runtime.

package com.example.log4j;

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

public class LogTester {

    public static void main(String[] args) {


        Logger logger = LogManager.getLogger(LogTester.class);
        logger.trace("Hello");
        logger.debug("Hello");
        logger.info("Hello");
        logger.warn("Hello");
        logger.error("Hello");
        logger.fatal("Hello");


    }

}

log4j2.xml file reads as below

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%m%n" />
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="com.example.log4j" level="DEBUG" additivity="false" />
        <AppenderRef ref="STDOUT" />
    </Loggers>
</Configuration>

In the pom.xml, following dependencies are added and Java version is 1.6

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>

Upvotes: 2

Views: 16063

Answers (2)

Vishwa Ratna
Vishwa Ratna

Reputation: 6420

Your log4j2.xml file reads as below:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%m%n" />
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="com.example.log4j" level="DEBUG" additivity="false">
        <AppenderRef ref="STDOUT" />
    </Loggers>
</Configuration>

Apart from above you must add a root logger as well else Log4j will create one for you that logs to the console. To do that you need to add below code inside <Loggers> tag.

<Loggers>
        <Root level="debug">
            <AppenderRef ref="YOUR_APPENDER_REFERENCE" />
        </Root>
</Loggers>

I will suggest to add a RollingFile as a Appender to output the log files to a text file in directory, logging a large data on console is of no use.

Upvotes: 1

ares
ares

Reputation: 4413

There was an error in log4j2.xml file.

<Loggers>
    <Logger name="com.example.log4j" level="DEBUG" additivity="false" />
    <AppenderRef ref="STDOUT" />
</Loggers>

should be

<Loggers>
    <Logger name="com.example.log4j" level="DEBUG" additivity="false">
        <AppenderRef ref="STDOUT" />
    </Logger>
</Loggers>

log4j really needs to have an xsd to validate the xml configuration.

Upvotes: 2

Related Questions