Stoffl Stoffl - 7 months ago 321
Java Question

Log4j2 - Error processing element Appender

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?

Answer

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.