Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
When an application crashes, you can read the stack trace to identify the problem.
But a stack trace only shows you the state of the application at the point of failure and not before it encountered the error.
This is where logging libraries come in.
They detail the application behavior before the point of failure and send them to your desired destination, such as a file or a monitoring tool.
There are many logging libraries available for Node.js, but not all of them are created equal.
In this article, we’ll take a look at the top five logging libraries for Node.js:
We’ll analyze them based on their features and popularity to help you choose the right one for your needs. But first, let’s establish why you’d even want to use a logging library for Node.js.
Why Use Logging Libraries for Node.js?
Node.js ships with the console API, which contains methods that can be used for logging. But the logs tend to be difficult to read or filter since they are in a text format. Most logging tools provide JSON support out of the box, and you can easily add dates, sort, or send logs to your desired destinations. Consider the following example that uses the console API:level field tells you the level each message belongs to at a glance. Most libraries support the following levels(ordered from the most severe to the least severe):
fatal: indicates a serious problem that can stop the application from runningerror: used to indicate that a required task failedwarn: indicates something that may cause a probleminfo: used for messages that confirm that the application is behaving as it shoulddebug: used for messages that help when diagnosing an issue
timestamp property, which tells you the date the message was logged. Since the logs are in JSON format, programs can use the date to easily filter or sort the messages.
On top of that, the logging tools also allow you to specify the destination you want to save your logs. You can even configure them to send logs to a specific file, database, or even cloud tools.
Now that you know why you should be using a logging library, we’ll go over the five best Node.js logging libraries.
5 Best Node.js Logging Libraries
#1 Winston
Winston is a popular, feature-rich, and flexible logging library for Node.js. The default format it uses is JSON, but it can be configured to send logs in multiple storage devices called transports. Winston currently has 7 million weekly downloads on npm and 19 thousand stars on GitHub, making it the most-starred logging library in Node.js. Here are some of Winston’s features:- JSON format: it creates JSON logs, which are easier to parse, sort, or filter
- Multiple transports: Winston can send logs to files, databases, the console, or tools like AWS CloudWatch or Graylog
- Supports child loggers
- Allows you to query logs
- Supports Node.js streams
- Supports creating custom levels
How to Use Winston
To install Winston, enter the following command in your terminal:npm install winston
Once you’ve installed it, create a winston_demo.js file and add the following code:
createLogger() method with a configuration object as its argument. You then assign the object a transports property with an instance of Console() so that Winston should send all logs to the console. Finally, you call the appropriate level method for each message you want to log.
Winston has seven levels (ordered from the most to least important): error, warn, info, verbose, debug, and silly.
When you run the program:
node winston_demo.js
It yields output that looks like the following:
info to error. To see all messages, you have to assign a level property to the config object:
winston_demo.js file prints JSON logs without dates. To add dates, you can use the format option in the config object:
error.log and app.log.
When you open error.log, you will see error and warning logs only:
app.log, you will see the following:
#2 Pino
Pino is another popular JSON logging tool. It is more lightweight and claims to be 5x faster than other logging libraries. At the time of writing, it has 3 million weekly downloads on npm and has 10K Github stars. The following are some of the features :- Transports: you can use Pino to send logs to files, the console, or tools like Sentry, Azure Application Insights, CouchDB, etc. See Pino’s Known Transports section for more details.
- Web frameworks support: Pino has modules that support logging in popular Node.js web frameworks, such as Express, Fastify, Hapi, etc.
- Browser API: Pino can be used for logging in the browser environment
- Creating child loggers
- Pretty printing with the
pino-prettymodule
How to Use Pino
First, install Pino with the following command in a new directory:npm install pino
Next, create a logger.js file to initialize the logger:
pino() function with a configuration object. You can leave the object empty for now.
Next, create a pino_demo.js file with the following contents:
fatal to info. To see all levels, you can specify the level property in the config object:
pino-pretty module:
npm install pino-pretty --save-dev
To use it, pipe the commands as follows:
`node pino_demo.js
| npx pino-pretty`
fs module.
Open logger.js and add the following:
#3 Bunyan
Bunyan is another invaluable tool that can be used for logging. It is a strong advocate of JSON logs and currently has 1.7 million weekly downloads on npm and 6.9K Github stars. Bunyan provides a simple API alongside many useful features:- Transports: using Nodejs streams, you can configure
bunyanto send logs to the console, files, streams, etc. - Pretty printing:
bunyanprovides a CLI for prettifying and filtering logs - Supports multiple runtime environments, such as [Node.js], Browserify, Webpack, and Nw.js
- Child loggers
- Node.js streams support
How to Use Bunyan
In a new directory, install Bunyan via npm:npm install bunyan
Next, create a file bunyan_demo.js with the following:
createLogger() method with a configuration object as an argument. You then assign the application name using the name property.
Now, run the file:
node bunyan_demo.js
You will see output that looks like the following:
info level and goes up to the fatal level. You can use the level property to change this behavior.
You can also make use of the Bunyan CLI to tidy the output:
`node bunyan_demo.js
| ./node_modules/.bin/bunyan`
streams property, which accepts an array of transports:
error.log file, and the rest are sent to the app.log.
To continue exploring the Bunyan Node.js logging library, visit their documentation page.
#4 Loglevel
Loglevel is a lightweight logging library, and currently has 9 million weekly downloads on npm and 2.3K stars on Github. But it lacks a lot of features available in the logging tools we have looked at so far, notably native JSON support. Depending on your use case, it has some features worth considering:- Can be embedded in the browser.
- It is a single file with no dependencies.
- Can be extended with plugins to redirect logs, format, or filter logs. Some popular plugins are loglevel-plugin-prefix, and loglevel-plugin-remote.
How to Use Loglevel
First, install loglevel in a new directory:npm install loglevel
Create a loglevel_demo.js file with the following contents:
#5 Npmlog
Last on our list is npmlog, a logging tool used by the npm project. It currently has 23 million weekly downloads and 390 stars on GitHub. Npmlog is simple and lightweight, making it a great option for beginners. Here are some of the features:- Colorized output in the terminal
- Ability to define custom levels
How to Use Npmlog
First, install thenpmlog library:
npm install npmlog
Next, create a npmlog_demo.js file:
info to error are logged. Also, note that the output is colorized allowing you to differentiate the messages. However, since Npmlog doesn’t use the JSON format, it can be a huge drawback, especially when you want to sort or filter them.
If you want npmlog to show all the messages, you can use the level property:
npmlog-file library. To use it, install npmlog-file in your directory:
npm install npmlog-file
Next, add the following code to your file like so:
app.log created with the log contents.
To explore npmlog, visit the npmlog documentation.