log4j2 - ERROR Loggers has no parameter that matches element AppenderRef

Viewed 381

I need help on configuring my log4j2.xml file. I get an error saying "Loggers has no parameter that matches element AppenderRef" so I'm not sure what I'm doing wrong here.

This is the log4j2.xml file

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

<Loggers>
    <logger name="org.springframework" level="WARN"/>
    <logger name ="java.sql" level="ERROR"/>
    <AppenderRef ref="appLog" />

    <Root level="INFO">
       <AppenderRef ref="appLog" />
    </Root>

</Loggers>
</Configuration>

And this is the pom.xml that has the dependencies for log4j2

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

   <dependency>
       <groupId>com.oracle</groupId>
       <artifactId>ojbc14</artifactId>
       <version>10.2.0.3.0</version>
       <type>pom</type>
   </dependency>
1 Answers

The error is saying that Loggers has no parameter that matches element AppenderRef.

You placed <AppenderRef ref="appLog" /> into <Loggers></Loggers> section.

However, appenders should be on <Configuration> level and defined not as <AppenderRef> but as <Appenders>.

Documentation with examples: https://logging.apache.org/log4j/2.x/manual/configuration.html

Working code would look like this:

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

    <Appenders>
        <Console name="appLog" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
        </Console>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="appLog" />
        </Root>

        <logger name="org.springframework" level="WARN"/>
        <logger name ="java.sql" level="ERROR"/>
    </Loggers>

</Configuration>
Related