Skip to content

Best Practices To Collect, Customize & Troubleshoot With Node Logs

Application errors can be challenging to diagnose when log files are unstructured and difficult to parse. While log files serve as valuable resources for troubleshooting and understanding application behavior, their utility is diminished when they present an undifferentiated mass of data. NodeJS Logging and Node logs, specifically, can be instrumental in pinpointing issues within Node.js applications.

This blog will help you get on track with Node logs, covering best collection practices, customizing, and troubleshooting.  Node logs can help you understand what’s happening within your application, but only if you can read them. The more precise your Node logs are, the easier it will be to pinpoint issues when your application is distressed. NexJS logging by Alerty can help you achieve your objectives by providing a clear, customizable structure to your log files so you can quickly find the data you need.

Table of Contents

What Are Node Logs?

Coding Logs - Node Logs

Node logs are records created by a Node.js application that provide insight into its performance, behavior, and issues. They capture significant events and activities that occur within a Node.js application. Node logs can include information about:

  • Application flow
  • Errors
  • Warnings
  • Other noteworthy occurrences

By logging these events, developers gain visibility into how the application functions and identify issues that need to be addressed. They can also help developers monitor applications in production so they know how an app performs under real-world conditions.

Common Node.js Logging Levels

Person Writing Code - Node Logs

In the context of Node.js logging, understanding the common logging levels is crucial for effective monitoring and troubleshooting. Each logging level serves a specific purpose and helps developers manage and interpret the information logged by their applications. Here’s a detailed look at each common logging level:

Debug

The debug level is designed for developers to get detailed, granular information about the application's operation. During the development and testing phases, it's helpful to trace the execution flow and diagnose issues in excellent detail.

Characteristics

  • Detail: This section includes detailed information about the application's internals, such as variable values, function calls, and execution paths.
  • Volume: Often produces a high volume of logs, which can be overwhelming in a production environment.
  • Use Case: Ideal for debugging issues, understanding application behavior, and identifying performance bottlenecks.

Example

  • Javascript
  • const debug = require('debug')('app:debug');
  • debug('User %s has logged in from IP %s', username, ipAddress);

Info

The info level provides general operational information about the application. It’s used to log routine events, which helps understand the application's state and confirm that things are running as expected.

Characteristics

  • Detail: Includes higher-level information about the application’s operations and milestones, such as startup messages, user actions, and important events.
  • Volume: Produces a moderate volume of logs, which are less detailed than debug logs but still informative.
  • Use Case: Suitable for tracking the application's overall behavior and operational status.

Example

  • Javascript
  • console.info('Server started on port %d', port);

Warn

The warning level indicates potential issues or warnings that might not be critical but should be addressed. These logs suggest that something unexpected has occurred, which could lead to problems if not monitored or corrected.

Characteristics

  • Detail: Provides information about unusual or unexpected situations that do not necessarily stop the application but may require attention.
  • Volume: Typically produces fewer logs than debug and info levels, focusing on potential problems.
  • Use Case: Useful for identifying and addressing issues impacting application stability or performance before they become critical.

Example

  • Javascript
  • console.warn('User %s tried to access restricted area', username);

Error

The error level logs critical issues that need immediate attention. These logs indicate severe problems that could cause the application to malfunction or crash.

Characteristics

  • Detail: Includes information about errors and exceptions that have occurred, often including stack traces or error codes.
  • Volume: Usually produces fewer logs than other levels, as errors are less frequent but more significant.
  • Use Case: This is essential for diagnosing and addressing critical failures and ensuring issues are resolved promptly to maintain application stability.

Example

  • Javascript
  • console.error('Failed to connect to database: %s', error.message);

By using these logging levels appropriately, developers can effectively manage the volume and detail of logs generated by their Node.js applications, making monitoring, troubleshooting, and maintaining their systems more accessible.

Catch issues before they affect your users with Alerty's NodeJS logging tool today! 

Related Reading

