Skip to main content

Introduction

HTTP Ledger provides extensive configuration options to customize logging behavior, security, and performance. All options are passed as a single configuration object to the logger() function.

Basic Setup

const express = require('express');
const logger = require('http-ledger');

const app = express();

// Use with default settings
app.use(logger());

// Or with custom configuration
app.use(logger({
  logBody: true,
  logResponse: true,
  maskFields: ['password', 'token']
}));

Configuration Categories

HTTP Ledger options are organized into several categories:

Basic Options

Control what data gets logged: bodies, headers, query parameters

Security

Protect sensitive data with field masking and header filtering

Custom Logging

Customize log levels and formats with custom functions

Request Tracking

Enable request ID generation for distributed tracing

Selective Logging

Control which requests get logged with sampling and filters

External Integrations

Integrate with IP geolocation and external logging services

Complete Configuration Example

Here’s an example showing all available options:
import logger, { ApiLoggerOptions, LogData, LogLevel } from 'http-ledger';

const config: ApiLoggerOptions = {
  // Basic options
  logBody: true,
  logResponse: true,
  logQueryParams: true,
  excludedHeaders: ['authorization', 'cookie'],

  // Security
  maskFields: ['password', 'token', 'secret', 'apiKey'],

  // Custom logging
  customLogLevel: (logData: LogData): LogLevel => {
    if (logData.statusCode >= 500) return 'error';
    if (logData.statusCode >= 400) return 'warn';
    return 'info';
  },
  customFormatter: (logData: LogData) => ({
    ...logData,
    environment: process.env.NODE_ENV,
    service: 'my-api'
  }),

  // Request tracking
  autoGenerateRequestId: true,

  // Selective logging
  shouldLog: (req, res) => req.path !== '/health',
  logSampling: 0.1, // Log 10% of requests

  // External integrations
  getIpInfo: async (ip: string) => {
    const response = await fetch(`https://ipapi.co/${ip}/json/`);
    return response.json();
  },
  onLog: async (logData: LogData) => {
    // Send to external service
    await fetch('https://logs.example.com/api', {
      method: 'POST',
      body: JSON.stringify(logData)
    });
  }
};

app.use(logger(config));

Configuration Best Practices

Start with default settings and gradually add configurations as needed. Not all applications require all features.

Development Environment

app.use(logger({
  logBody: true,
  logResponse: true,
  logQueryParams: true,
  excludedHeaders: ['user-agent']
}));

Production Environment

app.use(logger({
  logBody: false, // Reduce log volume
  logResponse: false,
  maskFields: ['password', 'token', 'secret'],
  logSampling: 0.1, // Sample 10% of requests
  autoGenerateRequestId: true,
  onLog: async (logData) => {
    // Send to centralized logging
    await loggingService.send(logData);
  }
}));

Next Steps

Explore specific configuration categories to learn more:

Basic Options

Control logging behavior

Security

Protect sensitive data

Performance

Optimize for production

Build docs developers (and LLMs) love