GithubHelp home page GithubHelp logo

slf4j-aws-lambda's Introduction

SLF4J for AWS Lambda

Important

The project was moved to vitalijr2/aws-lambda-slf4j on 18-02-2024.

An SLF4J Logger implementation for AWS Lambda and CloudWatch.

Codacy Grade Codacy Coverage Java Version

Logger Release Javadoc
slf4j-aws-lambda-logger Maven Central Javadoc
slf4j-aws-lambda-json-logger Maven Central Javadoc

Table of Contents

Created by gh-md-toc

Getting started

Yet another SLF4J Simple, isn't it?

No, it isn't.

This implementation supports MDC to print out AWS request ID in start of every logging record and supports Markers too. And the killer feature: it solves the CRLF issue described by Frank Afriat in Solving the Java Aws Lambda logging problem - you don't have to prepare logging messages and stacktraces to log them on CloudWatch Logs.

The footprint of slf4j-aws-lambda (88K) is same size as slf4j-simple (79K) and much smaller than logback (888K).

Usage

There is a great original manual.

The sample code, see the folder example-lambda or example-lambda-json:

@Override
public String handleRequest(Map<String, Object> input, Context context) {
  MDC.put("@aws-request-id@", context.getAwsRequestId());

  logger.trace("trace message");
  logger.debug("debug message");
  logger.info("info message");
  logger.warn("warning message");
  logger.error("error message");

  var marker = new BasicMarkerFactory().getMarker("important");

  Stream.of("\n", "\r\n", "\r").forEach(injection -> {
    logger.trace(marker, "CRLF{}injection", injection);
  });

  logger.warn("printable stacktrace", new Throwable("Printable Stacktrace Demo"));
  return "done";
}

The log with slf4j-aws-lambda-logger:

983f71e5-9091-443b-8c01-6668120c0e5d INFO uk.bot_by.slf4j_demo.BotHandler - info message
983f71e5-9091-443b-8c01-6668120c0e5d WARN uk.bot_by.slf4j_demo.BotHandler - warning message
983f71e5-9091-443b-8c01-6668120c0e5d ERROR uk.bot_by.slf4j_demo.BotHandler - error message
983f71e5-9091-443b-8c01-6668120c0e5d TRACE uk.bot_by.slf4j_demo.BotHandler - CRLF
injection
983f71e5-9091-443b-8c01-6668120c0e5d TRACE uk.bot_by.slf4j_demo.BotHandler - CRLF
injection
983f71e5-9091-443b-8c01-6668120c0e5d TRACE uk.bot_by.slf4j_demo.BotHandler - CRLF
injection
983f71e5-9091-443b-8c01-6668120c0e5d WARN uk.bot_by.slf4j_demo.BotHandler - printable stacktrace

There is a JSON option with slf4j-aws-lambda-json-logger:

{
    "level": "INFO",
    "logname": "uk.bot_by.slf4j_demo.BotHandler",
    "message": "info message",
    "aws-request-id": "7b9af47e-d861-44b4-bde7-fa2e84ffb7cf"
}
{
    "level": "WARN",
    "logname": "uk.bot_by.slf4j_demo.BotHandler",
    "message": "warning message",
    "aws-request-id": "7b9af47e-d861-44b4-bde7-fa2e84ffb7cf"
}
{
    "level": "ERROR",
    "logname": "uk.bot_by.slf4j_demo.BotHandler",
    "message": "error message",
    "aws-request-id": "7b9af47e-d861-44b4-bde7-fa2e84ffb7cf"
}
{
    "level": "TRACE",
    "logname": "uk.bot_by.slf4j_demo.BotHandler",
    "message": "CRLF\ninjection",
    "aws-request-id": "7b9af47e-d861-44b4-bde7-fa2e84ffb7cf"
}
{
    "level": "TRACE",
    "logname": "uk.bot_by.slf4j_demo.BotHandler",
    "message": "CRLF\r\ninjection",
    "aws-request-id": "7b9af47e-d861-44b4-bde7-fa2e84ffb7cf"
}
{
    "level": "TRACE",
    "logname": "uk.bot_by.slf4j_demo.BotHandler",
    "message": "CRLF\rinjection",
    "aws-request-id": "7b9af47e-d861-44b4-bde7-fa2e84ffb7cf"
}
{
    "stack-trace": "java.lang.Throwable: Printable Stacktrace Demo\n\tat uk.bot_by.slf4j_demo.BotHandler.handleRequest(BotHandler.java:36)\n\tat uk.bot_by.slf4j_demo.BotHandler.handleRequest(BotHandler.java:12)\n\tat lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:205)\n\tat lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:905)\n\tat lambdainternal.AWSLambda.startRuntime(AWSLambda.java:261)\n\tat lambdainternal.AWSLambda.startRuntime(AWSLambda.java:200)\n\tat lambdainternal.AWSLambda.main(AWSLambda.java:194)\n",
    "level": "WARN",
    "logname": "uk.bot_by.slf4j_demo.BotHandler",
    "message": "printable stacktrace",
    "aws-request-id": "7b9af47e-d861-44b4-bde7-fa2e84ffb7cf"
}

