JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 149
  • Score
    100M100P100Q95301F
  • License MIT

Log file rotation transport for the LogLayer logging library.

Package Exports

  • @loglayer/transport-log-file-rotation

Readme

@loglayer/transport-log-file-rotation

NPM Version NPM Downloads TypeScript

A transport for the LogLayer logging library that writes logs to files with automatic rotation based on size or time. Built on top of file-stream-rotator.

Features

  • Automatic log file rotation based on time (hourly, daily)
  • Size-based rotation with support for KB, MB, and GB units
  • Support for date patterns in filenames using numerical values
  • Compression of rotated log files using gzip
  • Maximum file count or age-based retention
  • Automatic cleanup of old log files
  • Batch processing of logs for improved performance

Installation

# npm
npm i loglayer @loglayer/transport-log-file-rotation serialize-error

# pnpm
pnpm add loglayer @loglayer/transport-log-file-rotation serialize-error

# yarn
yarn add loglayer @loglayer/transport-log-file-rotation serialize-error

Usage

import { LogLayer } from "loglayer";
import { LogFileRotationTransport } from "@loglayer/transport-log-file-rotation";
import { serializeError } from "serialize-error";

const logger = new LogLayer({
  errorSerializer: serializeError,
  transport: [
    new LogFileRotationTransport({
      filename: "./logs/app.log",
    }),
  ],
});

logger.info("Application started");

Filename Uniqueness

Each instance of LogFileRotationTransport must have a unique filename to prevent possible race conditions. If you try to create multiple transport instances with the same filename, an error will be thrown. If you need multiple loggers to write to the same file, they should share the same transport instance:

// Create a single transport instance
const fileTransport = new LogFileRotationTransport({
  filename: "./logs/app-%DATE%.log",
  dateFormat: "YMD",
  frequency: "daily"
});

// Share it between multiple loggers
const logger1 = new LogLayer({
  transport: [fileTransport]
});

const logger2 = new LogLayer({
  transport: [fileTransport]
});

Child loggers do not have this problem as they inherit the transport instance from their parent logger.

Documentation

For detailed documentation, including all configuration options and advanced features, visit the documentation.