Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 - Error processing element Appender

Tags:

java

log4j2

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?

like image 785
Stoffl Avatar asked May 02 '16 08:05

Stoffl


2 Answers

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.

like image 200
Luke Woodward Avatar answered Nov 19 '22 04:11

Luke Woodward


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">
...
like image 25
Mike Avatar answered Nov 19 '22 04:11

Mike