Logging with Pino and AsyncLocalStorage in Node.js

Logging with Pino and AsyncLocalStorage in Node.js

Logging with Pino and AsyncLocalStorage in Node.js - In this post, you can learn what a logging library is, why you should use it, and how to log with Pino and AsyncLocalStorage in Node.js. Proper logging can be the difference between a lousy dump of debug statements and a powerful debugging.

Spending hours, or even days, trying to fix an obscure bug is frustrating and unproductive. Eventually, you’ll end up staring at the screen waiting for an eureka moment to magically happen.

But what if instead of waiting for the solution to magically come to you, you had the super power to systematically trace any edge-case bug that you're dealing with?

You can — with logging. When used correctly, logging can give you the necessary insights into your application so you can figure out exactly what happened. Proper logging can be the difference between a lousy dump of debug statements and a powerful debugging tool that helps you find bugs easier and fix them faster.

What is a logging library and why should you use it?

In logging, it's important to have an output that's both easy to read by humans and parseable by machines. For us developers, it's important that when we look and inspect the logs that we can make sense of it. Machines need to be able to parse the logs so we can run advanced queries and perform fancy aggregations.

JSON is a format that fits both criteria, which is why a logging library parses the output into valid JSON and makes sure your logs are always formatted properly.

You may get by using console.log in hobby projects. In  production-grade Node.js applications, however, it's often useful to be able to distinguish between different log levels.

Enter logging libraries, which also allow you to turn logging on and off at different levels. In a production environment you'd typically want to have errors and perhaps warnings on, but in a staging environment debug/more verbose logs are useful as well, which would otherwise add too much noise in production.

What is Pino?

Pino is a popular logging library in the Node.js ecosystem. It's fast and has minimal overhead.

Prerequisites for this guide

  • Node.js version >=12.17 or >=13.10
  • NPM (usually included with Node.js)
  • An  Express web server you wish to add logging to

blog node.js node

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Hire Top Node JS Developers | Best Node.js Development Company India

Hire dedicated Node JS developers & programmers in India for custom full-stack NodeJS web development projects on hourly/full-time basis. Strict NDA, 16+ years exp & 2500+ clients|450+ Experts

Hire Dedicated Node.js Developers - Hire Node.js Developers

Get business-centric Node.Js development services from expert Node.JS developers. We have expertise in developing & maintaining Node JS apps as per the business requirements.

Node JS Development Company| Node JS Web Developers-SISGAIN

SISGAIN is the top rated node js development company providing professional services on node js web and mobile development.

Why use Node.js for Web Development? Benefits and Examples of Apps

Get Node.js developers & programmers, and dedicated Node coders in India with average 5 years of experience. 15+ Yrs Exp, 500+ Staff, 13800+ Projects, 6800+ Clients.

Node JS Development Company | Hire Node.js Developers

Looking to hire Node js developers? One of the top Node js development companies in India & USA offers cost-effective Node js web development services.