Skip to main content

Introduction to Log-Express

Welcome to Log-Express, a powerful and customizable logging middleware designed for Express.js applications. Whether you're building a small project or a large-scale application, Log-Express provides an efficient and flexible solution for logging API requests.

Key Features

  • Customizable Log Levels: Control the verbosity of your logs with log levels like trace, debug, info, warn, error, and fatal.
  • Console and File Logging: Log messages can be printed to the console, saved to log files, or both.
  • Daily Log Rotation: Optionally split log files by day for easier organization.
  • Intelligent Request Logging: Automatically log HTTP methods, URLs, status codes, response times, and client IPs.
  • Minimal Setup: Log-Express is easy to integrate with any Express.js application, requiring minimal configuration to get started.

Why Use Log-Express?

Effective logging is crucial for monitoring and debugging Express applications, and Log-Express simplifies this process by offering:

  • Granular Control: Define what gets logged based on log levels and routes.
  • Multi-Destination Logging: Choose where to log—either to files, the console, or both.
  • Scalable for Projects: Suitable for both development and production environments.

Installation

To install Log-Express, simply run:

npm install log-express

Usage Example

Below is a simple example of how to integrate Log-Express into your Express application:

const express = require("express");
const logExpress = require("log-express");

const app = express();

// Middleware configuration
app.use(
logExpress({
logLevel: "info", // Set log level (default: 'info')
consoleLog: true, // Enable console logging (default: true)
saveLogs: true, // Enable file logging (default: false)
logPerDay: true, // Split logs by day (default: false)
})
);

// Dummy API endpoints

app.get("/", (req, res) => {
res.send("Hello, world!");
});

app.get("/api/users", (req, res) => {
res.send("Hello, users!");
});

app.listen(3000, () => {
console.log("Server running on port 3000");
});

Configuration Options

Log-Express provides several configuration options to tailor logging to your specific needs:

  • logLevel: Define the minimum log level (trace, debug, info, warn, error, fatal). Logs with a level below this threshold will be ignored.
    • Default: "info"
  • consoleLog: Enable or disable logging to the console.
    • Default: true
  • saveLogs: Enable saving logs to a file. Log files are stored in a logs directory at the project’s root.
    • Default: false
  • logPerDay: If enabled, logs will be split into daily files with filenames in the format system-YYYY-MM-DD.logs.
    • Default: false

Example Logs

When using Log-Express, your logs will look like this:

2024-09-18 14:32:07 [INFO]: GET /api/users 200 23ms - 192.168.1.1
2024-09-18 14:33:10 [WARN]: GET /api/orders 404 15ms - 192.168.1.2
2024-09-18 14:34:02 [ERROR]: POST /api/auth 500 45ms - 192.168.1.3
2024-09-18 14:35:45 [INFO]: PUT /api/products/123 204 34ms - 192.168.1.4

Logging Levels

Log-Express supports the following logging levels, which allow you to filter logs based on the severity of events:

  • trace: Detailed information useful for debugging.

  • debug: Debugging information.

  • info: Informational messages indicating normal operation

  • warn: Warnings that indicate potential issues but do not stop the execution of your application.

  • error: Errors that prevent part of your application from functioning correctly.

  • fatal: Critical issues that may cause the application to crash.

  • off: No logging will take place at this level.

Customization and Flexibility

Log-Express provides the ability to customize the log format and logging behavior to fit your application's needs. You can easily configure it to log important request data such as:

  • HTTP Methods: GET, POST, PUT, DELETE, etc.
  • Response Status Codes: Success, client errors, server errors.
  • Response Time: Duration of the request, measured in milliseconds.
  • Client IP Address: The origin IP of the request.

These details make it easier to track how your API is being used and detect issues early on.

Best Practices

  1. Log Levels in Production: In production environments, it's generally best to log at the info level or higher (e.g., warn, error) to reduce verbosity and focus on important events.
  2. Log Rotation: Enabling logPerDay ensures that your log files are manageable and easy to navigate. This is especially helpful for long-running applications.
  3. Monitoring Tools: Combine Log-Express with monitoring tools to gain deeper insights into your application’s performance and behavior.