Setting Up Logging In Node.js

Person Thinking while Using Computer - Node Logs

Logging is essential in application development, helping you monitor and debug your Node.js applications effectively. Here’s a detailed guide on setting up logging, including:

  • Built-in methods 
  • Popular logging libraries.

Using Built-in Logging Methods

Node.js provides built-in logging methods through the console object. Here are the most commonly used methods:

  • Console.log(): Used for general logging purposes, typically for informational messages or debugging
  • Console.error(): Used for logging error messages or warnings
  • Console.warn(): Used for logging warnings

Basic Logging

Here’s an example of using these built-in methods: 

  • Console. log('This is an informational message')
  • Console.error('An error occurred:', error)
  • Console. warn('This is a warning')

While these built-in methods are helpful for basic logging, they have limitations, such as a lack of customization options and difficulty managing log levels and output formats. 

Using Popular Logging Libraries

To overcome the limitations of built-in logging methods, developers often use third-party logging libraries. Here are some popular options: 

  • Winston: A versatile logging library that supports multiple transports (file, console, HTTP, etc.), custom log levels, and asynchronous logging
  • Bunyan: A fast and straightforward JSON logging library with support for log rotation and child loggers 
  • Pino: A low-overhead logging library designed for performance, with the backing for asynchronous logging and child loggers
  • Morgan: An HTTP request logger middleware for Node.js, often used in Express.js applications

Setting Up Basic Logging with Winston

Let's set up basic logging using the Winston library. First, install the library using npm:

  • bash
  • npm install winston

Then, create a new logger instance and configure it:

  • javascript
  • const winston = require('winston');
  • const logger = winston.createLogger({

  level: 'info',

  format: winston.format.combine(

    winston.format.timestamp(),

    winston.format.printf(({ level, message, timestamp }) => {

      return `${timestamp} [${level}]: ${message}`;

    })

  ),

  transports: [

    new winston.transports.Console(),

    new winston.transports.File({ filename: 'error.log', level: 'error' }),

    new winston.transports.File({ filename: 'combined.log' }),

  ],

});

Advanced Configuration

In this example, we:

  • Set the log level to 'info', which means it will log messages with a level of 'info' or higher (e.g., 'error', 'warn').
  • Specify the log format, including a timestamp and a custom printf format.
  • Configure transports to output logs to the console, an error log file, and a combined log file.

Now, you can use the logger in your application:

  • javascript
  • logger.info('Application started');

Error Handling

try {

  // Some code that might throw an error

  throw new Error('Something went wrong');

} catch (error) {

  logger.error('An error occurred:', error);

}

This will output logs to the console and the specified log files with the appropriate log levels and formats.

By setting up logging using a library like Winston, you can easily manage log levels, customize log formats, and direct logs to multiple destinations, making it more powerful and flexible than using built-in logging methods alone. 

Advanced Logging Techniques

Person Looking at Code - Node Logs

Logging uncaught exceptions and unhandled promise rejections helps manage errors effectively in a Node.js application. Setting up global error handlers achieves this goal.

You can use process.on to catch uncaught exceptions and unhandled promise rejections. Here's a code snippet demonstrating this:

```javascript
process.on('uncaughtException', (error) => {
    logError(error); // Log the error
    // Optionally exit the process if it's not operational
    if (!isOperationalError(error)) {
        process.exit(1);
    }
});

process.on('unhandledRejection', (error) => {
    logError(error); // Log the error
    // Optionally rethrow the error
    throw error;
});

function logError(error) {
    // Implement your logging logic here (e.g., using Winston)
    console.error('Logged Error:', error);
}

function isOperationalError(error) {
    // Define logic to determine if the error is operational
    return error instanceof BaseError; // Example check
}
```

Exception Logging

This setup ensures that any unhandled exceptions or promise rejections are logged, providing visibility into issues that may occur during runtime.

Integrating Log Management Tools: Enhance Your Node.js Logging Today!

