import type { AnsiColors } from "@visulima/colorize"; import type { stringify } from "safe-stable-stringify"; import type { LiteralUnion, Primitive } from "type-fest"; import type InteractiveManager from "./interactive/interactive-manager.d.ts"; /** * Global namespace for extending Pail's metadata interface. * * This global declaration allows other packages and applications to extend * the Meta interface with custom properties by declaring additional properties * in the VisulimaPail.CustomMeta interface. * @example * ```typescript * declare global { * namespace VisulimaPail { * interface CustomMeta { * userId?: string; * requestId?: string; * } * } * } * ``` */ declare global { namespace VisulimaPail { interface CustomMeta { } } } /** * Metadata object containing all information about a log entry. * * This interface defines the structure of metadata that is passed to reporters * and processors. It contains all the contextual information about a log message * including the message itself, timing information, error details, and more. * @template L - The log level type */ export interface Meta extends VisulimaPail.CustomMeta { badge: string | undefined; context: any[] | undefined; date: Date | string; error: Error | undefined; groups: string[]; label: string | undefined; message: Primitive | ReadonlyArray | Record; prefix: string | undefined; repeated?: number | undefined; scope: string[] | undefined; suffix: string | undefined; traceError: Error | undefined; type: { level: ExtendedRfc5424LogLevels | L; name: string; }; } /** * Extended RFC 5424 Log Levels. * * Standard syslog severity levels as defined in RFC 5424, plus additional * levels commonly used in modern applications. Each level has a numeric * priority where lower numbers indicate higher severity. * @see https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1 */ export type ExtendedRfc5424LogLevels = "alert" | "critical" | "debug" | "emergency" | "error" | "informational" | "notice" | "trace" | "warning"; /** * Default Log Types. * * Predefined semantic log types that provide meaningful categorization * for different kinds of log messages. Each type has associated styling * and log level configuration. */ export type DefaultLogTypes = "alert" | "await" | "complete" | "critical" | "debug" | "emergency" | "error" | "info" | "log" | "notice" | "pending" | "start" | "stop" | "success" | "trace" | "wait" | "warn" | "warning" | "watch"; /** * Logger Function Type. * * Represents a logging function that can accept either a structured Message object * or multiple arguments in the traditional console.log style. */ export interface LoggerFunction { (message: Message): void; (...message: any[]): void; } /** * Logger Configuration. * * Configuration object that defines how a specific logger type should behave, * including its visual appearance and log level. * @template L - The log level type */ export interface LoggerConfiguration { badge?: string; color?: AnsiColors | undefined; label: string; logLevel: LiteralUnion; } /** * Logger Types Configuration. * * A record mapping logger type names to their configurations. * @template T - Custom logger type names * @template L - Log level types */ export type LoggerTypesConfig = Record>>; /** * Default Logger Types Configuration. * * A complete mapping of all default log types to their full configurations. * @template L - Log level types */ export type DefaultLoggerTypes = Record>; /** * Read-only Metadata. * * Immutable version of the Meta interface for use in reporters. * @template L - The log level type */ export type ReadonlyMeta = Readonly>; /** * Reporter Interface. * * Base interface for all reporters. Reporters are responsible for * outputting log messages to various destinations (console, files, etc.). * @template L - The log level type */ export interface Reporter { log: (meta: ReadonlyMeta) => void; } /** * Stream-Aware Reporter Interface. * * Extends Reporter with the ability to work with Node.js streams. * Used for server-side reporters that need to write to stdout/stderr. * @template L - The log level type */ export interface StreamAwareReporter extends Reporter { /** Set the stderr stream for error output */ setStderr: (stderr: NodeJS.WriteStream) => void; /** Set the stdout stream for standard output */ setStdout: (stdout: NodeJS.WriteStream) => void; } /** * Logger Types Aware Reporter Interface. * * Extends Reporter with the ability to receive logger type configurations. * Allows reporters to customize their output based on logger types. * @template T - Custom logger type names * @template L - The log level type */ export interface LoggerTypesAwareReporter extends Reporter { /** Set the logger types configuration */ setLoggerTypes: (types: LoggerTypesConfig & Partial>) => void; } /** * Stringify-Aware Reporter Interface. * * Extends Reporter with the ability to receive a custom stringify function. * Useful for reporters that need to serialize complex objects. * @template L - The log level type */ export interface StringifyAwareReporter extends Reporter { /** Set the stringify function for object serialization */ setStringify: (stringify: typeof JSON.stringify) => void; } /** * Interactive Stream Reporter Interface. * * Extends StreamAwareReporter with interactive capabilities for terminal UIs. * Supports features like progress bars and dynamic updates. * @template L - The log level type */ export interface InteractiveStreamReporter extends StreamAwareReporter { /** Set the interactive manager for handling interactive output */ setInteractiveManager: (manager?: InteractiveManager) => void; /** Enable or disable interactive mode */ setIsInteractive: (interactive: boolean) => void; } /** * Processor Interface. * * Base interface for all processors. Processors can modify or enhance * log metadata before it reaches reporters. * @template L - The log level type */ export interface Processor { /** Process the log metadata */ process: (meta: Meta) => Meta; } /** * Stringify-Aware Processor Interface. * * Extends Processor with the ability to receive a custom stringify function. * Useful for processors that need to serialize complex objects. * @template L - The log level type */ export interface StringifyAwareProcessor extends Processor { /** Set the stringify function for object serialization */ setStringify: (stringify: typeof JSON.stringify) => void; } export interface ConstructorOptions { disabled?: boolean; logLevel?: LiteralUnion; logLevels?: Partial> & Record; messages?: { timerEnd?: string; timerStart?: string; }; processors?: Processor[]; rawReporter?: Reporter; reporters?: Reporter[]; scope?: string[] | string; throttle?: number; throttleMin?: number; types?: LoggerTypesConfig & Partial>; } export interface ServerConstructorOptions extends ConstructorOptions { interactive?: boolean; stderr: NodeJS.WriteStream; stdout: NodeJS.WriteStream; } export type Message = { context?: any[] | undefined; message: any; prefix?: string; suffix?: string; }; /** * Internal interface for parent logger optimization properties. * * These properties are used internally to optimize child logger creation * by reusing parent logger's computed values when they haven't changed. * @internal */ export interface ParentLoggerOptimization { parentLogLevels?: Record; parentLongestLabel?: string; parentMessages?: { timerEnd: string; timerStart: string; }; parentStringify?: typeof stringify; parentTypes?: LoggerTypesConfig, L>; }