How to Configure Amazon CloudWatch in Node Application.

What is the use of CloudWatch?

Amazon CloudWatch is a monitoring and management service built for developers, system operators, site reliability engineers (SRE), and IT managers. CloudWatch provides you with data and actionable insights to monitor your applications, understand and respond to system-wide performance changes, optimize resource utilization, and get a unified view of operational health. CloudWatch collects monitoring and operational data in the form of logs, metrics, and events, providing you with a unified view of AWS resources, applications and services that run on AWS, and on-premises servers.

Read More About Amazon CloudWatch

For CloudWatch configuration we need to follow simple Steps.

Step 1: Login in AWS Console
Step 2: Going To Service Page and Select CloudWatch

1

Step 3: Create a Log Group. Select “Logs” From Left Menu and Click on Action.

2

Step 4: Set Log Group Name

3

Step 5: After Log group creation. We need to add following code in our Node.Js Application.

Prerequisite:
Install ‘winston’ and ‘winston-aws-cloudwatch’ in current application

const winston = require('winston');
const cloudWatchTransport = require('winston-aws-cloudwatch');
winston.loggers.add('error-log', {
transports: [
new winston.transports.Console({
json: true,
colorize: true,
level: 'info'
}),
(new cloudWatchTransport({
logGroupName: 'demo',
logStreamName: 'log-' + new Date().toISOString().split('T')[0],
createLogGroup: true,
createLogStream: true,
submissionInterval: 2000,
submissionRetryCount: 1,
batchSize: 20,
awsConfig: {
region: 'us-east-2',
accessKeyId: 'B….',
secretAccessKey: 'q..’
},
formatLog: function (item: any) {
return item.level + ': ' + item.message + ' ' + JSON.stringify(item.meta);
}
})
)
]
});

const logg = winston.loggers.get(‘error-log’);

Need to change following details

logGroupName= “Created Log Group”
awsConfig:
{
region = “Set Your Region Details”
accessKeyId: “Your Access Key Id”
secretAccessKey: “Your Secret Access Key Id”
}

Step 6: We can add the following code in our application as per uses.

logg.error({error: “Error”});
logg.info({success:"success"});

Step 7: For Log checking select your log from AWS console.

Now, Here we can see date wise log details. So click on a particular date.

4

Step 8: After click on the selected date, we can see all logs here for it.

5

Thanks for Reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.