3

I am getting following error while trying to use AWS lambda with log4j2. I followed all instructions given at:

https://github.com/aws/aws-lambda-java-libs/tree/master/aws-lambda-java-log4j2

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.

Can someone please help with this?

Yuri
  • 4,254
  • 1
  • 29
  • 46
Neel
  • 303
  • 1
  • 4
  • 15

1 Answers1

3

I'm able to get this to work just fine - lets see where you're setup is different. I'm using OpenJDK 11 and the Java 11 Lambda. My Lambda handler looks like:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LambdaHandler {
    private static final Logger logger = LogManager.getLogger(LambdaHandler.class);

    // your handler entry point may be different but that shouldn't matter
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
    }
}

The dependencies in my pom.xml are: com.amazonaws aws-lambda-java-core 1.2.0

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-log4j2</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.13.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.13.0</version>
    </dependency>

    <dependency>
        <groupId>com.github.edwgiz</groupId>
        <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
        <version>2.8.1</version>
    </dependency>

and the build/plugins section for the shade plugin is:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Multi-Release>true</Multi-Release>
                        </manifestEntries>
                    </transformer>
                    <transformer
                            implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
                    </transformer>
                </transformers>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.github.edwgiz</groupId>
                    <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
                    <version>2.8.1</version>
                </dependency>
            </dependencies>
        </plugin>

When my Lambda starts I get the message:

WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.

I've googled this but haven't been able to fix it yet. However, it doesn't seem to hurt anything.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • Thanks @Stdunbar. It seems my log4j2.xml was not on class path. After fixing that it works fine. – Neel Apr 08 '20 at 22:49
  • Re: the WARNING about getCallerClass not being supported - this is because the log4j2 jar is a multi-release jar which are not supported by AWS Lambda. To fix this, see my suggestion here: https://stackoverflow.com/a/67978671/2404240 – Maarten Brak Jun 15 '21 at 00:52