Reputation: 81
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
Reputation: 12662
pros: detailed Report Log
cons: only one level of log. Less configurable than 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
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
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
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
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
Reputation: 1744
What I understood from Aws Lambda Java Logging and searching around net is
Log4J
LambdaLogger
I hope that it helps!
Upvotes: 7
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