Integrate Spring Boot with CloudWatch logs

If you run your Spring Boot application using Beanstalk, you have a few steps to do in order to save the application logs in S3. But if you don’t like this option or if you want to have a better (and more expensive) way to search into logs, we want to present you another approach: a library that is going to help on batching your application logs into AWS CloudWatch.

This library is available in Github (here) and it comes with a very good documentation. Basically, it gives you anything you need to configure logs pushing from the logback.xml file, without having to create any AWS client, without any class annotation and so on.

Also, it is very easy to configure and we liked especially the fact you can set in the log file the AWS region, so it’s not your job to create the AWS CloudWatch client and so on. Yes, we know, we always advise you to do that in order to control all configuration parameters, but in this case, it’s not something so important.

The other parameters you should configure are batch size and time intervals between 2 calls, but nothing complicate. All of them are very intuitive.

As you know we are used to doing, we offer you a simple application to show that. Implementation is available here.

After you have cloned the repository, check the README files and there you’ll find steps to run it. The AWS credentials are sent using Java system properties, but all options offered DefaultAWSCredentialsProviderChain works.

To test things, simply make one call:

curl -X GET http://localhost:8080/health
Status:OK

You’ll see in the console 2 records:

20190504T205118 http-nio-8080-exec-5 INFO c.c.c.CirrusCloudDemoController GET /health request.
20190504T205118 http-nio-8080-exec-5 WARN c.c.c.CirrusCloudDemoController GET /health request.

But in the AWS CloudWatch, you’ll see one single log entry, for WARN event:

Hope this post is useful for you!

Happy cloud computing!