How to send application logs to AWS SQS using log4j appender

Apisero
4 min readJan 29, 2021

--

Author: Priyanka Paul

Prerequisites:

  1. AWS SQS queue set up
  2. Log4j configuration
  3. Maven configuration

Enablement :

  • AWS Simple queue Service set up:

Step 1: Once you logged in AWS account, navigate to the IAM service from the AWS console and check

if any IAM user exists or create a new IAM user

Step 2: click on the user name and go to summary and then Security credentials and Create Access ID. which will create a new Aws- Access ID and secret. Copy both the value or download the CSV file.

Step 3: Now navigate to AWS SQL service and create a new queue. Refer to the below screenshots for new queue creation.

Log4j configuration:

Step 1: Go to the mule application and navigate to log4j.xml under src/main/resources and

configure SQS log4j appender. This appender pushes all the application logs to specified Amazon

SQS Queue.

Step 2: We need to add some package information to support the mule- AWS SQS integration.

Step 3: Also include appender reference in Loggers section and define the log level of AsyncRoot

Maven configuration:

For the above configuration, you need to add a maven dependency in POM.XML

Execution:

Refer to the sample application and complete log4j.xml file.

Refer to the sample Log4j.xml file. Here, you need to Provide the AWS Access ID ({sys:awsAccessKey}) and Secret key ({sys:awsSecretKey}) that received initially for an IAM user as runtime argument. Also, you need to specify a queueName and awsRegion.

Now while hitting the service the application logs will be sent to the specified Amazon SQS Queue.

Console logs :

Queue logs:

Step 1: Click to queue name and navigate to the send and receive the message.

Step 2: Scroll to the receive message section and click poll for message for getting new messages.

Log Message has 3 parts.

  • Details
  • Body
  • attributes

Details where you can check the AWS sender account ID, timestamp, size of log message.

Message body will be shown in Body section

Message attributes data will be shown in the Attribute section if present.

Conclusion:

This is a custom appender for log4j. This appender pushes all the application logs to the specified Amazon SQS Queue. This appender is very useful

  • When you may want to store the logs somewhere other than in CloudHub
  • You can also feed this Queue to any Log analyzers like Splunk, ELK

--

--

No responses yet