Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log to an explicit AWS CloudWatch log stream and change it programmatically (Java/Scala/log4j)

I have a daemon in Scala running on EC2 that processes jobs. For each job I know a (possibly) different log stream I'd like to append messages to.

How can I make a log-stream specific Appender that I can attach to my log4j logger, and change it when my listener moves on to the next job?

like image 537
Scott Smith Avatar asked Aug 31 '16 22:08

Scott Smith


People also ask

Can CloudWatch logs be modified?

As you probably already know,Cloud-watch helps to record the logs of all aws services. Cloud-watch Log streams helps to listen the event automatically without any triggers. Using this Cloudwatch Log streams we can able to edit the cloud-watch logs.

How do I automate CloudWatch logs?

To send action output to CloudWatch Logs (console)Open the AWS Systems Manager console at https://console.aws.amazon.com/systems-manager/ . In the navigation pane, choose Automation. Choose the Preferences tab, and then choose Edit. Select the check box next to Send output to CloudWatch Logs.

How do I access my CloudWatch log stream?

Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/ . In the navigation pane, choose Log groups. For Log Groups, choose the log group to view the streams. In the list of log groups, choose the name of the log group that you want to view.

Does AWS CloudWatch use Log4j?

Log4j Cloudwatch Appender is a simple to use Log4j Appender that allows you to stream your application logs directly into your AWS Cloudwatch Logs. In order to optimise network usage and mitigate throttling issues with AWS Cloudwatch, the actual sending of logs may be deferred.


1 Answers

This type of integration is blended of different things like AWS Java API, Log4J API, and AWS Cloud Watch configurations.

You can achieve that by implementing a customized log4j appender. An example you may leverage from github is the following link.

The idea is to add the log4j appender class to your source project and use it with your log4j library. The appender will use AWS CloudWatch API to append the logs for a specific log stream and log group that you need to create on AWS account.

Example: CloudwatchAppender.java

Every log your program creates will send a cloud watch event.

like image 151
Rafael Gorski Avatar answered Oct 14 '22 14:10

Rafael Gorski