API with NestJS #166. Logging with the Drizzle ORM

NestJS

This entry is part 166 of 174 in the API with NestJS

Debugging is a great way to find issues when running an application locally. Unfortunately, we don’t have this option in a deployed application. Because of that, implementing logging functionality is necessary to track down and investigate any potential problems. In this article, we learn how to use the logger built into NestJS and integrate it with the Drizzle ORM.

Logger built into NestJS

NestJS conveniently offers built-in logging functionalities that we can use. Each message that we log needs to have a level. Here is a breakdown of all log levels, sorted by severity:

  1. fatal
  2. error
  3. warn
  4. log
  5. debug
  6. verbose

NestJS added the “fatal” log level some time last year.

To create logs, we should use the class. One way to do that is to import it from .

articles.service.ts

Although the above approach does the job, the official documentation recommends creating an instance of the logger inside each class that uses it.

articles.service.ts

With this adjustment, NestJS can include the name of our service in the log, which makes it easier to read.

Working with log levels

The higher the level of our log is, the higher the severity. For instance, if a user attempts to access an article that does not exist, it likely shouldn’t cause an alarm in the middle of the night. Because of that, in our example above, we used instead of . The higher the log, the more concerned we should be.

To avoid cluttering our logs, we can filter some of them out.

main.ts

A surprising thing is that providing just turns on all log levels. When we take a look at the isLogLevelEnabled function under the hood of NestJS, we can see that searches for the looks for the highest severity included in our array and turns on the logs with the lower severity as well. However, we provide the complete array for readability.

Logging SQL queries with the Drizzle ORM

Logging SQL queries allows us to investigate how Drizzle ORM interacts with our database and notice potential issues.

The most straightforward way to start logging our SQL queries is to pass an additional parameter to the function.

drizzle.service.ts

The downside to this solution is that the output of the default Drizzle ORM logger looks different from that of the NestJS logger. Let’s deal with that by creating a custom Drizzle ORM logger.

custom-drizzle-logger.ts

Every time Drizzle ORM wants to log a query, it calls the function. We can take advantage of that by using the NestJS logger.

We can now use our custom logger instead of the default one built into the Drizzle ORM.

drizzle-service.ts

Thanks to this solution, our logs are a lot more consistent.

Using a logging interceptor

Manually defining logs for every case in our application might produce the best results but can be time-consuming. To automate our logs somewhat, we can write a NestJS interceptor that handles logging automatically.

Interceptors in NestJS allow us run additional logic before or after NestJS handles a particular API request.

logger.interceptor.ts

You can add more data into the logs such as the POST request body.

Above, we wait for the HTTP request to complete and then determine the log level based on the status code of our response.

There is more than one way to use our interceptor. For example, we can decorate a single method with it.

articles.controller.ts

Another approach is to decorate the entire controller.

articles.controller.ts

We can also use the function to apply our interceptor to all our controllers.

main.ts

Summary

In this article, we explored how to use the built-in NestJS logger in a few different ways. Thanks to our approach, we can automatically log all SQL queries that Drizzle ORM makes and the HTTP responses our NestJS application sends. Besides that, we can also implement custom logging if necessary. To implement that, we had to create a custom interceptor that logs various messages automatically when our NestJS application sends a response. We also made a custom Drizzle ORM logger and combined it with the logger built into NestJS.

By adopting a thorough approach to logging, troubleshooting issues becomes significantly easier. It makes it a valuable thing to learn and implement in your NestJS application.

Series Navigation<< API with NestJS #165. Time intervals with the Drizzle ORM and PostgreSQLAPI with NestJS #167. Unit tests with the Drizzle ORM >>
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
minhmoon
minhmoon
2 months ago

Hey Wanago, thanks for article.
I hope you can create a seri about microservices with nest.js.
That topic is very hard and interesting!