An mutable object-based log format designed for chaining & objectMode streams.
npx @tessl/cli install tessl/npm-logform@2.7.0Logform is a mutable object-based log format library designed for chaining and objectMode streams. It serves as the core formatting engine for the winston logging library, providing a comprehensive set of composable formatting functions that transform log info objects through customizable pipelines.
npm install logformconst { format } = require('logform');For ES modules:
import { format } from 'logform';Individual formats can be imported directly:
const { format: { combine, timestamp, json } } = require('logform');const { format } = require('logform');
// Simple format usage
const timestampFormat = format.timestamp();
const info = timestampFormat.transform({
level: 'info',
message: 'Hello world'
});
// Combining multiple formats
const combinedFormat = format.combine(
format.timestamp(),
format.colorize(),
format.simple()
);
const formattedInfo = combinedFormat.transform({
level: 'info',
message: 'Hello world'
});Logform is built around several key concepts:
level, message, and optional metadata(info, opts) and return modified info or falsy valuestransform methodformat.combine()triple-beam packageThe library follows a functional approach where formats are composable transformation functions that can be combined to create sophisticated logging pipelines.
The main entry point for creating custom formats and accessing built-in formats.
/**
* Creates a new format from a transformation function
* @param {Function} transformFn - Function that takes (info, opts) and returns modified info
* @returns {Function} Format constructor function
*/
function format(transformFn);
/**
* Registers color configurations for log levels
* @param {Object} config - Configuration object with colors property
* @returns {Object} The configuration object
*/
function levels(config);Basic text formatting operations for messages and levels.
// Simple text formatting
function align();
function simple();
function printf(templateFn);Formats for structured data output including JSON and Logstash formats.
function json(opts);
function logstash();
function prettyPrint(opts);Color-related formatting for terminal and CLI output.
function colorize(opts);
function uncolorize(opts);
function cli(opts);Formats that add or organize metadata in log entries.
function timestamp(opts);
function label(opts);
function metadata(opts);
function ms();Advanced text processing and error handling capabilities.
function splat();
function errors(opts);
function padLevels(opts);Utilities for combining and organizing multiple formats.
function combine(...formats);// Info object structure (from TypeScript definitions)
interface TransformableInfo {
level: string;
message: unknown;
[LEVEL]?: string;
[MESSAGE]?: unknown;
[SPLAT]?: unknown;
[key: string | symbol]: unknown;
}
// Transform function signature
type TransformFunction = (info: TransformableInfo, opts?: unknown) => TransformableInfo | boolean;
// Format constructor function
type FormatWrap = (opts?: unknown) => Format;
// Base format class
class Format {
constructor(opts?: object);
options?: object;
transform: TransformFunction;
}Logform uses symbols from the triple-beam package for internal state:
const { LEVEL, MESSAGE, SPLAT } = require('triple-beam');
// Symbol.for('level') - Read-only level property
// Symbol.for('message') - Complete formatted message string
// Symbol.for('splat') - String interpolation arguments