or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-tool.mdcolorization.mdconfiguration.mdcustom-prettifiers.mdindex.mdmessage-formatting.mdprettifier-factory.mdstream-transport.md
tile.json

prettifier-factory.mddocs/

Prettifier Factory

The prettyFactory function creates a stateless prettifier function for transforming log objects or JSON strings into prettified log lines without using streams. This is useful for custom integrations or when you need direct control over log formatting.

Capabilities

Prettifier Factory Function

Creates a function that accepts log data and returns prettified strings.

/**
 * Creates a prettifier function for transforming log data
 * @param options - Configuration for the prettifier
 * @returns Function that accepts log data and returns prettified strings
 */
function prettyFactory(options: PrettyOptions): LogPrettifierFunc;

/**
 * The prettifier function returned by prettyFactory
 * @param inputData - A log string (JSON) or log object
 * @returns Prettified log string with line ending
 */
type LogPrettifierFunc = (inputData: string | object) => string;

Usage Examples:

const { prettyFactory } = require('pino-pretty');

// Create a prettifier with custom options
const prettifier = prettyFactory({
  colorize: true,
  translateTime: 'HH:MM:ss',
  ignore: 'pid,hostname'
});

// Use with JSON string input
const logLine = '{"level":30,"time":1522431328992,"msg":"hello world","pid":42,"hostname":"foo"}';
const pretty = prettifier(logLine);
console.log(pretty);
// Output: [10:30:45] INFO: hello world

// Use with object input
const logObject = {
  level: 30,
  time: Date.now(),
  msg: 'user logged in',
  userId: 123
};
const prettyObj = prettifier(logObject);
console.log(prettyObj);
// Output: [10:32:15] INFO: user logged in
//     userId: 123

Custom Integration Example

Integrate the prettifier into custom logging systems.

// Example: Custom log processing pipeline
const { prettyFactory } = require('pino-pretty');

const prettifier = prettyFactory(options);

function processLogLine(line) {
  const prettyLine = prettifier(line);
  // Send to custom destination
  return prettyLine;
}

Usage Example:

const { prettyFactory } = require('pino-pretty');
const fs = require('fs');

// Create prettifier
const prettifier = prettyFactory({
  colorize: false,
  translateTime: 'SYS:standard',
  singleLine: true
});

// Process log file line by line
const readline = require('readline');
const rl = readline.createInterface({
  input: fs.createReadStream('./logs/app.ndjson'),
  crlfDelay: Infinity
});

rl.on('line', (line) => {
  const pretty = prettifier(line);
  console.log(pretty);
});

Passthrough for Non-JSON

Non-JSON input is returned with a line ending appended.

/**
 * Behavior for non-JSON input:
 * - If input cannot be parsed as JSON, returns input + EOL unchanged
 * - If parsed JSON is not an object, returns input + EOL unchanged
 */

Behavior:

const { prettyFactory } = require('pino-pretty');

const prettifier = prettyFactory({ colorize: true });

// Valid JSON log
const validLog = prettifier('{"level":30,"msg":"hello"}');
// Returns prettified output

// Invalid JSON
const invalidJson = prettifier('this is not json');
// Returns: "this is not json\n"

// Valid JSON but not a log object
const notObject = prettifier('"just a string"');
// Returns: "\"just a string\"\n"

Minimum Level Filtering

The prettifier can filter out logs below a specified level.

/**
 * When minimumLevel is set, logs below that level return undefined
 * This allows filtering in the prettifier itself
 */

Usage Example:

const { prettyFactory } = require('pino-pretty');

const prettifier = prettyFactory({
  minimumLevel: 'info',  // Filter out trace and debug
  colorize: true
});

// This returns prettified output
const infoLog = prettifier({ level: 30, msg: 'info message' });
console.log(infoLog); // [10:30:45] INFO: info message

// This returns undefined (filtered out)
const debugLog = prettifier({ level: 20, msg: 'debug message' });
console.log(debugLog); // undefined

// Handle filtering in your code
function processLog(logData) {
  const result = prettifier(logData);
  if (result !== undefined) {
    console.log(result);
  }
}

Stateless Operation

Each prettifier function is stateless and thread-safe.

/**
 * Prettifier functions are stateless:
 * - Configuration is bound at creation time
 * - Same prettifier can be called concurrently
 * - Safe to use across multiple threads/workers
 */

Usage Example:

const { prettyFactory } = require('pino-pretty');

// Create once, use many times
const prettifier = prettyFactory({ colorize: true });

// Can be called concurrently
const logs = [
  '{"level":30,"msg":"log 1"}',
  '{"level":40,"msg":"log 2"}',
  '{"level":30,"msg":"log 3"}'
];

// Process in parallel (example with worker threads)
const results = logs.map(log => prettifier(log));

// Safe to share across workers
const { Worker } = require('worker_threads');
// Pass prettifier configuration to workers, not the function itself
const config = { colorize: true, translateTime: 'HH:MM:ss' };
// Workers create their own prettifier with same config

Integration with Custom Options

Combine with all PrettyOptions for full control.

// All PrettyOptions are supported:
// - Display options (colorize, singleLine, hideObject, etc.)
// - Key mapping (messageKey, levelKey, timestampKey, etc.)
// - Formatting (translateTime, messageFormat, errorProps, etc.)
// - Filtering (minimumLevel, ignore, include)
// - Custom prettifiers and colors

Usage Example:

const { prettyFactory } = require('pino-pretty');

// Complex configuration
const prettifier = prettyFactory({
  // Display
  colorize: true,
  levelFirst: true,
  singleLine: false,

  // Custom keys
  messageKey: 'message',
  levelKey: 'severity',
  timestampKey: 'timestamp',

  // Formatting
  translateTime: 'SYS:yyyy-mm-dd HH:MM:ss',
  messageFormat: '{levelLabel} [{pid}] {msg}',

  // Filtering
  ignore: 'hostname,pid',
  minimumLevel: 'info',

  // Custom prettifiers
  customPrettifiers: {
    time: (timestamp) => new Date(timestamp).toISOString(),
    userId: (id) => `USER:${id}`
  }
});

const log = {
  severity: 30,
  timestamp: Date.now(),
  message: 'User action',
  userId: 12345,
  hostname: 'server1',
  pid: 5678
};

console.log(prettifier(log));
// Output: INFO [5678] User action
//     userId: USER:12345