bot-by / slf4j-aws-lambda

An SLF4J Logger implementation for AWS Lambda and CloudWatch

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SLF4J for AWS Lambda

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

About

An SLF4J Logger implementation for AWS Lambda and CloudWatch

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

License:Apache License 2.0


Languages

Language:Java 99.7%Language:CSS 0.3%