What is @aws-sdk/client-cloudwatch-logs?
The @aws-sdk/client-cloudwatch-logs npm package allows developers to interact with the Amazon CloudWatch Logs service. This service provides monitoring, storing, and accessing log files from Amazon EC2 instances, AWS CloudTrail, and other sources. The package enables programmatic access to all the features of CloudWatch Logs, such as creating and managing log groups and streams, sending log events, and querying log data.
What are @aws-sdk/client-cloudwatch-logs's main functionalities?
Creating a Log Group
This feature allows you to create a new log group in CloudWatch Logs. A log group is a collection of log streams that share the same retention, monitoring, and access control settings.
const { CloudWatchLogsClient, CreateLogGroupCommand } = require('@aws-sdk/client-cloudwatch-logs');
const client = new CloudWatchLogsClient({ region: 'us-west-2' });
const command = new CreateLogGroupCommand({ logGroupName: 'my-log-group' });
client.send(command);
Creating a Log Stream
This feature allows you to create a log stream within a log group. A log stream represents a sequence of log events that share the same source.
const { CloudWatchLogsClient, CreateLogStreamCommand } = require('@aws-sdk/client-cloudwatch-logs');
const client = new CloudWatchLogsClient({ region: 'us-west-2' });
const command = new CreateLogStreamCommand({ logGroupName: 'my-log-group', logStreamName: 'my-log-stream' });
client.send(command);
Putting Log Events
This feature allows you to send log data to a log stream in CloudWatch Logs. Each log event consists of a timestamp and a message.
const { CloudWatchLogsClient, PutLogEventsCommand } = require('@aws-sdk/client-cloudwatch-logs');
const client = new CloudWatchLogsClient({ region: 'us-west-2' });
const command = new PutLogEventsCommand({
logGroupName: 'my-log-group',
logStreamName: 'my-log-stream',
logEvents: [{ timestamp: Date.now(), message: 'This is a log message.' }]
});
client.send(command);
Searching and Filtering Log Data
This feature allows you to search and filter log data within a log group. You can specify a filter pattern, as well as a time range for the search.
const { CloudWatchLogsClient, FilterLogEventsCommand } = require('@aws-sdk/client-cloudwatch-logs');
const client = new CloudWatchLogsClient({ region: 'us-west-2' });
const command = new FilterLogEventsCommand({
logGroupName: 'my-log-group',
filterPattern: 'ERROR',
startTime: Date.now() - 3600000,
endTime: Date.now()
});
client.send(command);
Other packages similar to @aws-sdk/client-cloudwatch-logs
winston-cloudwatch
winston-cloudwatch is a transport for the popular logging library winston, which allows you to send your logs to Amazon CloudWatch. Unlike @aws-sdk/client-cloudwatch-logs, which is the official AWS SDK for JavaScript, winston-cloudwatch integrates with the winston logging framework and provides a more streamlined way to log directly from your application.
pino-cloudwatch
pino-cloudwatch is a module that streams pino logs to Amazon CloudWatch. Pino is a very low overhead Node.js logger. This package is similar to @aws-sdk/client-cloudwatch-logs in that it allows logging to CloudWatch, but it is specifically designed to work with the pino logging library and focuses on performance.