Log4j2 - Error processing element Appender
Asked Answered
S

3

13

I wanted to write a simple programm to test log4j2. I copied most of my code form the manual. This is the error I get:

2016-05-02 10:11:31,054 main ERROR Error processing element Appender ([Appenders: null]): CLASS_NOT_FOUND
2016-05-02 10:11:31,054 main ERROR Error processing element Appender ([Appenders: null]): CLASS_NOT_FOUND
2016-05-02 10:11:31,117 main ERROR Unable to locate appender "STDOUT" for logger config "root"
2016-05-02 10:11:31,117 main ERROR Unable to locate appender "File" for logger config "MyLogger"

My java code:

package log4jtest;

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

public class Log4jtest {
    private static final Logger logger = LogManager.getLogger("MyLogger");
    public static void main(String[] args) {
        logger.error("Hello, World!");
    }
}

My log4j2 configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" name="Log4jTest" packages="">
    <Properties>
        <Property name="filename">test.log</Property>
    </Properties>

    <Appenders>
        <Appender type="Console" name="STDOUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Appender>

        <Appender type="File" name="File" fileName="${filename}">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Appender>
    </Appenders>

    <Loggers>
        <Logger name="MyLogger" level="ALL" additivity="false">
            <AppenderRef ref="File" />
        </Logger>
        <Root level="ALL">
            <AppenderRef ref="STDOUT" />
        </Root>
    </Loggers>
</Configuration>

Any ideas how to fix this error?

Schiedam answered 2/5, 2016 at 8:26 Comment(0)
F
16

In Log4j 2 the type of appender is specified in the name of the element, not with a type attribute.

In other words, replace lines such as

    <Appender type="Console" name="STDOUT">

and

    <Appender type="File" name="File" fileName="${filename}">

with

    <Console name="STDOUT">

and

    <File name="File" fileName="${filename}">

See also the Log4j 2 documentation on appenders.

Faletti answered 2/5, 2016 at 8:55 Comment(1)
Actually his syntax would be correct if he simply added strict="true" as an attribute on the Configuration element.Berthold
S
4

In my case it was wrong element name

<Appenders>
    <RollingRandomAccessFileAppender name="stdout" fileName="../logs/app.log" filePattern="../logs/app.%d{yyMMdd}.%i.log.gz">
...

Correct one is

<Appenders>
    <RollingRandomAccessFile name="stdout" fileName="../logs/app.log" filePattern="../logs/app.%d{yyMMdd}.%i.log.gz">
...
Sturrock answered 4/3, 2020 at 10:24 Comment(0)
C
-6

Use following line

public static Logger logger = Logger.getLogger("MyLogger");
Cockayne answered 2/5, 2016 at 8:46 Comment(2)
No, don't use that line. That's how you work with Log4j 1, the questioner is asking about Log4j 2.Faletti
I still the get the same error, if I use public static Logger logger = Logger.getLogger("MyLogger"); instead of private static final Logger logger = LogManager.getLogger("MyLogger");Schiedam

© 2022 - 2024 — McMap. All rights reserved.