Remote Logging Using node and CloudWatch Logs

If you have multiple servers running your application, you will need a way to monitor your logs in a remote and centralized place and have all the servers report to it. There are a lot of remote logging services, such as loggly, papertrail, etc. I've tried many of them but ended up choosing CloudWatch Logs.

The reasons I prefer CloudWatch Logs over the other services are:

  • It's very cheap. The more you log and the longer retention you need, the bigger issue pricing becomes. CloudWatch Logs also lets you archive your logs for an even lower price.

  • It's fast. New events appear almost in real-time, while I've experienced delays in the other services.

  • The UI is minimal but provides exactly the functionality I'm looking for. It has search, filtering, and you can set up alarms.

  • I like using AWS. The services integrate well with each other and have everything related to my servers in the same place.

On CloudWatch Logs, the events are catalogued by groups and streams. You can read more about these concepts in the CloudWatch developer guide, but to simplify, you can create a group for each applocation you use and a log stream for each error type.

mentum/lawgs is a library that lets you send events from each of the instances to CloudWatch. It's easy to use and does all the setup up for you (like creating log groups and streams and grouping uploads).

To use it, first pass it your AWS credentials:

import lawgs from 'lawgs';
lawgs.config({ region, accessKeyId, secretAccessKey });

Then, create a log group:

const logger = lawgs.getOrCreate('myapp');

And Log events:

logger.log('500 error', { data: 'my params' });

These events should now appear in your dashboard.

Follow me for updates on similar new posts I write or tweet about this post.