Debugging and Logging in Express

Learn via video courses
Topics Covered

Overview

Have you ever thought about how the developers find the error in the code? What are the ways to find errors in express? What is debugging and logging in express.js?

Debugging is the process of finding and fixing errors, bugs, or other problems in software. Debugging can be enabled in an Express application using the built-in Node.js debug module.

Logging is the process of capturing events or messages in an Express application for subsequent analysis. This can be beneficial for tracking the application's behaviour over time and discovering potential problems. Winston, Bunyan, and Morgan are three logging libraries that can be used with Express.

Debugging and logging in express.js can both help find and fix problems in an Express application. Debugging is beneficial for actively locating and addressing bugs while the application is running, whereas logging is good for documenting events and tracking the behaviour of the application over time.

Introduction to Debugging

Debugging is the process of identifying and fixing errors, bugs, or problems in software, hardware, or other systems. It is an integral part of the software development process and includes analyzing code, examining system logs, running tests, or using debugging tools to step through the system and identify errors. The purpose of debugging is to find the source of problems and fix them so that the system can function as expected.

Debugging can be done in several ways, including using an integrated development environment (IDE) with built-in debugging capabilities or using standalone debugging tools. The debugging process typically involves setting breakpoints in your code, stepping through your program line by line, examining variables and memory, and analyzing error messages and stack traces. Debugging also includes analyzing system logs and identifying performance problems using profiling tools.

Creating Express Server

Now, let’s discuss the express-fileupload installation, usage, and options. Create a new directory for the project and navigate it in the terminal:

Initializing a new Node.js project using npm:

Installing the Express framework as the dependency:

Create a new file called app.js and add the following code to it:

This code will import the express module, create a new express application instance, set up a route for the root URL (/), and start a server listening on port 3000.

Set the Debug Environment Variable to Express

  • On Unix

    To set the DEBUG environment variable for Express applications on Unix/Linux, you can use the export command in your terminal. We will use the export command in the terminal and assume that we have an express application with debugging enabled and a debug instance name of myapp:

    We can also add this command to our startup script or profile so that the DEBUG environment variable is always set when we start a new terminal session.

  • On Windows

    To set the DEBUG environment variable on Windows for Express applications, we can use the set command in Command Prompt or PowerShell. We will assume that we have an Express application with debugging enabled and a debug instance name of myapp:

    This command sets the DEBUG environment variable to my app. We can also add this command to our system's environment variables so that the DEBUG environment variable is always set when we open a new command prompt or PowerShell session.

    Search for the environment variables on the start menu. Click on Edit System Environment Variables. Click on the Environment Variables button. Under System Variables, click New. Enter DEBUG as the variable name and enter myapp. Click OK to save our new environment variables.

Advanced Options

The following advanced options help in a different way. We can use these options by setting up the environment variables.

  • Debug: It selects the namespace for which we want to view logs.
  • DEBUG_COLORS: By setting this to 1 or 0, we can decide whether the visible log should be displayed in a different colour or the same white colour by setting this to 1 or 0. Setting DEBUG_COLORS to 0 displays the logs in plain white text. By default, it is set to 0. When a user uploads a file on a server using express then the user can access the file using req.files as an object for the file.
  • DEBUG_DEPTH: It inspects the depth of the object.
  • DEBUG_FD: It is the file debugger that writes the debugged output.
  • DEBUG_SHOW_HIDDEN: It shows the hidden values or properties of the object.

Introduction to Logger

A logger is a software component that records events or messages to diagnose or debug application or system problems. Logging is an important part of software development and maintenance as it allows developers and system administrators to monitor and analyze application and system behaviour over time.

Each level represents a different level of importance or urgency, if the difficulty level is low then it is being debugged otherwise it is critical. Messages recorded by loggers may contain information about the source of the event or message, the time of occurrence, and any associated data or context.

Using Morgan for Logging Express App

Morgan is a popular logging middleware for Express that provides request-logging functionality.

Create Express App

To create the express app with logging using Morgan, we will use the following steps:

  • Installing the required dependencies:

  • Importing the required packages: we need to import the express and morgan packages.

  • Creating the instance by calling the express() function

  • Adding the Morgan middleware: Middleware is used for logging in to the express app. We can call it by app.use() function with the morgan() function as argument:

    In Morgan, Tiny is the predefined format for logging. We can use other formats or we can create our own.

  • Defining routes: we will define the routes and start the server.

Installing Morgan

To successfully install the Morgan in the editor, the following steps should be followed:

  • Open the terminal or command prompt. Navigate to the project directory. Type the command,

This command will download and install the Morgan package from the npm registry, and add it to the project’s dependencies in the file package.json.

  • We can also install the Morgan globally on the system via the g flag:

This command will install the Morgan globally in the system and we can use it in any project without having any repetition to install it again and again.

But it is recommended that users should install the packages and dependencies locally not globally as it makes it easier to manage the dependencies and ensure consistency across different environments.

Adding Configuration

To add the configuration in the Morgan app, we will use the following commands:

  • Importing the Morgan package:

  • Adding the Morgan middleware to the app:

    Dev is the predefined format for logging in the Morgan. It logs the HTTP request method, response status code, URL, response time, and the size of the response body.

  • Customising the logging format: We can customize the logging format by passing a string as the argument to the morgan() function.

Checking Demo

To check the demo of an express application, we need to follow the steps we followed while creating the express app using Morgan.

When the server is live then write the following command:

This log message contains information such as the URL of the request, the response status code, the response time, the HTTP method, and the size of the response.

Conclusion

  • Debugging is the process of identifying and fixing errors, bugs, or problems in software, hardware, or other systems.
  • A logger is a software component that records events or messages to diagnose or debug application or system problems.
  • Debugging and logging in express.js are essential tools for building and maintaining a robust and reliable.
  • Express.js debugging and logging tools can significantly improve the development and maintenance process of the application.
  • In Express.js debugging and logging tools can make development more efficient and effective which leads to a better end product.