All Projects → jlib-framework → jlib-awslambda-logback

jlib-framework / jlib-awslambda-logback

Licence: Apache-2.0 license
jlib AWS Lambda SLF4J/Logback Appender

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to jlib-awslambda-logback

logging-interceptor
CDI interceptor for logging to slf4j
Stars: ✭ 25 (+56.25%)
Mutual labels:  slf4j, mdc
slack-webhook-appender
Logback appender which posts logs to slack via incoming webhook.
Stars: ✭ 16 (+0%)
Mutual labels:  slf4j, logback-appender
logback-gelf-appender
Logback appender that sends GELF messages
Stars: ✭ 38 (+137.5%)
Mutual labels:  logback-appender
preact-mdc
material design components for preact using material-components-web sass styles (for live demo click the link below)
Stars: ✭ 23 (+43.75%)
Mutual labels:  mdc
slf4j-fluent
A fluent api for SLF4J
Stars: ✭ 19 (+18.75%)
Mutual labels:  slf4j
cwlog
🐾 cli AWS Cloudwatch Logs Downloader
Stars: ✭ 26 (+62.5%)
Mutual labels:  cloudwatch-logs
pino-cloudwatch
AWS CloudWatch Logs transport for pino
Stars: ✭ 35 (+118.75%)
Mutual labels:  cloudwatch-logs
logback-webhook-appender
Logback Webhook Appender
Stars: ✭ 15 (-6.25%)
Mutual labels:  logback-appender
liquibase-slf4j
Liquibase SLF4J Logger.
Stars: ✭ 42 (+162.5%)
Mutual labels:  slf4j
herald
Log annotation for logging frameworks
Stars: ✭ 71 (+343.75%)
Mutual labels:  slf4j
terraform-aws-cloudwatch-flow-logs
Terraform module for enabling flow logs for vpc and subnets.
Stars: ✭ 32 (+100%)
Mutual labels:  cloudwatch-logs
UMS
User management scaffolding, integration: User password login, mobile login, one-click login, OAuth2 login(Based on JustAuth), jwt, validate code(image, sms, sliderCode), RBAC, Support multi-tenancy, SLF4J-MDC, sign etc...
Stars: ✭ 24 (+50%)
Mutual labels:  mdc
akka-contextual-actor
A really small library (just a few classes) which lets you trace your actors messages transparently propagating a common context together with your messages and adding the specified values to the MDC of the underlying logging framework.
Stars: ✭ 17 (+6.25%)
Mutual labels:  mdc
slf4j-timber
SLF4J binding for Jake Wharton's Timber Android logging library
Stars: ✭ 44 (+175%)
Mutual labels:  slf4j
logunit
A Java library for unit-testing logging.
Stars: ✭ 40 (+150%)
Mutual labels:  slf4j
BOBBIN
Revolutionary high-performance Groovy/Java Slf4j logger
Stars: ✭ 17 (+6.25%)
Mutual labels:  slf4j
slf4j-lambda
slf4j with Java 8 lambda
Stars: ✭ 23 (+43.75%)
Mutual labels:  slf4j
cwlogs-writable
Writable stream for AWS CloudWatch Logs
Stars: ✭ 18 (+12.5%)
Mutual labels:  cloudwatch-logs
jcabi-log
Static Wrapper of SLF4J easing you from the necessity to create static LOGGER instances in each Java class
Stars: ✭ 53 (+231.25%)
Mutual labels:  slf4j
terraform-aws-kinesis-firehose
This code creates a Kinesis Firehose in AWS to send CloudWatch log data to S3.
Stars: ✭ 25 (+56.25%)
Mutual labels:  cloudwatch-logs

jlib AWS Lambda SLF4J/Logback Appender

Build Status Maven Central Javadoc

The jlib AWS Lambda Logback appender library allows to log through SLF4J/Logback to CloudWatch Logs from AWS Lambda code.

Usage

Dependency
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)
<dependencies>
    <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>
</dependencies>
Configuration
XML (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}] &lt;%-36X{AWSRequestId:-request-id-not-set-by-lambda-runtime}&gt; %-5level %logger{10} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="awslambda" />
    </root>

</configuration>
Code

To log information from your Lambda application, just get the logger for your class and output the message:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...

public class MyHandler {

    private static final Logger log = LoggerFactory.getLogger(MyHandler.class);
     
    public void handle(Context context) {
        log.info("My lambda is called '{}'.", context.getFunctionName());
        ...
    }
}

Features

Clean Multi-Line Logging to CloudWatch Logs

The library handles stacktraces and other messages spanning across multiple lines. Each multi-line message is treated as a single CloudWatch Log event.

If you were instead writing a multi-line text to the standard output, CloudWatch Logs would register every line of this text as a separate event.

To ensure a single event per multi-line message, this library does not write to the standard output. It uses the LambdaLogger provided by the AWS Lambda SDK, and CloudWatch Logs treats the whole multi-line message as a single event. Consequently, the developer does not need to handle newline characters, e.g. by replacing them by carriage return characters.

Request Tracing

When dealing with multiple requests, it can be hard to identify to which request a specific response belongs. This library helps to trace requests by allowing to include a unique request id to every single log message. The AWSRequestId is provided by the AWS Lambda runtime. Simply include an MDC reference to this id into the encoder pattern, and the id will be added to every single log message. The encoder pattern can be specified in the Logback configuration, e.g. logback.xml. Please refer to the Logback documentation for details on how to use the MDC.

Faster Deployment and Cold Starts

One goal when building Lambda applications should be to keep the application archive as small as possible. This allows for a faster deployment of the application when uploading its archive to AWS. It also speeds up the initial loading of the application, also known as cold start.

Up to 700kB can be saved when using this library. When depending on logback-classic in Maven or Gradle, the dependency tree includes a transitive dependency to com.sun.mail:javax.mail. When building an archive for the Lambda application, this transitive dependency is included in the archive. It raises the archive size by around 700kB. This happens whether the application is packaged as an uber-jar or as a zip archive.

This library excludes this transitive dependency in order to minimize the archive size of the Lambda application.

Alternative approaches using other Logging implementations for SLF4J produce archives at least about 100kB larger than the archive including this library.

Simple Build for Uber Jar

Some logging implementations for SLF4J require additional handling during the build process when creating an uber-jar. For instance, log4j2 requires the maven-shade-plugin.log4j2-cachefile-transformer to be executed while producing the archive.

This library does not require further configuration. Just add the dependency.

Disclaimer

“Amazon Web Services", "AWS", "Lambda" and "CloudWatch" are trademarks of Amazon.com, Inc. or its affiliates in the United States and/or other countries.

License

Copyright 2019 Igor Akkerman

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].