How to use the CloudWatchLogs function from aws-sdk

Find comprehensive JavaScript aws-sdk.CloudWatchLogs code examples handpicked from public code repositorys.

The aws-sdk.CloudWatchLogs module provides methods for interacting with Amazon CloudWatch Logs service in Node.js.

636
637
638
639
640
641
642
643
644
645
  }));
} else if (!body.logs.length) {
  return response(200, 'application/json', JSON.stringify({}));
}

const cloudWatchClient = new AWS.CloudWatchLogs({ apiVersion: '2014-03-28' });
const putLogEventsInput = {
  logGroupName,
  logStreamName: createLogStreamName(body.meetingId, body.attendeeId),
  logEvents: createLogEvents(body.logs, body.meetingId, body.attendeeId)
fork icon450
star icon638
watch icon0

+ 11 other calls in file

6
7
8
9
10
11
12
13
14
15
        originalWorkingDir = process.cwd();

module.exports = function destroyObjects(newObjects) {
        'use strict';
        const lambda = new aws.Lambda({ region: awsRegion }),
                logs = new aws.CloudWatchLogs({ region: awsRegion }),
                apiGatewayPromise = retriableWrap(new aws.APIGateway({ region: awsRegion })),
                s3 = new aws.S3(),
                iot = new aws.Iot({ region: awsRegion }),
                sns = new aws.SNS({ region: awsRegion }),
fork icon290
star icon0
watch icon3

How does aws-sdk.CloudWatchLogs work?

The aws-sdk.CloudWatchLogs module provides a Node.js SDK for interacting with the Amazon CloudWatch Logs service. Using this module, developers can create, read, update, and delete CloudWatch Logs resources such as log groups, log streams, and log events. The module provides a number of classes and methods for performing these operations, including: CloudWatchLogs: the main class for interacting with the CloudWatch Logs service, which provides methods for creating and managing log groups, log streams, and log events. FilterLogEventsRequest: a request object used to filter log events in a log group or log stream based on a set of criteria such as a time range, a search string, or a specific field. GetLogEventsRequest: a request object used to retrieve log events from a specific log stream or group based on a time range or a set of sequence token markers. In addition to these classes and methods, the aws-sdk.CloudWatchLogs module also provides a number of events that can be used to monitor and respond to changes in log groups and streams. Overall, the aws-sdk.CloudWatchLogs module provides a powerful and flexible way to work with Amazon CloudWatch Logs in Node.js, enabling developers to integrate log data into their applications and infrastructure.

29
30
31
32
33
34
35
36
37
38
    cli.getUsage();
}

async function setLogGroupRetention() {
    await awsConfigHelper.updateConfig(cliArgs.region);
    const cloudwatchLogs = new AWS.CloudWatchLogs();

    let isCompleted = false;
    let nextToken = undefined;
    while (!isCompleted) {
fork icon95
star icon94
watch icon14

98
99
100
101
102
103
104
105
106
107
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create the CloudWatchLogs service object
var cwl = new AWS.CloudWatchLogs({apiVersion: '2014-03-28'});

var params = {
  destinationArn: 'LAMBDA_FUNCTION_ARN',
  filterName: 'FILTER_NAME',
fork icon94
star icon107
watch icon29

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const AWS = require("aws-sdk");
const cloudwatchlogs = new AWS.CloudWatchLogs({ region: "us-west-2" });

const logGroupName = "example-log-group";
const logStreamName = "example-log-stream";

// Create a new log group
cloudwatchlogs.createLogGroup({ logGroupName }, (err, data) => {
  if (err) console.log(err, err.stack);
  else console.log("Created log group:", data.logGroupName);

  // Create a new log stream in the log group
  cloudwatchlogs.createLogStream(
    { logGroupName, logStreamName },
    (err, data) => {
      if (err) console.log(err, err.stack);
      else console.log("Created log stream:", data.logStreamName);

      // Put some log events into the log stream
      cloudwatchlogs.putLogEvents(
        {
          logGroupName,
          logStreamName,
          logEvents: [
            { message: "Event 1" },
            { message: "Event 2" },
            { message: "Event 3" },
          ],
        },
        (err, data) => {
          if (err) console.log(err, err.stack);
          else console.log("Put log events:", data);

          // Retrieve the most recent log events from the log stream
          cloudwatchlogs.getLogEvents(
            { logGroupName, logStreamName },
            (err, data) => {
              if (err) console.log(err, err.stack);
              else console.log("Retrieved log events:", data.events);
            }
          );
        }
      );
    }
  );
});

In this example, we're using the aws-sdk.CloudWatchLogs module to create a new log group, log stream, and log events, and then retrieve the most recent log events from the stream. We first create a new instance of the CloudWatchLogs class and specify the region where our CloudWatch Logs service is running. We then define a log group name and log stream name that we want to create and work with. We use the createLogGroup method to create a new log group, passing in the log group name as a parameter. We then use the createLogStream method to create a new log stream within the log group, passing in the log stream name and log group name as parameters. Next, we use the putLogEvents method to put some log events into the log stream. We pass in the log group name, log stream name, and an array of log events as parameters. Finally, we use the getLogEvents method to retrieve the most recent log events from the log stream. We pass in the log group name and log stream name as parameters, and the method returns an array of log events. When we run this code, it will output the following logs: yaml Copy code

18
19
20
21
22
23
24
25
26
27
  this._logGroupName = logGroupName
  this._logStreamName = logStreamName
  this._options = Object.assign({}, DEFAULT_OPTIONS, options)
  this._formatter = new CloudWatchEventFormatter(this._options)
  this._sequenceToken = null
  this._client = new AWS.CloudWatchLogs(this._options.awsConfig)
  this._initializing = null
}

submit (batch) {
fork icon11
star icon46
watch icon4

4
5
6
7
8
9
10
11
12
13
// get stackName, projectTag from our config file
const config = yaml.safeLoad(fs.readFileSync('config/config.yml').toString())
const stackName = config.default.stackName
const projectTag = config.default.projectTag

const cw = new AWS.CloudWatchLogs()

// helper
function tagWithProject (logGroup) {
  console.log(`tagging ${logGroup.logGroupName} with { Project: ${projectTag} }`)
fork icon7
star icon33
watch icon32

85
86
87
88
89
90
91
92
93
94

/**
 * find the latest log stream name, if the log group does not exist, return null.
 */
const getLatestLogStreamName = async (logGroupName) => {
  const client = new AWS.CloudWatchLogs();
  try {
    const response = await client
      .describeLogStreams({
        logGroupName,
fork icon0
star icon0
watch icon1

+ 7 other calls in file

75
76
77
78
79
80
81
82
83
84
85


function getLogsGroupsTags(logGroupName, tempCreds, serviceData) {
  if(serviceData){
  	tempCreds.region = serviceData.deployment_accounts[0].region;
  }
  var cloudwatchlogs = new AWS.CloudWatchLogs(tempCreds);
  var params = {
    logGroupName: logGroupName /* required */
  };
  return new Promise((resolve, reject) => {
fork icon0
star icon0
watch icon1

+ 12 other calls in file

0
1
2
3
4
5
6
7
8
9
10
11
12
13
const fs    = require('fs');
const AWS   = require('aws-sdk');


AWS.config.loadFromPath('./config.json');


const cloudwatchlogs 	= new AWS.CloudWatchLogs({apiVersion: '2014-03-28'});
const s3    = new AWS.S3();




var linux_data 		= null;
fork icon0
star icon0
watch icon1

+ 21 other calls in file

0
1
2
3
4
5
6
7
8
9
10
11
12
const fs = require("fs");
const AWS = require("aws-sdk");


AWS.config.loadFromPath("./config.json");


const cloudwatchlogs = new AWS.CloudWatchLogs({ apiVersion: "2014-03-28" });
const s3 = new AWS.S3();


var linux_data = null;
var windows_data = null;
fork icon0
star icon0
watch icon1

+ 4 other calls in file

304
305
306
307
308
309
310
311
312
313
  response.body = "Empty Parameters Received";
  response.statusCode = 400;
  return response;
}
const logStreamName = `ChimeReactSDKMeeting_${body.timestamp}`;
const cloudWatchClient = new AWS.CloudWatchLogs({
  apiVersion: '2014-03-28'
});
const putLogEventsInput = {
  "logGroupName": logGroupName,
fork icon0
star icon0
watch icon0

231
232
233
234
235
236
237
238
239
		httpOptions: { timeout: 10000 },
	};
	if (this.region) {
		clientOpt.region = this.region;
	}
	const cloudwatchlogs = new AWS.CloudWatchLogs(clientOpt);
	const result = await cloudwatchlogs.describeMetricFilters(params).promise();
	return _.get(result, "metricFilters[0]");
}
fork icon0
star icon0
watch icon0