This article is going to explore using winston and Elasticsearch to do “structured logging” in Node.js. The basic idea is that we want to write logs with some metadata attached to them, beyond just a timestamp, a level, and a message.

The traditional “twelve factors” approach to logging is just to write your logs to stdout, and let some extrenal process deal with logging. For example, you might use fluentd to read stdout on all your processes and write the results into Elasticsearch. This works well, and it works across different projects written in different languages. But, with a little extra work, we can write some extra information into those logs.

For example, for every request we could write the URL of the request to the log as request.url, and then write the response time in milliseconds as response.responseTime, and now it’s a simple mater to generate an Elasticsearch graph showing our average and max response time to HTTP requests, or even to write a query which finds the top ten slowest routes in our system. Or we could write a user field to the log on every request, so we can figure out which users in our system are most active.

#node.js #winston #logging #elasticsearch #node

Structured logging in Node.js with Winston and Elasticsearch
19.30 GEEK