Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in AWS Lambda with slf4j

Tags:

I am using a lambda function and writing it in Java. I was looking up logging for Lambda functions when I read the docs and they support log4j - http://docs.aws.amazon.com/lambda/latest/dg/java-logging.html#java-wt-logging-using-log4j.

I was wondering if we could use logging using the Slf4j annotation as well since Slf4j is only a binding annotation. Has anybody tried using Slf4j before with lambda?

like image 264
chrisrhyno2003 Avatar asked Sep 06 '17 16:09

chrisrhyno2003


People also ask

Does AWS Lambda use Log4j?

AWS Lambda does not include Log4j2 in its managed runtimes or base container images. These are therefore not affected by the issue described in CVE-2021-44228 and CVE-2021-45046.

How do I log messages in AWS Lambda?

Using the CloudWatch console You can use the Amazon CloudWatch console to view logs for all Lambda function invocations. Open the Log groups page on the CloudWatch console. Choose the log group for your function (/aws/lambda/ your-function-name ). Choose a log stream.


1 Answers

Yes, you can. Just add the following dependencies to your project:

    <dependency>         <groupId>org.slf4j</groupId>         <artifactId>jcl-over-slf4j</artifactId>         <version>1.7.25</version>     </dependency>     <dependency>         <groupId>org.slf4j</groupId>         <artifactId>slf4j-log4j12</artifactId>         <version>1.7.25</version>     </dependency>     <dependency>         <groupId>com.amazonaws</groupId>         <artifactId>aws-lambda-java-log4j</artifactId>         <version>1.0.0</version>     </dependency> 

and create correct log4j.properties in /src/main/resources/ of your project, e.g.

log = . log4j.rootLogger = DEBUG, LAMBDA  # Define the LAMBDA appender log4j.appender.LAMBDA=com.amazonaws.services.lambda.runtime.log4j.LambdaAppender log4j.appender.LAMBDA.layout=org.apache.log4j.PatternLayout log4j.appender.LAMBDA.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} <%X{AWSRequestId}> %-5p %c{1}:%m%n 
like image 193
foal Avatar answered Sep 20 '22 23:09

foal