How to solve issue java.lang.NoSuchMethodError: com.amazonaws.services.lambda.runtime.LambdaLogger.log and why do we have this error?
This tutorial will show you how to solve the issue java.lang.NoSuchMethodError: com.amazonaws.services.lambda.runtime.LambdaLogger.log with an unrecognized method log() on LambdaLogger.
Some time ago I had an issue with the logger in my AWS Lambda project while trying to test it using invoke local
on my Lambda function. I was getting the java.lang.NoSuchMethodError: com.amazonaws.services.lambda.runtime.LambdaLogger.log exception and was very frustrated trying to find a solution on the internet. This is more of the stack trace:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.serverless.InvokeBridge.invoke(InvokeBridge.java:95)
at com.serverless.InvokeBridge.<init>(InvokeBridge.java:39)
at com.serverless.InvokeBridge.main(InvokeBridge.java:150)
Caused by: java.lang.NoSuchMethodError: com.amazonaws.services.lambda.runtime.LambdaLogger.log([B)V
at com.amazonaws.services.lambda.runtime.log4j2.LambdaAppender.append(LambdaAppender.java:74)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:433)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:417)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:403)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2091)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2005)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1876)
at org.apache.logging.log4j.spi.AbstractLogger.info(AbstractLogger.java:1421)
at com.serverless.Handler.handleRequest(Handler.java:18)
... 7 more
I created my Serverless project using serverless maven-java template:
serverless create --template aws-java-maven --name test -p aws-java-test
My auto-generated pom.xml contained the following dependencies:
<dependencies>
<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.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.10</version>
</dependency>
</dependencies>
What I figured out that was an issue here is that library aws-lambda-java-log4j2 does not have an adequate logger method and hat com.amazonaws.services.lambda.runtime.LambdaLogger.log is present in the previous version of this library (1.0.0). So, what you need to do here is to replace version 1.1.0 with version 1.0.0 and everything will work like a charm.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-log4j2</artifactId>
<version>1.0.0</version>
</dependency>