Integrating log management tools can significantly enhance your ability to monitor and analyze logs from your Node.js applications. Some popular log management tools include:

Alerty

Alerty is a cloud monitoring service for developers and early-stage startups, offering:

  • Application performance monitoring
  • Database monitoring
  • Incident management

It supports technologies like:

  • NextJS
  • React
  • Vue
  • Node.js, helping developers identify and fix issues

Database Monitoring

Alerty monitors databases such as:

  • Supabase
  • PostgreSQL
  • RDS

These databases track key metrics like:

  • CPU usage 
  • Memory consumption

Incident Management

It features quick incident management and Real User Monitoring (RUM) to optimize user experience. Its Universal Service Monitoring covers dependencies like:

  • Stripe API
  • OpenAI
  • Vercel

Alerty uses AI to simplify setup, providing a cost-effective solution compared to competitors. It is designed for ease of use, allowing quick setup, and integrates with tools like Sentry, making it ideal for developers and small teams needing efficient, affordable monitoring. 

Catch issues before they affect your users with Alerty's NodeJS logging tool today!

Loggly

Loggly is a cloud-based log management service that provides powerful search, analysis, and visualization capabilities for logs. It allows you to centralize logs from various sources and offers real-time insights.

Splunk

Splunk is a robust platform for searching, monitoring, and analyzing machine-generated big data via a web-style interface. Splunk is highly scalable and is often used for enterprise-level applications.

Datadog

Datadog is a monitoring and analytics platform providing log management and performance monitoring. It allows you to correlate logs with metrics and traces for comprehensive observability.

Benefits Of Using Log Management Tools Like Alerty

Centralized Logging

Log management tools centralize logs from multiple sources, making accessing and analyzing logs in one place more accessible.

Enhanced Search and Filtering

These tools often provide advanced search capabilities, allowing you to quickly find specific log entries based on various attributes (e.g., timestamps, error types).

Real-Time Monitoring

Log management services can alert you to real-time issues, enabling faster response to critical errors or performance degradation.

Data Visualization

Many tools offer dashboards and visualization features that help you understand trends and patterns in your logs, making it easier to identify recurring issues.

Integration with Other Monitoring Tools

Log management services often integrate seamlessly with other monitoring solutions, providing a holistic view of your application's health.

Related Reading

7 Best Practices For Node.js Logging

Man Pointing at Code on Laptop - Node Logs

1. Opt for a Recognized Logging Framework

Using the Console module for production logging is not ideal due to its limitations. Instead, turn to established logging frameworks like:

  • Winston 
  • Pino

These tools offer comprehensive features for managing and organizing logs effectively. They provide advanced capabilities beyond simple console outputs, making them suitable for development and production environments.

2. Use Structured Logging

Structured logging is crucial for making log data easily interpretable by machines. Unlike unstructured logs, just plain text, structured logs organize data into specific fields, typically using formats like JSON. 

This structured approach simplifies parsing and analyzing logs, though it may need to be more human-readable. To bridge this gap, frameworks often offer options to format logs in a more readable style for development while maintaining structured data for production.

3. Employ Log Levels Correctly

Log levels help categorize the severity of log messages, making it easier to identify and prioritize issues. By differentiating between levels such as info, warn, and error, you can manage and respond to events more effectively. 

This classification also helps adjust logging verbosity based on the environment, highlighting critical issues while routine information is manageable.

4. Write Descriptive Log Messages

Detailed and descriptive log messages are essential for understanding the context of events. Instead of vague entries like, something went wrong, aim for messages that include specific information about what happened, such as the following:

  • Nature of the error 
  • Relevant identifiers

This approach helps quickly diagnose issues and understand the state of the application.

5. Always Include a Timestamp

Timestamps in log entries provide essential context about when an event occurred. Without timestamps, it’s challenging to determine the sequence of events or the timing of issues. 

Most logging frameworks include timestamps by default, but you can customize the format to ensure clarity and consistency. Using a standard format like ISO-8601 is recommended for its universality.

