Skip to content

Commit

Permalink
Use modular cloudwatch package instead of entire aws sdk. (#172)
Browse files Browse the repository at this point in the history
* Use modular cloudwatch package instead of entire aws sdk.

* err.code to err.name
  • Loading branch information
peakyDicers authored Mar 16, 2022
1 parent 2688ff6 commit e705a18
Show file tree
Hide file tree
Showing 8 changed files with 4,756 additions and 3,970 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,13 @@ This is the list of options you could pass as argument to `winston.add`:
* level - defaults to `info`
* logGroupName - `string` or `function`
* logStreamName - `string` or `function`
* cloudWatchLogs - `AWS.CloudWatchLogs` instance, used to set custom AWS instance. aws* and proxyServer options do not get used if this is set.
* cloudWatchLogs - `AWS.CloudWatchLogs` instance, used to set custom AWS instance.
* awsAccessKeyId
* awsSecretKey
* awsRegion
* awsOptions - `object`, params as per [docs](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatchLogs.html#constructor-property), values in `awsOptions` are overridden by any other if specified, run [this example](https://github.com/lazywithclass/winston-cloudwatch/blob/master/examples/simple-with-aws-options.js) to have a look
* jsonMessage - `boolean`, format the message as JSON
* messageFormatter - `function`, format the message the way you like. This function will receive a `log` object that has the following properties: `level`, `message`, and `meta`, which are passed by winston to the `log` function (see [CustomLogger.prototype.log as an example](https://github.com/winstonjs/winston#adding-custom-transports))
* proxyServer - `String`, use `proxyServer` as proxy in httpOptions
* uploadRate - `Number`, how often logs have to be sent to AWS. Be careful of not hitting [AWS CloudWatch Logs limits](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html), the default is 2000ms.
* errorHandler - `function`, invoked with an error object, if not provided the error is sent to `console.error`
* retentionInDays - `Number`, defaults to `0`, if set to one of the possible values `1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653` the retention policy on the log group written will be set to the value provided.
Expand Down
13 changes: 2 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var util = require('util'),
winston = require('winston'),
AWS = require('aws-sdk'),
{ CloudWatchLogs } = require('@aws-sdk/client-cloudwatch-logs'),
cloudWatchIntegration = require('./lib/cloudwatch-integration'),
isEmpty = require('lodash.isempty'),
assign = require('lodash.assign'),
Expand All @@ -27,22 +27,13 @@ var WinstonCloudWatch = function(options) {
return [ log.level, log.message ].join(' - ')
};
this.formatMessage = options.jsonMessage ? stringify : messageFormatter;
this.proxyServer = options.proxyServer;
this.uploadRate = options.uploadRate || 2000;
this.logEvents = [];
this.errorHandler = options.errorHandler;

if (options.cloudWatchLogs) {
this.cloudwatchlogs = options.cloudWatchLogs;
} else {
if (this.proxyServer) {
AWS.config.update({
httpOptions: {
agent: require('proxy-agent')(this.proxyServer)
}
});
}

var config = {};

if (awsAccessKeyId && awsSecretKey && awsRegion) {
Expand All @@ -58,7 +49,7 @@ var WinstonCloudWatch = function(options) {
config = assign(config, options.awsOptions);
}

this.cloudwatchlogs = new AWS.CloudWatchLogs(config);
this.cloudwatchlogs = new CloudWatchLogs(config);
}

debug('constructor finished');
Expand Down
10 changes: 5 additions & 5 deletions lib/cloudwatch-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ lib.upload = function(aws, groupName, streamName, logEvents, retentionInDays, op
// InvalidSequenceToken means we need to do a describe to get another token
// also do the same if ResourceNotFound as that will result in the last token
// for the group being set to null
if (err.code === 'InvalidSequenceTokenException' || err.code === 'ResourceNotFoundException') {
debug(err.code + ', retrying', true);
if (err.name === 'InvalidSequenceTokenException' || err.name === 'ResourceNotFoundException') {
debug(err.name + ', retrying', true);
lib.submitWithAnotherToken(aws, groupName, streamName, payload, retentionInDays, options, cb)
} else {
debug('error during putLogEvents', err, true)
Expand Down Expand Up @@ -162,7 +162,7 @@ lib.ensureGroupPresent = function ensureGroupPresent(aws, name, retentionInDays,
var params = { logGroupName: name };
aws.describeLogStreams(params, function(err, data) {
// TODO we should cb(err, false) if there's an error?
if (err && err.code == 'ResourceNotFoundException') {
if (err && err.name == 'ResourceNotFoundException') {
debug('create group');
return aws.createLogGroup(params, lib.ignoreInProgress(function(err) {
if(!err) lib.putRetentionPolicy(aws, name, retentionInDays);
Expand Down Expand Up @@ -219,8 +219,8 @@ lib.getStream = function getStream(aws, groupName, streamName, cb) {

lib.ignoreInProgress = function ignoreInProgress(cb) {
return function(err, data) {
if (err && (err.code == 'OperationAbortedException' ||
err.code == 'ResourceAlreadyExistsException')) {
if (err && (err.name == 'OperationAbortedException' ||
err.name == 'ResourceAlreadyExistsException')) {
debug('ignore operation in progress', err.message);
cb(null, data);
} else {
Expand Down
Loading

0 comments on commit e705a18

Please sign in to comment.