What is winston-cloudwatch?
The winston-cloudwatch npm package is a transport for the popular logging library Winston that allows you to send logs to Amazon CloudWatch. This package is useful for integrating your application's logging with AWS CloudWatch, enabling centralized log management, monitoring, and analysis.
What are winston-cloudwatch's main functionalities?
Basic Logging to CloudWatch
This feature allows you to send basic log messages to AWS CloudWatch. By configuring the transport with your log group, log stream, and AWS region, you can start logging messages to CloudWatch.
const winston = require('winston');
const WinstonCloudWatch = require('winston-cloudwatch');
const logger = winston.createLogger({
transports: [
new WinstonCloudWatch({
logGroupName: 'your-log-group',
logStreamName: 'your-log-stream',
awsRegion: 'your-region'
})
]
});
logger.info('Hello, CloudWatch!');
Custom Log Format
This feature allows you to customize the format of your log messages before sending them to CloudWatch. In this example, the log messages are formatted with a timestamp and converted to JSON.
const winston = require('winston');
const WinstonCloudWatch = require('winston-cloudwatch');
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new WinstonCloudWatch({
logGroupName: 'your-log-group',
logStreamName: 'your-log-stream',
awsRegion: 'your-region'
})
]
});
logger.info('This is a custom formatted log message');
Error Logging
This feature demonstrates how to log error messages to CloudWatch. By catching an error and logging its message, you can keep track of exceptions and issues in your application.
const winston = require('winston');
const WinstonCloudWatch = require('winston-cloudwatch');
const logger = winston.createLogger({
transports: [
new WinstonCloudWatch({
logGroupName: 'your-log-group',
logStreamName: 'your-log-stream',
awsRegion: 'your-region'
})
]
});
try {
throw new Error('Something went wrong!');
} catch (error) {
logger.error(error.message);
}
Other packages similar to winston-cloudwatch
winston
Winston is a versatile logging library for Node.js that supports multiple transports. While it does not natively support CloudWatch, it can be extended with custom transports like winston-cloudwatch to achieve similar functionality.
bunyan
Bunyan is another logging library for Node.js that focuses on structured logging. It can be integrated with AWS CloudWatch using additional packages like 'bunyan-cloudwatch'. Compared to winston-cloudwatch, bunyan-cloudwatch offers similar functionality but is tailored for Bunyan's logging format.
log4js
Log4js is a logging library inspired by Apache Log4j. It supports various appenders, including one for AWS CloudWatch through the 'log4js-cloudwatch-appender' package. This provides similar capabilities to winston-cloudwatch but within the Log4js ecosystem.
winston-cloudwatch

Send logs to Amazon Cloudwatch using Winston.
Issues
As of now there are two major issues that I'm fixing:
I am working on those, please note that they do prevent the module from working properly in certain cases.
Installing
$ npm install --save winston winston-cloudwatch
Configuring
AWS configuration works using ~/.aws/credentials
as written in AWS JavaScript SDK guide.
I still have to check if everything works ok with ENV variables.
Usage
Please refer to AWS CloudWatch Logs documentation for possible contraints that might affect you.
Also have a look at AWS CloudWatch Logs limits.
var winston = require('winston'),
options = {
logGroupName: 'your-log-group',
logStreamName: 'your-log-stream'
};
winston.add(require('winston-cloudwatch'), options);
winston.error('log this', { and: 'this too' });
Release notes
0.1.2
Work around to the AWS time limit for sending events.
0.1.1
First release