1

I'm facing issue with log4j2

below is my log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.opensymphony.xwork2" level="info"/>
        <Logger name="org.apache.struts2" level="info"/>
        <Root level="info">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

below is the exception

<Jun 21, 2018 7:23:48 PM IST> <Error> <HTTP> <BEA-101165> <Could not load user defined filter in web.xml: org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.
java.lang.ExceptionInInitializerError
        at org.apache.logging.log4j.core.impl.Log4jLogEvent.createContextData(Log4jLogEvent.java:472)
        at org.apache.logging.log4j.core.impl.Log4jLogEvent.<init>(Log4jLogEvent.java:331)
        at org.apache.logging.log4j.core.impl.DefaultLogEventFactory.createEvent(DefaultLogEventFactory.java:54)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:401)
        at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
        Truncated. see log file for complete stacktrace
Caused By: java.lang.IllegalArgumentException: Initial capacity must be at least one but was 0
        at org.apache.logging.log4j.util.SortedArrayStringMap.<init>(SortedArrayStringMap.java:102)
        at org.apache.logging.log4j.core.impl.ContextDataFactory.createContextData(ContextDataFactory.java:109)
        at org.apache.logging.log4j.core.impl.ContextDataFactory.<clinit>(ContextDataFactory.java:57)
        at org.apache.logging.log4j.core.impl.Log4jLogEvent.createContextData(Log4jLogEvent.java:472)
        at org.apache.logging.log4j.core.impl.Log4jLogEvent.<init>(Log4jLogEvent.java:331)
        Truncated. see log file for complete stacktrace
>

above exception resulting to failure of war file deployment.

below are the jars used

1.commons-fileupload-1.3.3.jar
2.commons-io-2.5.jar
3.commons-lang3-3.6.jar
4.commons-logging-1.1.3.jar
5.freemarker-2.3.26-incubating.jar
6.javassist-3.20.0-GA.jar
7.log4j-1.2-api-2.11.0.jar
8.log4j-api-2.10.0.jar
9.log4j-core-2.11.0.jar
10.ognl-3.1.15.jar
11.struts2-core-2.5.16.jar

what do i need to correct?

2 Answers 2

2

Try upgrading log4j-api-2.10.0.jar to 2.11.0.

IllegalArgumentException is thrown from the constructor of org.apache.logging.log4j.util.SortedArrayStringMap:

public SortedArrayStringMap(final int initialCapacity) {
    if (initialCapacity < 1) {
        throw new IllegalArgumentException("Initial capacity must be at least one but was " + initialCapacity);
    }
    threshold = ceilingNextPowerOfTwo(initialCapacity);
}

and the given parameter initialCapacity has been changed since 2.11 as follows:

https://github.com/apache/logging-log4j2/blob/log4j-2.10.0/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java#L54

https://github.com/apache/logging-log4j2/blob/log4j-2.11.0/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java#L57

Sign up to request clarification or add additional context in comments.

5 Comments

This answer is close, but i think is backwards, 2.10 is has the initialCapacity of 1 where 2.11 is 0, going to 2.10 fixed it for me.
Yes, I was using 2.11.0 and facing same issue. moving on 2.10.0 version, fixed issue for me. Thank you.
2.11.2 has the same problem.
I was using older log4j-api (2.9.1) with newer log4j-core (2.11.2) which caused this issue. Updating log4j-api to 2.11.2 solved the issue for me.
The newer log4j-api throws exception if value < 0 but older throws if value < 1 and newer log4j-core initializes with 0 while older log4j-core initializes with 1
0

Updating the log4j-api and log4j-core from 2.11.1 to 2.12.0 solved the same problem for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.