UNPKG

@loglayer/transport-simple-pretty-terminal

Version:

Pretty log output in the terminal / browser / Next.js for the LogLayer logging library.

243 lines (236 loc) 8.81 kB
import { LoggerlessTransportConfig, LoggerlessTransport, LogLayerTransportParams } from '@loglayer/transport'; import { ChalkInstance } from 'chalk'; export * from 'chalk'; /** * Configuration for log level colors. * Each log level can have its own chalk styling. */ interface ColorConfig { /** Style for trace level logs - lowest severity */ trace?: ChalkInstance; /** Style for debug level logs */ debug?: ChalkInstance; /** Style for info level logs - normal operation */ info?: ChalkInstance; /** Style for warning level logs */ warn?: ChalkInstance; /** Style for error level logs */ error?: ChalkInstance; /** Style for fatal level logs - highest severity */ fatal?: ChalkInstance; } /** * Configuration for log view styling. * Defines colors and styles for different log elements. */ interface ViewConfig { /** Color configuration for different log levels */ colors?: ColorConfig; /** Style for log entry IDs */ logIdColor?: ChalkInstance; /** Style for data values in structured data */ dataValueColor?: ChalkInstance; /** Style for data keys in structured data */ dataKeyColor?: ChalkInstance; } /** * Runtime environment for the transport. * Determines how logs are output. */ type Runtime = "node" | "browser"; /** * Theme configuration for the simple pretty terminal transport. * Defines styling for log output. */ interface SimplePrettyTerminalTheme extends ViewConfig { } /** * View modes for log display. * - inline: Shows all information with complete data structures inline (no truncation) * - message-only: Shows only the timestamp, log level and message for a cleaner output (no data shown) * - expanded: Shows timestamp, level, and message on first line, with data on indented separate lines */ type PrettyTerminalViewMode = "inline" | "message-only" | "expanded"; /** * Main configuration interface for SimplePrettyTerminalTransport. * Extends the base transport configuration with simple pretty terminal specific options. */ interface SimplePrettyTerminalConfig extends LoggerlessTransportConfig { /** Maximum depth for inline data display before collapsing. Default is 4. */ maxInlineDepth?: number; /** Custom theme configuration for log display */ theme?: SimplePrettyTerminalTheme; /** Whether the transport is enabled. If false, all operations will no-op. Defaults to true */ enabled?: boolean; /** View mode for log display. Defaults to "inline" */ viewMode?: PrettyTerminalViewMode; /** Whether to show log IDs in the output. Defaults to false */ showLogId?: boolean; /** Custom timestamp format. Can be a date-fns format string or a custom function. Defaults to "HH:mm:ss.SSS" */ timestampFormat?: string | ((timestamp: number) => string); /** Whether to collapse arrays in expanded mode. Defaults to true */ collapseArrays?: boolean; /** Whether to flatten nested objects with dot notation in inline mode. Defaults to true */ flattenNestedObjects?: boolean; /** Runtime environment for output */ runtime: Runtime; /** Whether to include data object as second parameter in browser console calls for better debugging. Defaults to false */ includeDataInBrowserConsole?: boolean; } /** * A transport for LogLayer that provides simple pretty terminal output. * This transport displays logs with theming and formatting but without interactive features. * * Features: * - Real-time log display with color-coded levels * - Configurable themes and colors * - Three view modes: inline, message-only, expanded * - JSON data pretty printing * - No interactive features (no keyboard navigation, no input) * - Browser and Node.js runtime support * * Usage: * ```typescript * const transport = new SimplePrettyTerminalTransport({ * maxInlineDepth: 4, * maxInlineLength: 120, * theme: customTheme, * viewMode: "inline", * runtime: "node" // or "browser" * }); * ``` */ /** * Main transport class that handles simple pretty terminal output. * This class provides the display functionality of PrettyTerminal without interactive features. * * The transport supports three view modes: * 1. Inline: Shows all information with complete data structures inline (no truncation) * 2. Message-only: Shows only the timestamp, log level and message for a cleaner output (no data shown) * 3. Expanded: Shows timestamp, level, and message on first line, with data on indented separate lines */ declare class SimplePrettyTerminalTransport extends LoggerlessTransport { /** Handles rendering and formatting of logs */ private renderer; /** Configuration options */ private config; /** * Creates a new SimplePrettyTerminalTransport instance. * * @param config - Configuration options for the transport */ constructor(config: SimplePrettyTerminalConfig); /** * Generates a random ID for each log entry. * Uses base36 encoding for compact, readable IDs. * * @returns A 6-character string ID * @example * "a1b2c3" // Example generated ID */ private generateId; /** * Main transport method that receives logs from LogLayer. * This method is called for each log event and handles: * - Generating a unique ID for the log * - Converting the log data to a storable format * - Rendering the log using the SimpleView renderer * * @param logLevel - The severity level of the log * @param messages - Array of message strings to be joined * @param data - Additional structured data to be logged * @param hasData - Whether the log includes additional data * @returns The original messages array */ shipToLogger({ logLevel, messages, data, hasData }: LogLayerTransportParams): any[]; /** * Changes the view mode for log display. * * @param viewMode - The new view mode to use */ setViewMode(viewMode: PrettyTerminalViewMode): void; /** * Gets the current view mode. * * @returns The current view mode */ getViewMode(): PrettyTerminalViewMode; } /** * Moonlight - A dark theme with cool blue tones * Inspired by moonlit nights and modern IDEs * * Color Palette: * - Primary: Cool blues and soft greens * - Accents: Warm yellows and soft reds * - Background: Assumes dark terminal (black or very dark grey) * * Best used with: * - Dark terminal themes * - Night-time coding sessions * - Environments where eye strain is a concern */ declare const moonlight: SimplePrettyTerminalTheme; /** * Sunlight - A light theme with warm tones * Inspired by daylight reading and paper documentation * * Color Palette: * - Primary: Deep, rich colors that contrast well with white * - Accents: Earth tones and deep jewel tones * - Background: Assumes light terminal (white or very light grey) * * Best used with: * - Light terminal themes * - Daytime coding sessions * - High-glare environments * - Printed documentation */ declare const sunlight: SimplePrettyTerminalTheme; /** * Neon - A dark theme with vibrant cyberpunk colors * Inspired by neon-lit cityscapes and retro-futuristic aesthetics * * Color Palette: * - Primary: Electric blues and hot pinks * - Accents: Bright purples and cyber greens * - Background: Assumes dark terminal (black or very dark grey) * * Best used with: * - Dark terminal themes * - High-contrast preferences * - Modern, tech-focused applications */ declare const neon: SimplePrettyTerminalTheme; /** * Nature - A light theme with organic, earthy colors * Inspired by forest landscapes and natural elements * * Color Palette: * - Primary: Deep forest greens and rich browns * - Accents: Autumn reds and golden yellows * - Background: Assumes light terminal (white or very light grey) * * Best used with: * - Light terminal themes * - Nature-inspired interfaces * - Applications focusing on readability */ declare const nature: SimplePrettyTerminalTheme; /** * Pastel - A soft, calming theme with gentle colors * Inspired by watercolor paintings and soft aesthetics * * Color Palette: * - Primary: Soft pastels and muted tones * - Accents: Gentle pinks and light blues * - Background: Assumes light terminal (white or very light grey) * * Best used with: * - Light terminal themes * - Long coding sessions * - Reduced visual stress */ declare const pastel: SimplePrettyTerminalTheme; declare function getSimplePrettyTerminal(config: SimplePrettyTerminalConfig): SimplePrettyTerminalTransport; export { type PrettyTerminalViewMode, type Runtime, type SimplePrettyTerminalConfig, type SimplePrettyTerminalTheme, SimplePrettyTerminalTransport, getSimplePrettyTerminal, moonlight, nature, neon, pastel, sunlight };