CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-logform

An mutable object-based log format designed for chaining & objectMode streams.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

core-formats.mddocs/

Core Format System

The core format system provides the foundation for creating custom formats and managing format instances.

Capabilities

Format Constructor

Creates a new format from a transformation function.

/**
 * Creates a new format from a transformation function
 * @param {Function} transformFn - Function that takes (info, opts) and returns modified info or falsy value
 * @returns {Function} Format constructor function that creates format instances
 */
function format(transformFn);

The transformation function must have exactly 2 parameters and should:

  • Accept an info object and opts object
  • Return a modified info object or falsy value to filter the log entry
  • Handle the transformation synchronously

Usage Examples:

const { format } = require('logform');

// Custom format that uppercases messages
const upperFormat = format((info, opts) => {
  if (opts.enable) {
    info.message = info.message.toUpperCase();
  }
  return info;
});

// Create format instance
const upperInstance = upperFormat({ enable: true });

// Use the format
const result = upperInstance.transform({
  level: 'info',
  message: 'hello world'
});
// Result: { level: 'info', message: 'HELLO WORLD' }

// Filtering example
const filterFormat = format((info, opts) => {
  if (info.private) {
    return false; // Filter out private messages
  }
  return info;
});

Level Configuration

Registers color configurations for log levels with the Colorizer class.

/**
 * Registers color configurations for log levels
 * @param {Object} config - Configuration object with colors property or direct color mapping
 * @returns {Object} The configuration object
 */
function levels(config);

Usage Examples:

const { levels } = require('logform');

// Register custom colors
const config = levels({
  colors: {
    info: 'blue',
    warn: 'yellow',
    error: 'red',
    debug: 'green'
  }
});

// Or directly with color mapping
levels({
  info: 'blue',
  warn: 'yellow'
});

Format Base Class

The base class that all format instances inherit from.

/**
 * Base format class with transform method
 */
class Format {
  /**
   * @param {Object} opts - Configuration options for the format
   */
  constructor(opts);
  
  /** Configuration options passed to constructor */
  options: Object;
  
  /**
   * Transform function that processes info objects
   * @param {Object} info - Log info object to transform
   * @param {Object} opts - Transform options
   * @returns {Object|boolean} Modified info object or false to filter
   */
  transform: Function;
}

Invalid Format Error

Error thrown when format functions have incorrect signatures.

/**
 * Error thrown when format function has invalid signature
 * Format functions must take exactly 2 arguments: (info, opts)
 */
class InvalidFormatError extends Error {
  constructor(formatFn);
}

Info Object Structure

The info object is the data structure that flows through format transformations:

/**
 * Basic info object structure
 */
interface InfoObject {
  /** Log level (required) */
  level: string;
  /** Log message (required) */
  message: any;
  /** Additional metadata properties */
  [key: string]: any;
}

/**
 * Symbol properties for internal state
 */
const SYMBOLS = {
  /** Read-only level symbol - Symbol.for('level') */
  LEVEL: Symbol,
  /** Formatted message symbol - Symbol.for('message') */
  MESSAGE: Symbol,
  /** Splat arguments symbol - Symbol.for('splat') */
  SPLAT: Symbol
};

Several formats add standard properties to info objects:

PropertyAdded ByDescription
splatsplat()String interpolation arguments
timestamptimestamp()Timestamp when message was received
labellabel()Custom label for the message
msms()Milliseconds since previous log

docs

colorization.md

core-formats.md

data-enhancement.md

format-composition.md

index.md

json-formats.md

string-processing.md

text-formatting.md

tile.json