GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — aws-lambda-java-libs/aws-lambda-java-log4j2 at main · aws/aws-lambda-java-libs

Expand file tree

/

aws-lambda-java-log4j2

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

aws-lambda-java-log4j2

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
src
src
 
 
README.md
README.md
 
 
RELEASE.CHANGELOG.md
RELEASE.CHANGELOG.md
 
 
pom.xml
pom.xml
 
 

README.md

Outline

Using log4j2 with AWS Lambda

IMPORTANT: The v1.6.3 release contained a regression (see #612) resulting in missing logs. Please upgrade to v1.6.4 or later. We apologize for the inconvenience.

1. Pull in log4j2 dependencies

Example for Maven pom.xml

<dependencies>
  ...
  <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-log4j2</artifactId>
    <version>1.6.5</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.25.5</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.25.5</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-layout-template-json</artifactId>
    <version>2.25.5</version>
  </dependency>
  ....
</dependencies>

If using maven shade plugin, set the plugin configuration as follows

<plugins>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.6.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer
                    implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
            </transformer>
          </transformers>
        </configuration>
      </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>
  ...
</plugins>

If you are using the John Rengelman Gradle shadow plugin, then the plugin configuration is as follows:

dependencies{
  ...
    implementation group: 'com.amazonaws', name: 'aws-lambda-java-log4j2', version: '1.6.5'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4jVersion
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4jVersion
}

jar {
    enabled = false
}
shadowJar {
    transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer)
}

build.dependsOn(shadowJar)

If you are using the sam build and sam deploy commands to deploy your lambda function, then you don't need to use the shadow jar plugin. The sam cli-tool merges itself the Log4j2Plugins.dat files.

2. Configure log4j2 using log4j2.xml file

Add the following file <project-dir>/src/main/resources/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Lambda name="Lambda" format="${env:AWS_LAMBDA_LOG_FORMAT:-TEXT}">
      <LambdaTextFormat>
        <PatternLayout>
          <pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
        </PatternLayout>
      </LambdaTextFormat>
      <LambdaJSONFormat>
        <JsonTemplateLayout eventTemplateUri="classpath:LambdaLayout.json" />
      </LambdaJSONFormat>
    </Lambda>
  </Appenders>
  <Loggers>
    <Root level="${env:AWS_LAMBDA_LOG_LEVEL:-INFO}">
      <AppenderRef ref="Lambda" />
    </Root>
  </Loggers>
</Configuration>

If the AWS_LAMBDA_LOG_FORMAT is set to JSON, the LambdaJSONFormat formatter will be applied, otherwise the LambdaTextFormat.

3. Example code

package example;

import com.amazonaws.services.lambda.runtime.Context;

import static org.apache.logging.log4j.CloseableThreadContext.put;
import org.apache.logging.log4j.CloseableThreadContext.Instance;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Hello {
    // Initialize the Log4j logger.
    static final Logger logger = LogManager.getLogger(Hello.class);

    public String myHandler(String name, Context context) {
        // System.out: One log statement but with a line break (AWS Lambda writes two events to CloudWatch).
        System.out.println("log data from stdout \n this is continuation of system.out");

       // System.err: One log statement but with a line break (AWS Lambda writes two events to CloudWatch).
        System.err.println("log data from stderr. \n this is a continuation of system.err");

        // Use log4j to log the same thing as above and AWS Lambda will log only one event in CloudWatch.
        logger.debug("log data from log4j debug \n this is continuation of log4j debug");

        logger.error("log data from log4j err. \n this is a continuation of log4j.err");

        // When logging in JSON, you can also add custom fields
        // In java11+ you can use the `try (var ctx = put("name", name)) {}` structure
        Instance ctx = put("name", name);
        logger.info("log line with input name");
        ctx.close();

        // Return will include the log stream name so you can look
        // up the log later.
        return String.format("Hello %s. log stream = %s", name, context.getLogStreamName());
    }
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.