1.0.3 • Published 7 months ago

perf-logx v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

performance-logger

A lightweight Express middleware to log HTTP requests and responses with colored output.

Features:

  • Logs HTTP methods, paths, status codes, and request duration.
  • Adds colors to improve readability.
  • Customizable logging behavior.

Installation

npm i perf-logx

Usage

Basic Setup

Here's how you can use perf-logx in a simple Express application:

const express = require("express");
const perfLogx = require("perf-logx");

const app = express();

// Use the perf-logx middleware
app.use(perfLogx());

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

app.listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});

Output

When a request is made to the server, you'll see a log like:

[INFO] GET / 200 0.003s
  • Method: GET
  • Route: /
  • Status Code: 200
  • Request Duration: 0.003 seconds (color-coded)

Example with Customization

You can also customize the logger behavior by passing options:

const express = require("express");
const performanceLogger = require("perf-logx");

const app = express();

// Use the logger middleware with customized options
app.use(
  performanceLogger({
    logIncoming: true, // Log incoming requests
    logStatusCode: true, // Log response status codes
    logDuration: true, // Log the request duration
    logMethod: true, // Log the HTTP method
    logPath: true, // Log the request path
    logIP: true, // Log the IP address of the requester
    logUserAgent: true, // Log the User-Agent of the requester
  })
);

// Example route
app.get("/example", (req, res) => {
  res.send("This is an example route.");
});

// Start the server
app.listen(3000, () => {
  console.log("Server is running on http://localhost:3000");
});

Configuration Options

OptionTypeDefaultDescription
logIncomingBooleantrueWhether to log incoming requests (enabled by default).
logStatusCodeBooleantrueWhether to log the response status code (enabled by default).
logDurationBooleantrueWhether to log the request duration (enabled by default).
logMethodBooleantrueWhether to log the HTTP method (enabled by default).
logPathBooleantrueWhether to log the request path (enabled by default).
logIPBooleanfalseWhether to log the IP address of the requester.
logUserAgentBooleanfalseWhether to log the User-Agent of the requester.

License

This project is licensed under the MIT License

1.0.3

7 months ago

1.0.4

7 months ago

1.0.3-docs.0

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago