Skip to content

Instantly share code, notes, and snippets.

@carlok
Created May 23, 2018 15:16
Show Gist options
  • Save carlok/f2f25ea56fd395328b7dfaa5d598b8c8 to your computer and use it in GitHub Desktop.
Save carlok/f2f25ea56fd395328b7dfaa5d598b8c8 to your computer and use it in GitHub Desktop.
A simple Node.js module for logging on AWS CloudWatch using Winston
"use strict";
let winston = require("winston");
let cloudWatchTransport = require("winston-aws-cloudwatch");
const set = function (options) {
let logger = new winston.Logger({
transports: [
new winston.transports.Console({
timestamp: true,
colorize: true
})
]
});
const config = {
logGroupName: options.logGroupName,
logStreamName: options.logStreamName,
createLogGroup: false,
createLogStream: true,
awsConfig: {
accessKeyId: options.awsConfig.accessKeyId,
secretAccessKey: options.awsConfig.secretAccessKey,
region: options.awsConfig.region
},
formatLog: function (item) {
return item.level + ": " + item.message + " " + JSON.stringify(item.meta);
}
};
logger.add(cloudWatchTransport, config);
logger.level = "silly";
logger.stream = {
write: function (message, encoding) {
logger.info(message);
}
};
return logger;
};
module.exports = {
set: set
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment