Created
May 23, 2018 15:16
-
-
Save carlok/f2f25ea56fd395328b7dfaa5d598b8c8 to your computer and use it in GitHub Desktop.
A simple Node.js module for logging on AWS CloudWatch using Winston
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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