Configuration

The configuration is similar to SLF4J Simple.

It looks for the lambda-logger.properties resource and read properties:

  • dateTimeFormat - The date and time format to be used in the output messages. The pattern describing the date and time format is defined by SimpleDateFormat. If the format is not specified or is invalid, the number of milliseconds since start up will be output.
  • defaultLogLevel - Default log level for all instances of LambdaLogger. Must be one of (trace, debug, info, warn, error), a value is case-insensitive. If not specified, defaults to info.
  • levelInBrackets - Should the level string be output in brackets? Defaults to false.
  • log.a.b.c - Logging detail level for a LambdaLogger instance named a.b.c.
  • requestId - Set the context name of AWS request ID. Defaults to AWS_REQUEST_ID.
  • showDateTime - Set to true if you want the current date and time to be included in output messages. Defaults to false.
  • showLogName - Set to true if you want the Logger instance name to be included in output messages. Defaults to true.
  • showShortLogName - Set to true if you want the last component of the name to be included in output messages. Defaults to false.
  • showThreadId - If you would like to output the current thread id, then set to true. Defaults to false.
  • showThreadName - Set to true if you want to output the current thread name. Defaults to false.

The environment variables overrides the properties: LOG_AWS_REQUEST_ID, LOG_DATE_TIME_FORMAT, LOG_DEFAULT_LEVEL, LOG_LEVEL_IN_BRACKETS, LOG_SHOW_DATE_TIME, LOG_SHOW_NAME, LOG_SHOW_SHORT_NAME, LOG_SHOW_THREAD_ID, LOG_SHOW_THREAD_NAME.

More information you can find on site:

https://slf4j-aws-lambda.bot-by.uk/

See also how to setup a provider by a system property

Other solutions

Other AWS centric loggers are SLF4J/Logback Appender, slf4j-simple-lambda, Logback's CloudWatch appender and CloudWatchLogs Java appender.

Contributing

Please read Contributing.

History

See Changelog

Authors and acknowledgment

  • Replace the custom output to stdout with AWS LambdaLogger, the main idea from @igorakkerman's SLF4J/Logback Appender.

License

Copyright 2022-2024 Vitalij Berdinskih

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.

Apache License v2.0
License

slf4j-aws-lambda's People

Contributors

dependabot[bot] avatar vitalijr2 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

slf4j-aws-lambda's Issues

Binders are deprecated in SLF4J 2

Summary

SLF4J's FAQ says, see [What has changed in SLF4J version 2.0.0?|https://www.slf4j.org/faq.html#changesInVersion200]:
{quote}SLF4J 1.7.x and earlier versions relied on the static binder mechanism which is no loger honored by slf4j-api version 2.0.x.
{quote}
The static binder classes were removed in [remove org.slf4j.impl package - fixes SLF4J-407|https://github.com/qos-ch/slf4j/commit/6ac20c6a0ae96ea6609f24d92eb235d87d8a0f8b]

All *Binder interfaces were removed in qos-ch/slf4j fix SLF4J-555

Related issue SLF4J-555 and PR Binders are deprecated in SLF4J 2 #299

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.