NestJS Developers: Upgrade Your Logging with This Simple Guide
Tags: javascript, nestjs, node, beginners
Published: 2024-05-18 20:04:40

[NestJS](https://nestjs.com) is a powerful, progressive Node.js framework for building efficient and scalable server-side applications. It is written in TypeScript and is heavily inspired by Angular. It comes with a modular architecture and in-built support for a plethora of back-end features straight out of the box. One important part of developing applications with NestJS, or with any other back-end framework, is logging.

## Why Logging Is Important for Developers

**Debugging and Troubleshooting the Detection of Errors**: Logging helps in the identification and understanding of errors and exceptions that occur during the application's runtime. By reading these log entries, developers can identify the source of a problem and make a decision on the best course of action to take.

**Context Information**: Logs allow for context about the application state when the error occurred. Context information could be a set of request parameters, user sessions, or system states that can be used to diagnose complex issues.


## Choosing the Right Logging Library for NestJS

When building applications with NestJS, effective logging is essential for debugging, monitoring, and maintaining your application. There are several open-source logging libraries available, each offering unique features. Some popular choices include **Winston, Pino, Morgan, Bunyan, Log4js, and Errsole**. The other ones will give you custom logging, but Errsole has a few key benefits that you might want to think about.

[Errsole](https://github.com/errsole/errsole.js) comes with a built-in dashboard, allowing for easy visualization and management of logs without requiring additional integrations. Errsole also automatically captures standard logs, ensuring comprehensive coverage with minimal effort. Furthermore, it includes a notification system that sends real-time alerts via Slack and email, complete with error context, enabling quick responses to issues. These features make Errsole an excellent choice for integrating effective logging into your NestJS application, ensuring a more reliable and maintainable system.

---

## Step-by-Step Guide

1. **Install Errsole and Dependencies**
First, you need to install Errsole and your choice of storage adapter. In this guide, we will use SQLite to store logs locally as a file. However, Errsole supports many popular databases, so you can select the one that best meets your requirements.

```
npm install errsole errsole-sequelize

```
**2. Initialize Errsole**
Insert the Errsole initialization code at the beginning of your Nest.js app's main file (main.ts).


```
import * as errsole from 'errsole';
import * as ErrsoleSequelize from 'errsole-sequelize';

errsole.initialize({
  storage: new ErrsoleSequelize({
    dialect: 'sqlite',
    storage: '/tmp/logs.sqlite'
  })
});
```

Example (main.ts)

```
import * as errsole from 'errsole';
import * as ErrsoleSequelize from 'errsole-sequelize';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

errsole.initialize({
  storage: new ErrsoleSequelize({
    dialect: 'sqlite',
    storage: '/tmp/logs.sqlite'
  })
});

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();
```


---

## Accessing the Web Dashboard

After setting up Errsole, errsole will start capturing the logs automatically and you can access the Errsole Web Dashboard:

**Local Environment**: Open your web browser and visit http://localhost:8001/.

**Remote Server**: If your remote app is hosted at https://api.example.com, visit https://api.example.com:8001.

> Errsole provides full flexibility, allowing you to modify the port, path, and domain to keep your dashboard secure and hidden from others.

For more details about advanced configurations and other features, please refer to the official documentation. [README](https://github.com/errsole/errsole.js)