AWS lambda not working with Log4j 2 appender

11,935

Solution 1

please note that there is/was a bug in the AWS documentation for lambda logging in java. I have pushed a fix to github for it, but in the mean time see my answer to another post it may be what you need.

tldr; remove .LambdaAppender from the packages attribute of the <Configuration ... tag

see this other stackoverflow question

Solution 2

That error message is produced by an old version of Log4j (1.2.x) that’s on the classpath somehow. Log4j 2.x error messages look different.

Please remove the Log4j 1.2.x jar from the classpath. If any of the libraries have a dependency on Log4j 1.2, add the adapter log4j-1.2-api-2.8.2.jar.

Solution 3

Logging reference

When creating an Alexa Skill - its dependencies already contain log4j components, but laking the Log4j appender. Add aws-lambda-java-log4j2 to dependencies:

Maven

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-log4j2</artifactId>
    <version>1.1.0</version>
</dependency>

Gradle:

dependencies {
    compile 'com.amazonaws:aws-lambda-java-log4j2:1.1.0'
}

Error variants

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.

Add: log4j2.xml (described in Logging reference) to src/main/resources

StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console

Add (same version as in existing dependencies):

   dependencies {
    compile 'com.amazonaws:aws-lambda-java-core:1.2.0'
       compile 'org.apache.logging.log4j:log4j-core:2.8.2’
   }

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.

Add: log4j2.xml (described above) to src/main/resources

Error processing element Lambda ([Appenders: null]): CLASS_NOT_FOUND Unable to locate appender "Lambda" for logger config "root"

Add:

dependencies {
   compile 'com.amazonaws:aws-lambda-java-log4j2:1.1.0'    
}

enter image description here

enter image description here

Share:
11,935
Qedrix
Author by

Qedrix

Updated on June 27, 2022

Comments

  • Qedrix
    Qedrix almost 2 years

    I am trying to write a java based lambda function. Everything works fine except the logging. I have the log4j2.xml file in classpath.

    I have also followed the instruction laid down in AWS Lambda LOgging in Java to the word.

    I get this in the log when I trigger the Lambda function.

    log4j:WARN No appenders could be found for logger (com.amazonaws.AmazonWebServiceClient). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

    I use maven to package the jar. The pom.xml has the following dependencies.

    <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.amazon.alexa</groupId>
      <artifactId>alexa-skills-kit</artifactId>
      <version>${alexa.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.directory.studio</groupId>
      <artifactId>org.apache.commons.io</artifactId>
      <version>2.4</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.0.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-dynamodb</artifactId>
      <version>1.9.40</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-log4j2</artifactId>
      <version>1.0.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>
    

    I am not sure what else I must do to make this work.