Skip to content

Work towards 4.0 #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/using-winston-transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var winston = require('winston'),
WinstonCloudWatch = require('../index');

function format(message, ...rest) {
// I am inventing a custom formatter
return `
${message}
${rest.map(r => ` * ${r}`).join('\n')}
`
}

function customFormatter({level, message, [Symbol.for('splat')]: args = []}) {
return `${level} - ${format(message, ...args)}`;
}

var transport = winston.createLogger({
exitOnError: false,
transports: [
new WinstonCloudWatch({
logGroupName: 'testing',
logStreamName: 'first',
awsRegion: 'us-east-1',
})],
format: winston.format.printf(customFormatter)
});

transport.info('some text', {foo: 'bar'}, new Error('wtf'));
260 changes: 128 additions & 132 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,150 +1,146 @@
'use strict';

var util = require('util'),
winston = require('winston'),
AWS = require('aws-sdk'),
cloudWatchIntegration = require('./lib/cloudwatch-integration'),
isEmpty = require('lodash.isempty'),
assign = require('lodash.assign'),
isError = require('lodash.iserror'),
stringify = require('./lib/utils').stringify,
debug = require('./lib/utils').debug,
defaultFlushTimeoutMs = 10000;

var WinstonCloudWatch = function(options) {
winston.Transport.call(this, options);
this.level = options.level || 'info';
this.name = options.name || 'CloudWatch';
this.logGroupName = options.logGroupName;
this.retentionInDays = options.retentionInDays || 0;
this.logStreamName = options.logStreamName;

var awsAccessKeyId = options.awsAccessKeyId;
var awsSecretKey = options.awsSecretKey;
var awsRegion = options.awsRegion;
var messageFormatter = options.messageFormatter ? options.messageFormatter : function(log) {
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 = {};
const winston = require('winston'),
Transport = require('winston-transport'),
AWS = require('aws-sdk'),
{ LEVEL, MESSAGE } = require('triple-beam'),
cloudWatchIntegration = require('./lib/cloudwatch-integration'),
isEmpty = require('lodash.isempty'),
assign = require('lodash.assign'),
isError = require('lodash.iserror'),
stringify = require('./lib/utils').stringify,
debug = require('./lib/utils').debug

const defaultFlushTimeoutMs = 10000


module.exports = class WinstonCloudwatch extends Transport {
constructor(opts) {
super(opts)
this.setOptions(opts)
debug('constructor finished')
}

if (awsAccessKeyId && awsSecretKey && awsRegion) {
config = { accessKeyId: awsAccessKeyId, secretAccessKey: awsSecretKey, region: awsRegion };
} else if (awsRegion && !awsAccessKeyId && !awsSecretKey) {
// Amazon SDK will automatically pull access credentials
// from IAM Role when running on EC2 but region still
// needs to be configured
config = { region: awsRegion };
log(info, callback) {
debug('log (called by winston)', info);
if (!isEmpty(info.message) || isError(info.message)) {
this.add(info);
}

if (options.awsOptions) {
config = assign(config, options.awsOptions);
if (!/^uncaughtException: /.test(info.message)) {
// do not wait, just return right away
return callback(null, true);
}

this.cloudwatchlogs = new AWS.CloudWatchLogs(config);
}
debug('message not empty, proceeding')

debug('constructor finished');
};

util.inherits(WinstonCloudWatch, winston.Transport);

WinstonCloudWatch.prototype.log = function (info, callback) {
debug('log (called by winston)', info);

if (!isEmpty(info.message) || isError(info.message)) {
this.add(info);
// clear interval and send logs immediately
// as Winston is about to end the process
clearInterval(this.intervalId);
this.intervalId = null;
this.submit(callback);
}

if (!/^uncaughtException: /.test(info.message)) {
// do not wait, just return right away
return callback(null, true);
}
add(log) {
debug('add log to queue', log);

debug('message not empty, proceeding')
if (!isEmpty(log.message) || isError(log.message)) {
this.logEvents.push({
message: this.formatMessage(log),
timestamp: new Date().getTime()
});
}

// clear interval and send logs immediately
// as Winston is about to end the process
clearInterval(this.intervalId);
this.intervalId = null;
this.submit(callback);
};
if (!this.intervalId) {
debug('creating interval');
this.intervalId = setInterval(() => {
this.submit((err) => {
if (err) {
debug('error during submit', err, true);
this.errorHandler ? this.errorHandler(err) : console.error(err);
}
});
}, this.uploadRate);
}
}

WinstonCloudWatch.prototype.add = function(log) {
debug('add log to queue', log);
submit(callback) {
var groupName = typeof this.logGroupName === 'function' ?
this.logGroupName() : this.logGroupName
var streamName = typeof this.logStreamName === 'function' ?
this.logStreamName() : this.logStreamName
var retentionInDays = this.retentionInDays

var self = this;
if (isEmpty(this.logEvents)) {
return callback()
}

if (!isEmpty(log.message) || isError(log.message)) {
self.logEvents.push({
message: self.formatMessage(log),
timestamp: new Date().getTime()
});
cloudWatchIntegration.upload(
this.cloudwatchlogs,
groupName,
streamName,
this.logEvents,
retentionInDays,
callback
)
}

if (!self.intervalId) {
debug('creating interval');
self.intervalId = setInterval(function() {
self.submit(function(err) {
if (err) {
debug('error during submit', err, true);
self.errorHandler ? self.errorHandler(err) : console.error(err);
}
});
}, self.uploadRate);
kthxbye(callback) {
clearInterval(this.intervalId);
this.intervalId = null;
this.flushTimeout = this.flushTimeout || (Date.now() + defaultFlushTimeoutMs);

this.submit(((error) => {
if (error) return callback(error);
if (isEmpty(this.logEvents)) return callback();
if (Date.now() > this.flushTimeout) return callback(new Error('Timeout reached while waiting for logs to submit'));
else setTimeout(this.kthxbye.bind(this, callback), 0);
}));
}
};

WinstonCloudWatch.prototype.submit = function(callback) {
var groupName = typeof this.logGroupName === 'function' ?
this.logGroupName() : this.logGroupName;
var streamName = typeof this.logStreamName === 'function' ?
this.logStreamName() : this.logStreamName;
var retentionInDays = this.retentionInDays;

if (isEmpty(this.logEvents)) {
return callback();
setOptions(options) {
this.level = options.level || 'info';
this.name = options.name || 'CloudWatch';
this.logGroupName = options.logGroupName;
this.retentionInDays = options.retentionInDays || 0;
this.logStreamName = options.logStreamName;

var awsAccessKeyId = options.awsAccessKeyId;
var awsSecretKey = options.awsSecretKey;
var awsRegion = options.awsRegion;
var messageFormatter = options.messageFormatter ?
options.messageFormatter : (log) => log[MESSAGE]
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) {
config = { accessKeyId: awsAccessKeyId, secretAccessKey: awsSecretKey, region: awsRegion };
} else if (awsRegion && !awsAccessKeyId && !awsSecretKey) {
// Amazon SDK will automatically pull access credentials
// from IAM Role when running on EC2 but region still
// needs to be configured
config = { region: awsRegion };
}

if (options.awsOptions) {
config = assign(config, options.awsOptions);
}

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

cloudWatchIntegration.upload(
this.cloudwatchlogs,
groupName,
streamName,
this.logEvents,
retentionInDays,
callback
);
};

WinstonCloudWatch.prototype.kthxbye = function(callback) {
clearInterval(this.intervalId);
this.intervalId = null;
this.flushTimeout = this.flushTimeout || (Date.now() + defaultFlushTimeoutMs);

this.submit((function(error) {
if (error) return callback(error);
if (isEmpty(this.logEvents)) return callback();
if (Date.now() > this.flushTimeout) return callback(new Error('Timeout reached while waiting for logs to submit'));
else setTimeout(this.kthxbye.bind(this, callback), 0);
}).bind(this));
};

winston.transports.CloudWatch = WinstonCloudWatch;

module.exports = WinstonCloudWatch;
}
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"license": "MIT",
"typings": "typescript/winston-cloudwatch.d.ts",
"peerDependencies": {
"winston": "^3.0.0"
"winston": "^3.0.0",
"winston-transport": "^4.3.0"
},
"dependencies": {
"async": "^3.1.0",
Expand All @@ -31,7 +32,8 @@
"lodash.find": "^4.6.0",
"lodash.isempty": "^4.4.0",
"lodash.iserror": "^3.1.1",
"proxy-agent": "^3.1.1"
"proxy-agent": "^3.1.1",
"triple-beam": "^1.3.0"
},
"devDependencies": {
"@types/node": "13.11.0",
Expand Down
27 changes: 22 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
describe('index', function() {
var sinon = require('sinon'),
should = require('should'),
mockery = require('mockery');


// the following does not, and should not, mock winston,
// to actually test the transport
describe.only('Transport', function() {

var sinon = require('sinon'),
should = require('should'),
mockery = require('mockery');
var winston = require('winston'),
WinstonCloudWatch = require('../index.js'),
clock = sinon.useFakeTimers(),
Transport = require('winston-transport'),
transport;

it('should derive from Winston transport', function() {
transport = new WinstonCloudWatch({});
transport.should.be.an.instanceOf(Transport)
});
});

describe('index', function() {

// TODO check that the API is still this one
var stubbedWinston = {
transports: {},
Transport: function() {}
Expand Down Expand Up @@ -303,5 +321,4 @@ describe('index', function() {
clock.tick(1);
});
});

});