Logging System

This document provides an overview of the logging system used in our application, which is implemented using Winston, a flexible and extensible logging library for Node.js.

Logger Configuration

Logging Levels

  • Development Mode: Set to debug to provide detailed information for debugging
  • Production Mode: Set to info to reduce verbosity

Error Handling

If an error occurs, the logger captures the error stack trace for more detailed debugging information.

Formats

The logger uses the following formatting options:

  • Error Stack Trace: Captures full stack traces for errors to identify issue sources
  • Colorization: Log messages are colorized in development mode for better readability
  • Printf Format: Logs include level and message in the format: level: message

Transports

The logger saves messages to multiple destinations (transports):

  1. Console

    • Logs all messages to the console
    • Errors are directed to stderr
  2. File System

    • Error logs: logs/error.log
    • Combined logs: logs/combined.log

Usage Guide

Basic Implementation

Import the logger in your application:

import logger from "./logger"

Logging Messages

Use appropriate methods based on message severity:

logger.info("This is an info message")
logger.warn("This is a warning message")
logger.error("This is an error message")

Log File Structure

Directory Layout

logs/
├── error.log    # Contains only error messages
└── combined.log # Contains all log messages

Advanced Configuration

Adding Custom Transports

Winston supports various additional transports, including:

  • Database logging
  • External monitoring services
  • Rotating log files

Example: Daily Rotating File Transport

import "winston-daily-rotate-file"
 
logger.add(
  new winston.transports.DailyRotateFile({
    filename: "logs/%DATE%-combined.log",
    datePattern: "YYYY-MM-DD",
    zippedArchive: true,
    maxSize: "20m",
    maxFiles: "14d",
  })
)

This configuration:

  • Creates new log files daily
  • Automatically zips old log files
  • Maintains logs for the last 14 days

For more advanced configurations and integrations, please refer to the Winston documentation.