The jlib AWS Lambda Logback Appender allows you to use SLF4J with Logback from your AWS Lambda functions.
Simply add these dependencies:
Gradle (build.gradle)
dependencies {
implementation 'org.slf4j:slf4j-api:1.8.0-beta2'
runtimeOnly 'org.jlib:jlib-awslambda-logback:1.0.0'
}
Maven (pom.xml)
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta2</version>
</dependency>
<dependency>
<groupId>org.jlib</groupId>
<artifactId>jlib-awslambda-logback</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
Then use the AwsLambdaAppender
in your logging configuration:
Example XML configuration (src/main/resources/logback.xml)
<configuration>
<appender name="awslambda" class="org.jlib.cloud.aws.lambda.logback.AwsLambdaAppender">
<encoder type="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] <%-36X{AWSRequestId:-request-id-not-set-by-lambda-runtime}>
%-5level %logger{10} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="awslambda" />
</root>
</configuration>
Unlike other solutions, this Logback Appender correctly handles multi-line log messages, especially stack traces, and produces only a single CloudWatch Logs event per message.
The library also allows you to include the AWSRequestId, provided by the AWS Lambda runtime, in every single log message for better tracing.
While log4j2 requires additional handling in the build when you create an uber-jar, this solution works out of the box.
Disclaimer: I'm the developer of jlib