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

string-processing.mddocs/

String Processing

Advanced text processing and error handling capabilities including string interpolation, error object handling, and level padding.

Capabilities

Splat Format

Transforms log messages by performing string interpolation using util.format, similar to printf-style formatting.

/**
 * Creates a splat format for string interpolation
 * @returns {Format} Splat format instance
 */
function splat();

The splat format processes string interpolation tokens like %s, %d, %j, etc., using Node.js util.format internally.

Usage Examples:

const { format } = require('logform');
const { SPLAT } = require('triple-beam');

const splatFormat = format.splat();

// Basic string interpolation
const result1 = splatFormat.transform({
  level: 'info',
  message: 'User %s logged in with ID %d',
  [SPLAT]: ['Alice', 123]
});
// Result: { level: 'info', message: 'User Alice logged in with ID 123', [SPLAT]: ['Alice', 123] }

// With metadata objects
const result2 = splatFormat.transform({
  level: 'info',
  message: 'Processing %s',
  [SPLAT]: ['payment', { userId: 456, amount: 99.99 }]
});
// Result: {
//   level: 'info',
//   message: 'Processing payment',
//   userId: 456,
//   amount: 99.99,
//   [SPLAT]: ['payment']
// }

// Format tokens
const examples = [
  '%s - string',
  '%d - number',
  '%j - JSON',
  '%% - literal percent'
];

Splatter Class

The internal class that handles string interpolation logic.

/**
 * Splatter class that handles string interpolation
 */
class Splatter {
  /**
   * @param {Object} opts - Splatter options
   */
  constructor(opts);
  
  /** Configuration options */
  options: Object;
  
  /**
   * Transform method that processes string interpolation
   * @param {Object} info - Log info object
   * @returns {Object} Info object with interpolated message
   */
  transform(info);
  
  /**
   * Internal method that performs actual string interpolation
   * @param {Object} info - Log info object
   * @param {string[]} tokens - Array of format tokens found in message
   * @returns {Object} Info object with interpolated message and metadata
   * @private
   */
  _splat(info, tokens);
}

Errors Format

Handles JavaScript Error objects in log entries, optionally including stack traces and error causes.

/**
 * Creates an errors format for handling Error objects
 * @param {Object} opts - Error handling options
 * @param {boolean} opts.stack - Include error stack trace
 * @param {boolean} opts.cause - Include error cause
 * @returns {Format} Errors format instance
 */
function errors(opts);

Usage Examples:

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

// Basic error handling
const errorsFormat = format.errors({ stack: true });

// Error as info object
const error = new Error('Something went wrong');
const result1 = errorsFormat.transform(error);
// Converts Error to info object with message and optional stack

// Error in message property
const result2 = errorsFormat.transform({
  level: 'error',
  message: new Error('Database connection failed')
});

// With stack trace and cause
const errorWithDetails = format.errors({ 
  stack: true, 
  cause: true 
});

const nestedError = new Error('Connection timeout');
const mainError = new Error('Database operation failed');
mainError.cause = nestedError;

const result3 = errorWithDetails.transform({
  message: mainError
});
// Result includes stack trace and cause information

Pad Levels Format

Pads log level names to uniform length for aligned output.

/**
 * Creates a pad levels format for uniform level width
 * @param {Object} opts - Padding options
 * @param {Object} opts.levels - Level configuration (default: configs.npm.levels)
 * @param {string} opts.filler - Padding character (default: ' ')
 * @returns {Format} Pad levels format instance
 */
function padLevels(opts);

Usage Examples:

const { format } = require('logform');
const { LEVEL, MESSAGE } = require('triple-beam');

const padLevelsFormat = format.padLevels();

const result1 = padLevelsFormat.transform({
  [LEVEL]: 'info',
  message: 'Short level'
});
// Result: { message: '    Short level', [LEVEL]: 'info' }

const result2 = padLevelsFormat.transform({
  [LEVEL]: 'error',
  message: 'Longer level'
});
// Result: { message: '   Longer level', [LEVEL]: 'error' }

// Custom filler and levels
const customPadding = format.padLevels({
  filler: '.',
  levels: {
    info: 0,
    warn: 1,
    error: 2,
    debug: 3
  }
});

Padder Class

The internal class that handles level padding calculations and application.

/**
 * Padder class that handles level padding
 */
class Padder {
  /**
   * @param {Object} opts - Padder options
   */
  constructor(opts);
  
  /** Padding mappings for each level */
  paddings: Object;
  
  /** Configuration options */
  options: Object;
  
  /**
   * Static method to get longest level name length
   * @param {Object} levels - Level configuration object
   * @returns {number} Maximum level name length
   */
  static getLongestLevel(levels);
  
  /**
   * Static method to calculate padding for a specific level
   * @param {string} level - Level name
   * @param {string} filler - Padding character
   * @param {number} maxLength - Maximum level length
   * @returns {string} Padding string
   */
  static paddingForLevel(level, filler, maxLength);
  
  /**
   * Static method to create padding mappings for all levels
   * @param {Object} levels - Level configuration
   * @param {string} filler - Padding character (default: ' ')
   * @returns {Object} Mapping of level to padding string
   */
  static paddingForLevels(levels, filler);
  
  /**
   * Transform method that applies padding to messages
   * @param {Object} info - Log info object
   * @param {Object} opts - Transform options
   * @returns {Object} Info object with padded message
   */
  transform(info, opts);
}

String Interpolation Tokens

The splat format supports these util.format tokens:

TokenTypeDescription
%sStringString conversion
%dNumberNumber conversion
%iIntegerInteger conversion
%fFloatFloat conversion
%jJSONJSON.stringify()
%oObjectObject inspection
%OObjectObject inspection with options
%%LiteralLiteral percent sign

Error Handling Options

interface ErrorOptions {
  /** Include error stack trace in output */
  stack?: boolean;
  
  /** Include error cause in output */
  cause?: boolean;
}

Pad Levels Options

interface PadLevelsOptions {
  /** Level configuration object (default: configs.npm.levels) */
  levels?: Record<string, number>;
  
  /** Character used for padding (default: ' ') */
  filler?: string;
}

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