Mayur Dabhi
Mayur Dabhi

Reputation: 81

Recommendation for Logging data in AWS Lambda

AWS lambda currently supports two ways for logging, when Lambda function written in Java.

I want to know if is their any advantage of using one logging scheme over other.

Thanks!

Upvotes: 8

Views: 8722

Answers (5)

Raymond Chenon
Raymond Chenon

Reputation: 12662

LambdaLogger

pros: detailed Report Log

  • RequestId – The unique request ID for the invocation.
  • Duration – The amount of time that your function's handler method spent processing the event.
  • Billed Duration – The amount of time billed for the invocation. – For traced requests (XRAY TraceId, SegmentId ... ).
  • more ...

cons: only one level of log. Less configurable than Log4J

Log4J

pros: facade library to configure your log level (info, debug, error...).

cons: Cannot add detail report log (request ID) to your function's logs

Best of both worlds

You can have the best of both world by aws-lambda-java-log4j2 library https://docs.aws.amazon.com/lambda/latest/dg/java-logging.html

Configuration to run both Log4j and LambdaLogger

In your src/main/resources/log4j2.xml file

<Configuration status="WARN">
  <Appenders>
    <Lambda name="Lambda">
      <PatternLayout>
          <pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1} - %m%n</pattern>
      </PatternLayout>
    </Lambda>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="Lambda"/>
    </Root>
    <Logger name="software.amazon.awssdk" level="WARN" />
    <Logger name="software.amazon.awssdk.request" level="DEBUG" />
  </Loggers>
</Configuration>

In your build.gradle

dependencies {
    // ...
    runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.13.0'
    runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.2.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
}

Upvotes: 5

wz366
wz366

Reputation: 2930

You can actually use log4j with custom lambda appender, so that your logs can go to AWS Lambda CloudWatch. Refer to https://github.com/aws/aws-lambda-java-libs/tree/master/aws-lambda-java-log4j2.

Upvotes: 0

Igor Akkerman
Igor Akkerman

Reputation: 2478

You might want to use a popular logging framework like SLF4J, together with its most native implementation, Logback.

The jlib AWS Lambda Logback Appender allows you to do so in your AWS Lambda functions.

Upvotes: 1

user1321759
user1321759

Reputation: 1744

What I understood from Aws Lambda Java Logging and searching around net is

  • Log4J

    • pros: you can customize logs(using log4j.properties).
    • cons: it increase the size of your jar/zip as you add more dependency to your project
  • LambdaLogger

    • pros: it saves memory as it is already embeded on your dependencies
    • cons: it can't be customizable

I hope that it helps!

Upvotes: 7

hungrytolearn
hungrytolearn

Reputation: 41

In my opinion to keep it simple.

1. Assume you have an application project in maven where there will be multiple maven modules. You won't be passing context.getLogger() OR context argument every time you call some other module's implementation.

2. If you have a single handler function with no modules and multiple classes. In this case, you can start your AWS Lambda handler function code and get the context.getLogger() to get Lambda logger.

For more info : http://docs.aws.amazon.com/lambda/latest/dg/programming-model-v2.html

Upvotes: 3

Related Questions