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

colorization.mddocs/

Colorization

Color-related formatting for terminal and CLI output, providing ANSI color codes for levels and messages.

Capabilities

Colorize Format

Adds ANSI color codes to log levels and/or messages based on the log level.

/**
 * Creates a colorize format with configurable color options
 * @param {Object} opts - Colorization options
 * @param {boolean} opts.level - Apply color to level (default: false)
 * @param {boolean} opts.all - Apply color to both level and message (default: false)
 * @param {boolean} opts.message - Apply color to message (default: false)
 * @param {Object} opts.colors - Color mappings for log levels
 * @returns {Colorizer} Colorizer format instance
 */
function colorize(opts);

Usage Examples:

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

// Colorize just the level
const levelColorizer = format.colorize({ 
  level: true,
  colors: { info: 'blue', error: 'red' }
});

const result1 = levelColorizer.transform({
  [LEVEL]: 'info',
  level: 'info',
  message: 'Hello world'
});

// Colorize everything
const allColorizer = format.colorize({ 
  all: true,
  colors: { info: 'blue' }
});

const result2 = allColorizer.transform({
  [LEVEL]: 'info',
  level: 'info',
  message: 'Hello world'
}, { all: true });

// Multiple colors for a single level
const multiColorizer = format.colorize({
  colors: {
    info: 'blue bold',
    error: 'red underline'
  }
});

Colorizer Class

The advanced colorizer class with static and instance methods for color management.

/**
 * Colorizer class that handles ANSI color application
 */
class Colorizer extends Format {
  /**
   * @param {Object} opts - Colorizer options
   */
  constructor(opts);
  
  /**
   * Static method to add colors to all colorizer instances
   * @param {Object} colors - Color mappings object
   * @returns {Object} Updated color mappings
   */
  static addColors(colors);
  
  /**
   * Instance method to add colors to this colorizer
   * @param {Object} colors - Color mappings object
   * @returns {Object} Updated color mappings
   */
  addColors(colors);
  
  /**
   * Apply colors to text based on lookup key
   * @param {string} lookup - Level to look up colors for
   * @param {string} level - Level string (used if message not provided)
   * @param {string} message - Message to colorize (optional)
   * @returns {string} Colorized text
   */
  colorize(lookup, level, message);
  
  /**
   * Transform info object by applying colors
   * @param {Object} info - Log info object
   * @param {Object} opts - Transform options
   * @returns {Object} Colorized info object
   */
  transform(info, opts);
}

Usage Examples:

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

// Add global colors
format.colorize.addColors({
  info: 'blue',
  warn: 'yellow',
  error: 'red bold',
  debug: 'green dim'
});

// Create colorizer instance
const colorizer = format.colorize({ level: true });

// Use colorizer methods directly
const colorizedText = colorizer.colorize('info', 'info', 'Hello world');

Uncolorize Format

Strips ANSI color codes from log info objects.

/**
 * Creates an uncolorize format that strips ANSI color codes
 * @param {Object} opts - Uncolorize options
 * @param {boolean} opts.level - Process level field (default: true)
 * @param {boolean} opts.message - Process message field (default: true)
 * @param {boolean} opts.raw - Process MESSAGE symbol field (default: true)
 * @returns {Format} Uncolorize format instance
 */
function uncolorize(opts);

Usage Examples:

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

const uncolorizeFormat = format.uncolorize();

// Strip colors from all fields
const result = uncolorizeFormat.transform({
  level: '\u001b[34minfo\u001b[39m', // Blue 'info'
  message: '\u001b[34mHello world\u001b[39m', // Blue 'Hello world'
  [MESSAGE]: '\u001b[34minfo: Hello world\u001b[39m' // Blue formatted message
});
// Result: { level: 'info', message: 'Hello world', [MESSAGE]: 'info: Hello world' }

// Selective uncolorizing
const selectiveUncolorize = format.uncolorize({
  level: true,
  message: false, // Keep message colors
  raw: true
});

CLI Format

Combines colorization and level padding for CLI-style output.

/**
 * Creates a CLI format combining colorization and padding
 * @param {Object} opts - CLI format options (combines ColorizeOptions and PadLevelsOptions)
 * @param {Object} opts.levels - Level configuration (default: configs.cli.levels)
 * @param {Object} opts.colors - Color mappings for levels
 * @param {boolean} opts.level - Apply color to level
 * @param {boolean} opts.all - Apply color to both level and message
 * @param {boolean} opts.message - Apply color to message
 * @returns {CliFormat} CLI format instance
 */
function cli(opts);

Usage Examples:

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

const cliFormat = format.cli({ 
  colors: { info: 'blue' },
  all: true
});

const result = cliFormat.transform({
  [LEVEL]: 'info',
  level: 'info',
  message: 'Hello world'
}, { all: true });

console.log(result[MESSAGE]);
// Colorized output: "info:    Hello world" (with blue coloring and padding)

CLI Format Class

The internal CLI format class that combines colorization and padding.

/**
 * CLI format class that handles colorization and padding
 */
class CliFormat {
  /**
   * @param {Object} opts - CLI format options
   */
  constructor(opts);
  
  /** Colorizer instance for color application */
  colorizer: Colorizer;
  
  /** Padder instance for level padding */
  padder: Padder;
  
  /** Configuration options */
  options: Object;
  
  /**
   * Transform method that applies both padding and colors
   * @param {Object} info - Log info object
   * @param {Object} opts - Transform options
   * @returns {Object} Formatted info object
   */
  transform(info, opts);
}

Color Configuration

Colors can be specified as:

  • Single color names: 'blue', 'red', 'green'
  • Space-separated multiple colors: 'blue bold', 'red underline'
  • Arrays: ['blue', 'bold']

Available colors include standard ANSI colors and styles from the @colors/colors package.

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