Search code examples
javaelasticsearchlog4j2

Elasticsearch config with log4j2.xml


I'm iniatilizing the elasticsearch client with new PreBuiltTransportClient(Settings.EMPTY); and I have the following config:

pom.xml

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.1</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.1</version>
</dependency>

since log4j2 takes the xml file ==> log4j2.xml:

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

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
        </Console>

        <RollingFile name="RollingFile" filename="log/rolling.log"
            filepattern="${logPath}/%d{YYYYMMddHHmmss}-rolling.log">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="100 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingFile>

    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="RollingFile" />
        </Root>
        <logger name="org.springframework.web">
            <level value="info" />
            <appender-ref ref="Console" />
        </logger>
    </Loggers>
</Configuration>

but the initialization throws nasty config exceptions, namely

Error while converting string [] to type [class.org.apache.logging.log4j.Level]. Using default value [null]. java.lang.IllegalArgumentException: Unknown level constant [].

I would have expected some config is missing for the logging. Searching for proper config I found only hints for the log4j.properties files - which I don't want to use. I guess I need to configure an appropriate logger name - but don't know which. org.elasticsearch.common.logging did not help.

How to configure it properly?


Solution

  • I think the error lies in your xml file. Inside your loggers tag, you have defined a logger incorrectly. Could you try the following?

    <Loggers>
        <Root level="INFO">
          <AppenderRef ref="Console"/>
          <AppenderRef ref="RollingFile" />
        </Root>
        <Logger name="corg.springframework.web" level="INFO">
          <AppenderRef ref="Console"/>
        </Logger>
    </Loggers>
    

    The level needs to be inside the logger line, instead of a separate level line.

    Source: log4j