6. Add Contextual Fields to Your Log Entries

Incorporating contextual details into logs, such as request IDs or user IDs, enhances the ability to trace and analyze events across systems. This additional context makes it easier to correlate logs and understand the flow of requests or transactions. 

Using child loggers or adding contextual information to each entry can streamline this process and provide valuable insights during troubleshooting and analysis.

7. Ensure Sensitive Data Stays Out of Logs

Your application logs should never capture data like passwords, credit card details, or authorization tokens. In some contexts, even IP addresses might qualify as Personally Identifiable Information (PII).

Log Security Risks

Recall the 2018 incident when Twitter inadvertently logged millions of plaintext passwords into an internal system. Even though no misuse was detected, this is a stark reminder of the implications of negligent logging.

Data breaches through logs could lead to hefty penalties under regulations like:

Troubleshooting With Node Logs

Man Using Laptop - Node Logs

Logs are a developer’s best friend. They provide a detailed record of application activity over time and are crucial for monitoring applications and diagnosing problems. When a Node.js application runs smoothly, logs can help you understand how the application works.

Logs can provide insight into what went wrong in the event of a crash, performance issues, or unexpected behavior. They can even help you predict future problems and resolve them before your users notice.  

Identifying Common Issues and How Logs Can Help

Application Crashes

Logs, including error messages and stack traces, can reveal the events leading up to a crash. By analyzing these logs, developers can identify the root cause of the failure.  

Performance Bottlenecks

Logs can help track response times and resource usage, allowing you to pinpoint areas where the application is slowing down. For example, logs may show that specific API endpoints are taking longer to respond, indicating a need for optimization.

Authentication Failures

Logs that track user authentication attempts can help identify unauthorized access attempts or issues with user credentials. For instance, logs may show multiple failed login attempts from a specific IP address, indicating a potential security threat.  

Configuration Errors

Logs often provide information about application misconfigurations. If a service fails to start due to incorrect settings, the logs typically contain error messages indicating what went wrong.  

Setting Up Alerts for Critical Log Entries

Setting up alerts for specific log entries is essential to ensure timely responses to critical issues. Here’s how to do it:

Choose a Log Management Tool

Use Alerty; it supports alerting features.

Define Alert Criteria

Determine which log entries should trigger alerts. Common criteria include:  

  • Error messages (e.g., "Unhandled exception," "Failed login")
  • Performance thresholds (e.g., response times exceeding a specific limit)
  • Security events (e.g., multiple failed login attempts from the same IP)

Set Up Alerts

In Alerty, configure alert rules based on the defined criteria. You might set an alert for any log entry containing the word error or for any instance of a specific error code. Specify notification methods (e.g., email, SMS, Slack) to ensure the relevant team members are promptly informed.

Test Alerts

Simulate conditions that trigger alerts to ensure the system functions as expected and receives notifications.  

Related Reading

Catch Issues Before They Affect Your Users with Alerty's NodeJS Logging Tool

Alerty is a cloud monitoring service for developers and early-stage startups, offering:

  • Application performance monitoring
  • Database monitoring
  • Incident management

It supports technologies like:

  • NextJS
  • React
  • Vue
  • Node.js, helping developers identify and fix issues

Core Monitoring Features

Alerty monitors databases such as:

  • Supabase
  • PostgreSQL
  • RDS tracking key metrics like CPU usage and memory consumption

It features quick incident management and Real User Monitoring (RUM) to optimize user experience. Its Universal Service Monitoring covers dependencies like:

  • Stripe API
  • OpenAI
  • Vercel

AI-Powered Efficiency

Alerty uses AI to simplify setup, providing a cost-effective solution compared to competitors. It is designed for ease of use, allowing quick setup, and integrates with tools like Sentry, making it ideal for developers and small teams needing efficient, affordable monitoring. 

Catch issues before they affect your users with Alerty's NodeJS logging tool today!