The colorization system provides terminal color support for log levels and message content. It uses the Colorette library under the hood and supports custom color schemes for different log levels.
Creates a colorizer function for applying terminal colors to log levels and messages.
/**
* Factory function to create a colorizer for log levels and messages
* @param useColors - Enable terminal colors (default: false)
* @param customColors - Array of [level, colorName] tuples for custom colors
* @param useOnlyCustomProps - Only use custom colors without fallback (default: false)
* @returns Colorizer function with message/property colorization methods
*/
function colorizerFactory(
useColors?: boolean,
customColors?: [number, string][],
useOnlyCustomProps?: boolean
): ColorizerFunc;
interface ColorizerFunc {
/**
* Colorize a log level
* @param level - Level number (10-60) or level name string
* @param opts - Optional custom levels configuration
* @returns Colorized level label string
*/
(level: number | string, opts?: {
customLevels?: { [level: number]: string };
customLevelNames?: { [name: string]: number };
}): string;
/**
* Colorize message text (cyan by default)
* @param input - Text to colorize
* @returns Colorized text
*/
message(input: string | number): string;
/**
* Colorize with grey color
* @param input - Text to colorize
* @returns Grey colorized text
*/
greyMessage(input: string | number): string;
/**
* Colorize property names (magenta by default)
* @param input - Text to colorize
* @returns Colorized property name
*/
property(input: string | number): string;
/**
* Colorette instance with all color functions
*/
colors: Colorette;
}
interface Colorette {
isColorSupported: boolean;
reset: (str: string) => string;
bold: (str: string) => string;
dim: (str: string) => string;
italic: (str: string) => string;
underline: (str: string) => string;
inverse: (str: string) => string;
hidden: (str: string) => string;
strikethrough: (str: string) => string;
black: (str: string) => string;
red: (str: string) => string;
green: (str: string) => string;
yellow: (str: string) => string;
blue: (str: string) => string;
magenta: (str: string) => string;
cyan: (str: string) => string;
white: (str: string) => string;
gray: (str: string) => string;
bgBlack: (str: string) => string;
bgRed: (str: string) => string;
bgGreen: (str: string) => string;
bgYellow: (str: string) => string;
bgBlue: (str: string) => string;
bgMagenta: (str: string) => string;
bgCyan: (str: string) => string;
bgWhite: (str: string) => string;
}Usage Examples:
const { colorizerFactory } = require('pino-pretty');
// Create colorizer with default colors
const colorizer = colorizerFactory(true);
// Colorize log levels
console.log(colorizer(30)); // Green "INFO"
console.log(colorizer(50)); // Red "ERROR"
console.log(colorizer(60)); // Red with background "FATAL"
// Colorize messages
console.log(colorizer.message('This is a message')); // Cyan text
// Colorize property names
console.log(colorizer.property('userId')); // Magenta text
// Use Colorette functions directly
console.log(colorizer.colors.bold('Bold text'));
console.log(colorizer.colors.red('Red text'));Standard colors for different log levels.
/**
* Default color mapping:
* - TRACE (10): gray
* - DEBUG (20): blue
* - INFO (30): green
* - WARN (40): yellow
* - ERROR (50): red
* - FATAL (60): bgRed (red background)
* - USERLVL (default): white
* - messages: cyan
* - properties: magenta
* - grey messages: gray
*/Usage Example:
const { colorizerFactory } = require('pino-pretty');
const colorizer = colorizerFactory(true);
// Default colors
console.log(colorizer(10)); // Gray "TRACE"
console.log(colorizer(20)); // Blue "DEBUG"
console.log(colorizer(30)); // Green "INFO"
console.log(colorizer(40)); // Yellow "WARN"
console.log(colorizer(50)); // Red "ERROR"
console.log(colorizer(60)); // Red background "FATAL"
// Works with level names too
console.log(colorizer('info')); // Green "INFO"
console.log(colorizer('error')); // Red "ERROR"Define custom colors for specific log levels.
/**
* Custom colors are specified as array of [level, colorName] tuples
* Available color names: black, red, green, yellow, blue, magenta,
* cyan, white, gray, bgRed, bgGreen, etc.
*/Usage Examples:
const { colorizerFactory } = require('pino-pretty');
// Custom colors for specific levels
const customColorizer = colorizerFactory(
true,
[
[30, 'blue'], // INFO in blue
[40, 'magenta'], // WARN in magenta
[50, 'bgRed'] // ERROR with red background
],
false // Fall back to default colors for other levels
);
console.log(customColorizer(30)); // Blue "INFO"
console.log(customColorizer(40)); // Magenta "WARN"
console.log(customColorizer(20)); // Default blue "DEBUG"
// Use only custom colors (no fallback)
const strictColorizer = colorizerFactory(
true,
[
[30, 'cyan'],
[50, 'red']
],
true // Only use custom colors
);
console.log(strictColorizer(30)); // Cyan "INFO"
console.log(strictColorizer(40)); // White "WARN" (default fallback)Create a no-op colorizer that returns text unchanged.
/**
* When useColors is false, colorizer returns text unchanged
*/Usage Example:
const { colorizerFactory } = require('pino-pretty');
// Plain text colorizer (no colors)
const plainColorizer = colorizerFactory(false);
console.log(plainColorizer(30)); // "INFO" (no colors)
console.log(plainColorizer.message('text')); // "text" (no colors)
// Still provides Colorette instance (disabled)
console.log(plainColorizer.colors.isColorSupported); // falseCheck if the terminal supports colors.
/**
* Boolean indicating if the current terminal supports colors
* Re-exported from Colorette library
*/
const isColorSupported: boolean;Usage Example:
const { isColorSupported } = require('pino-pretty');
if (isColorSupported) {
console.log('Terminal supports colors');
// Use colorizer with colors enabled
} else {
console.log('Terminal does not support colors');
// Use colorizer with colors disabled
}
// isColorSupported is also available on colorizer
const { colorizerFactory } = require('pino-pretty');
const colorizer = colorizerFactory(true);
console.log(colorizer.colors.isColorSupported);Colorize custom log levels.
/**
* Colorizer supports custom levels via opts parameter
*/Usage Example:
const { colorizerFactory } = require('pino-pretty');
// Create colorizer with custom level colors
const colorizer = colorizerFactory(
true,
[
[99, 'bgRed'], // Custom severe level
[5, 'gray'] // Custom verbose level
]
);
// Use with custom levels
const result = colorizer(99, {
customLevels: { 99: 'SEVERE', 5: 'VERBOSE' },
customLevelNames: { severe: 99, verbose: 5 }
});
console.log(result); // Red background "SEVERE"
// Can also pass string level
const result2 = colorizer('severe', {
customLevels: { 99: 'SEVERE' },
customLevelNames: { severe: 99 }
});
console.log(result2); // Red background "SEVERE"Colorizer is automatically used when colorize option is enabled.
/**
* When using build() or prettyFactory() with colorize option:
* - colorize: true enables default colors
* - customColors: configures custom color scheme
* - useOnlyCustomProps: controls fallback behavior
*/Usage Example:
const pinoPretty = require('pino-pretty');
// Automatic colorization
const stream = pinoPretty({
colorize: true, // Enables colorization
customColors: 'info:blue,error:bgRed', // Custom colors
useOnlyCustomProps: false // Allow fallback to defaults
});
// Manual colorizer usage
const { colorizerFactory } = require('pino-pretty');
const colorizer = colorizerFactory(true);
// Use in custom prettifier
const customPrettifier = {
customPrettifiers: {
level: (level, key, log, { colors }) => {
return colors.bold(colorizer(level));
}
}
};All Colorette color functions are accessible.
/**
* Text colors: black, red, green, yellow, blue, magenta, cyan, white, gray
* Background colors: bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
* Modifiers: bold, dim, italic, underline, inverse, hidden, strikethrough, reset
*/Usage Example:
const { colorizerFactory } = require('pino-pretty');
const colorizer = colorizerFactory(true);
const colors = colorizer.colors;
// Text colors
console.log(colors.red('Error text'));
console.log(colors.green('Success text'));
console.log(colors.yellow('Warning text'));
// Background colors
console.log(colors.bgRed('Critical'));
console.log(colors.bgGreen('OK'));
// Modifiers
console.log(colors.bold('Bold text'));
console.log(colors.dim('Dim text'));
console.log(colors.underline('Underlined'));
// Combine multiple
console.log(colors.bold(colors.red('Bold Red')));
console.log(colors.bgYellow(colors.black('Black on Yellow')));