An mutable object-based log format designed for chaining & objectMode streams.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The core format system provides the foundation for creating custom formats and managing format instances.
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:
info object and opts objectinfo object or falsy value to filter the log entryUsage 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;
});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'
});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;
}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);
}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:
| Property | Added By | Description |
|---|---|---|
splat | splat() | String interpolation arguments |
timestamp | timestamp() | Timestamp when message was received |
label | label() | Custom label for the message |
ms | ms() | Milliseconds since previous log |