3

Logging Solutions for Node.js

 2 years ago
source link: https://blog.bitsrc.io/logging-solutions-for-node-js-964487f169ea
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Logging Solutions for Node.js

Recommended Logging Solutions for your Node.js Application. Comparison for Winston, Pino, Bunyan, Log Level, and npmlog

0*5mwdAwoRpjQrPzW0.jpg

Logging is an essential part of building modern web applications. It helps developers investigate various application issues and find solutions quickly. However, developers often ignore using login in applications and spend unnecessary debugging time.

So, in this article, I will discuss the importance of logging and different login solutions available for Node.js to speed up your debugging process.

Why Do We Need a Logging?

As mentioned, logging helps developers easily track errors in the applications. You can easily understand the root causes and provide the correct solutions using the correct logging solutions.

Also, you can categorize the issues in your applications using log levels. Then, you can prioritize the issues based on the category and address the urgent issues first. For example, ERROR, INFO, DEBUG, WARN, and FATAL are the most common log levels. This is because logs in the FATAL and ERROR categories need to be addressed immediately, while you can take some time to fix WARN logs since they are not blocking issues.

Although you can build a logging solution for your application from scratch, it is advisable to use a well-known logging library and customize it as you need. This process will help you follow best practices while saving precious time. So, let’s discuss some of the most used Node.js logging solutions.

1. Winston

Winston is a well-known, simple logging library for Node.js. It supports universal logging across multiple transports. And you can use it to route logs to other services such as AWS CloudWatch, graylog2, and even Sematext Logsene. It has more than 7 million weekly NPM downloads and 18K+ GitHub stars.

Features of Winston

  • It’s simple to use and customizable.
  • It offers a variety of log formats.
  • It allows you to send and save logs in various formats, including files, databases, emails, and consoles.
  • It assists you in profiling a block of code and measuring the time it takes for successful execution.

Installation

You can easily install Winston using NPM as follows:

npm install winston

Using Winston

After the installation of the library, you can create your logger using Winston as follows:

const winston = require('winston');

const logger = winston.createLogger({
level: 'warn',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});

2. Pino

Pino is another popular Node.js logging library. It is well known for its asynchronous logging ability and claims to be more than 5x faster than alternatives. Pino has more than 2.8 million weekly NPM downloads and 9.2K+ GitHub stars.

Features of Pino

  • It uses minimum resources for logging.
  • It has child loggers.
  • Printing and formatting.
  • It is the quickest JSON logger.
  • It has many core and community modules that contribute to forming a thriving ecosystem.
  • All log processes known as transports are executed in a worker thread, as recommended by Node.js.

Installation

You can easily install Pino using NPM as follows:

npm install pino

Using Pino

After the installation, you can create a new logger using Pino and export it for use in other files.

const pino = require('pino');
module.exports = pino({});

The above code returns a logger instance, and you can export it and use it as shown below:

const logger = require('./logger');

logger.info('My Log!');

3. Bunyan for Node.js logging

Bunyan is another well-known, simple Node.js logging library. It stores logs in JSON format and provides a CLI tool to view logs. It has more than 1.6 million weekly NPM downloads and 6.8K+ GitHub stars.

Features of Bunyan

  • A serializer will be used to render log objects in a customized manner.
  • A DTrace-based log snooping system for runtime logs.
  • The runtime environment supports Node.js, Browserify, Webpack, and NW.js.

Installation

You can easily install Bunyan using NPM as follows:

npm install bunyan

Using Bunyan

After installation, you need to create a new logger using the createLogger method like below:

const bunyan = require('bunyan');

var log = bunyan.createLogger({
name: '<application name',
stream: process.stdout
});

log.info('Hello world');

4. Log level

Log level is another popular logging solution that supports both Node.js and browsers. It is a minimal, lightweight, simple logging for JavaScript. Log level has over 9 million weekly NPM downloads and 2.2K+ GitHub stars.

Features of Log level

  • Log level is convenient and straightforward to use.
  • It supports Node.js and browsers.
  • It weighs in at 1.1KB minified and gzipped.
  • It supports filtering by log level.
  • It automatically falls back to simpler console logging methods if specific ones are unavailable.
  • It works well with all the standard JavaScript loading systems.

Installation

You can easily install Log level using NPM as follows:

npm install loglevel

Using Log level

After installation, you can use Log level as below:

  • ES6 Modules
import log from 'loglevel';
log.warn("my log");
  • CommonsJS
var log = require('loglevel');
log.warn("my log");
  • Directly on web pages
<script src="loglevel.min.js"></script>
<script>
log.warn("my log");
</script>

5. npmlog

npmlog is a basic yet widely used logging solution for Node.js. It is the official logger used by NPM. And it supports custom levels and colored outputs to make the output more user-friendly. It has more than 19 million weekly NPM downloads.

Features of npmlog

  • It allows setting log headers with custom styles.
  • It supports custom levels and colored output.

Installation

You can easily install npmlog using NPM as follows

npm install npmlog --save

Using npmlog

After installation, you can import it and use it like below:

const log = require('npmlog');

log.info('INFO', 'Hello', {'message': 'test'})

Conclusion

I have in this article discussed the importance of logging and the different logging solutions available for Node.js. Each of the discussed libraries has its own highlighted features, and you can find their popularity and usage in the following chart.

0*OyeEVgE9deoqVKb6.png

As you can see, all the above logging libraries have a weekly download count beyond 1 million. The npmlog library is even touching 20 million downloads. These counts show the importance of logging and how much people use them in their projects.

I hope my suggestions will help you choose the best logging library for your Node.js project. Thank you for reading!

Build composable web applications

Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favorite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience.

Bring your team to Bit Cloud to host and collaborate on components together, and speed up, scale, and standardize development as a team. Try composable frontends with a Design System or Micro Frontends, or explore the composable backend with serverside components.

Give it a try →

0*-bOLd_JKeI1L-87M.gif

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK