npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mccann-hub/express-log-smith

v0.1.2

Published

A middleware library for Express.js, providing structured request/response logging, unique ID generation, and contextual loggers for enhanced observability in your applications.

Downloads

303

Readme

Express Log Smith

Express Log Smith is a middleware library for Express.js designed to enhance logging and request tracing. It simplifies request/response observability by integrating structured logging and unique identifiers like correlationId, traceId, and spanId into your application.

Features

  • Request ID Middleware: Automatically assigns correlationId, traceId, and spanId to each request.
  • Contextual Logger: Attaches a logger with enriched metadata to the request object for consistent, contextual logging.
  • Request Logger: Logs structured details of incoming requests and outgoing responses.
  • Customizable: Configure ID generation, log formats, and skip conditions as needed.
  • Plug-and-Play: Works seamlessly with any logger implementing the provided ILogger interface.

Installation

Install ExpressLogSmith via npm:

npm install @mccann-hub/express-log-smith

Basic Usage

A simple example integrating the middlewares into an Express.js application:

import express from "express";
import {
  dexter,
  requestId,
  requestLogger,
} from "@mccann-hub/express-log-smith";

// Import your logger (must implement ILogger)
import myLogger from "./myLogger";

const app = express();

// Add request ID middleware to generate unique IDs
app.use(requestId());

// Add contextual logger middleware
app.use(dexter(myLogger));

// Add request logging middleware
app.use(requestLogger(myLogger));

// Define routes
app.get("/", (req, res) => {
  req.logger.info("Processing request", { custom: "metadata" });
  res.send("Hello, world!");
});

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

Exported Middlewares

requestId(options?: RequestIdOptions)

Adds correlationId, traceId, and spanId to each request. These IDs help trace logs across distributed systems.

Options

  • correlation (optional): Configuration for the correlation ID.
    • headerName: The header name for the correlation ID (default: X-Correlation-Id).
    • generator: A custom ID generation function.
    • setHeader: Whether to include the correlation ID in the response headers (default: true).
  • trace (optional): Configuration for the trace ID.
    • Same properties as correlation.
  • span (optional): Configuration for the span ID.
    • headerName: The header name for the span ID (default: X-svc2svc-Id).
    • setHeader: Whether to include the span ID in the response headers (default: true).

Example

app.use(
  requestId({
    correlation: { headerName: "X-My-Correlation-Id" },
    trace: { generator: () => `trace-${Date.now()}` },
  }),
);

dexter(logger: ILogger)

Logs structured details of incoming requests and outgoing responses. The default format includes:

  • correlation_id
  • trace_id
  • span_id
  • HTTP method, content type, user agent, URI path
  • Response time and status

Customization

You can customize logging behavior by providing a skip function:

app.use(
  dexter(myLogger, {
    skip: (req) => req.url === "/health",
  }),
);

requestLogger(logger: ILogger)

Attaches a logger to the request object. The logger includes metadata from the request, such as correlationId, traceId, and spanId.

Requirements

The provided logger must implement the ILogger interface.

Example

app.use(requestLogger(myLogger));

app.get("/test", (req, res) => {
  req.logger.info("This is a test log");
  res.send("Logged successfully!");
});