What is the best Node.js logging library?

A comparison between the most popular npm packages

·

5 min read

Sooner or later in the lifespan of your project, it will grow to a stage where the humble console.log will stop being adequate for your needs. Maybe you will need to store the logs in multiple places, separated by levels or just print them formatted in a human-readable way. Whatever the exact case may be, there are numerous logging libraries out there that can help you. Here is a short comparison of the best.

Available logging libraries

A quick google search on "node.js logging libraries", "best JS logging libraries", etc. Produced numerous results, eleven of which looked promising. In order to evaluate the state of these open sourced projects we looked at their popularity, project pulse (development and maintenance frequency), dependency & license checks, availability of types for Typescript and so on. The results of this evaluation are summarized in the following table

PackageProsCons
npm: loglevel0 dependencies, lightweight-
npm: pinofast, extensive docs, gaining popularity-
npm: winstonpopular, feature-richlarge package size
npm: npmlogused by npm, lightweightminimal, feature-lacking
npm: loglightweightminimal, feature-lacking
npm: ulogfeature-richnot maintained, deps not maintained, no built-in types
npm: morganpopularnot maintained, no built-in types
npm: bunyan-lot of open issues, no built-in types
npm: tracer-not maintained
npm: signalecustomizablenot maintained
npm: log4js-lot of open issues

Head to head comparison

Out of the packages from above it is only loglevel, pino and winston that seem to be universal and rich in features, while still being supported and maintained. We will compare them using available tools such as npm trends and npmcompare.

npm trends loglevel vs pino vs winston

Since tools like npmcompare and npm trends essentially measure popularity of a package, they unsurprisingly favor winston which has been around for almost as long as npm itself. Also unsurprisingly, winston has the biggest number of unresolved issues among these three.

In contrast, results reveal that pino, the youngest one of the three, is most worked on, with a new version release every 9 days. It also has a quite strong high ratio of stars per download, which is an indication of a good user experience. And it is tiny in comparison to winston with 2.5KB min zipped

Finally, loglevel has the smallest community of the three, which is surprising given its number of downloads is equal to that of winston. This can be partially explained by the fact that there are several popular libraries that depend on it.

Playground

In order to compare them in practice a minimal testing setup was put in place. It can be found on log-lib-compare - CodeSandbox or cloned from obostjancic/log-lib-compare

NOTE: check README file for instructions on how to start the project

All three libraries were installed in a Typescript project and “wrapped” to implement a common interface. This then allowed a fair comparison between them as they were similarly configured. The following features were tested:

  • log levels
  • error logging
  • object & array logging
  • namespace support
  • structured vs. pretty logging
  • multi transport support

The results, as well as the observations made during development are listed below:

loglevel

It's README describes the project as:

“Minimal lightweight simple logging for JavaScript. loglevel replaces console.log() and friends with level-based logging and filtering, with none of console's downsides.”

loglevel is easy to get started with and integrate in a project, and it does fullfil on the promise it makes. However being minimal, it relies on plugins to get features like log formatting or remote storage. Unfortunately this plugin ecosystem is unmaintained and consists of only 4 officially supported plugins, none of which allow for structured logging or filesystem interaction.

loglevel.png Unstructured logging (left) vs. structured (right) - Note that the formatting of the erorr in the right comes from the shell and not from the library itself.

pino

Similar to loglevel, pino is easy to get started with. Unlike loglevel it comes with structured JSON logging by default. It also supports multiple transport which allow for a simultaneous outputs to console and log files. It has namespace support, as well as child loggers which can be then used to further specify logging scope. According to a very simple duration measurement it is fastest of the three, although it is not 5x faster than winston as it's README claims. When it comes to pretty print support it relies on a npm: pino-pretty package that prints out colored logs to the console. Last but not least, unlike the other 2 it has extensive and well organised documentation

pino.png Unstructured logging (left) vs. structured (right)

winston

winston has a very rich feature set that is comparable to pino. It also supports multiple transports, has child loggers, extensive formatting options etc. Additionally, it has a huge community that brings a lot of answered questions on Stack overflow. Where it falls short compared to pino is its ease of use. Seemingly simple tasks such as timestamp formatting require a lot of configuration. There is no namespace support, but it can be implemented with metadata. Pretty printing is supported and the colors are distinguishable colors between the log levels. The entire documentation is in the README file which makes it a bit hard to navigate considering the projects size and age.

winston.png Unstructured logging (left) vs. structured (right)

Conclusion

In case that you are on the lookout for a lightweight logger that can replace the console.log statements, then loglevel is worth a try. In all other cases where you require a mature logging library, take a look at pino and winston. Both are very capable logging libraries that come with "batteries included", so they will probably meet any criteria you have in mind. winston is the traditional solution while pino more modern, better maintained and provides a much better developer experience overall.