/** * The set of value types a logger method can accept and emit. * * The `unknown` member was intentionally dropped: a union containing `unknown` * collapses to `unknown`, which silently widened every output type and defeated * the generic inference the logger relies on. The remaining members cover every * value the built-in loggers produce (strings, numbers, booleans, and arbitrary * objects — including serialized `Error`s). */ type SupportedOutput = boolean | number | object | string; /** * Default shape of the contextual data carried by a logger. Any object keyed by * string is accepted; richer context types narrow this via the generic * parameters on {@link Logger} and {@link createLogger}. */ type DefaultContext = Record; /** * A message that is computed lazily from the logger's current context. Receives * the context and returns the value to log, so the value can depend on whatever * context was attached via {@link Logger.withContext}. * * @template TContext - The context shape passed to the message factory * @template TOutput - The value type the factory produces */ type ContextualLoggerMessage = (context: TContext) => TOutput; /** * A single argument accepted by a log method: either a plain value to log or a * {@link ContextualLoggerMessage} that derives the value from the context. * * @template TContext - The context shape available to contextual messages * @template TOutput - The value type produced */ type LoggerMessage = ContextualLoggerMessage | TOutput; /** * Call signature of a log method on a built logger. Accepts any number of * messages (plain or contextual) and returns nothing. * * @template TContext - The context shape available to contextual messages * @template TOutput - The value type each message produces */ interface LogMethod { (...messages: LoggerMessage[]): void; } /** * Low-level handler backing a single log method. Unlike {@link LogMethod}, it * receives the resolved context explicitly as its first argument, which is what * lets middleware wrap and transform it. * * @template TOutput - The value type each message produces */ type MethodHandler = (context: DefaultContext, ...messages: LoggerMessage[]) => void; /** * Map of method name to {@link MethodHandler}. This is the definition object * passed to {@link createLogger} to describe the methods a logger exposes. * * @template TOutput - The value type each handler produces */ type MethodDefinitions = Record>; type MethodsFromDefinitions> = { [K in keyof TDefs]: TDefs[K] extends MethodHandler ? LogMethod : never; }; /** * A middleware wrapping a {@link MethodHandler}. Given the next handler in the * chain, it returns a new handler, allowing it to transform the context or * messages before (or after) delegating to `next`. * * @template TOutput - The value type the wrapped handler produces */ type Middleware = (next: MethodHandler) => MethodHandler; type MiddlewaresFor> = { [K in keyof TDefs]?: TDefs[K] extends MethodHandler ? Middleware : never; }; /** * A fully built logger: every method from its definitions plus the * {@link Logger.withMiddleware} and {@link Logger.withContext} combinators for * deriving new loggers. * * @template TContext - The context shape carried by this logger * @template TDefs - The method definitions backing this logger */ type Logger> = MethodsFromDefinitions & { /** * Returns a new logger whose listed methods are wrapped by the given * middleware. Methods not listed are left untouched, and the original logger * is not modified. */ withMiddleware: (middlewares: MiddlewaresFor) => Logger; /** * Returns a new logger with `context` merged into the existing context. The * original logger is not modified, so derived loggers can be created freely. */ withContext: (context: TNewContext) => Logger; }; type ExtractMethodDefs = { [K in keyof T as K extends "withContext" | "withMiddleware" ? never : T[K] extends LogMethod ? K : never]: T[K] extends LogMethod ? MethodHandler : never; }; /** * Re-derives a logger's public shape for a given context from the logger type * itself (rather than from its method definitions). Useful for typing values * that hold an already-built logger while preserving its method signatures and * the {@link Logger.withMiddleware} / {@link Logger.withContext} combinators. * * @template TContext - The context shape to expose * @template TLogger - The source logger type to extract methods from */ type LoggerWithContext = { [K in keyof ExtractMethodDefs]: ExtractMethodDefs[K] extends MethodHandler ? LogMethod : never; } & { withMiddleware: (middlewares: { [K in keyof ExtractMethodDefs]?: ExtractMethodDefs[K] extends MethodHandler ? Middleware : never; }) => LoggerWithContext; withContext: (context: TNewContext) => LoggerWithContext; }; /** * Type guard that narrows a {@link LoggerMessage} to a * {@link ContextualLoggerMessage}. Used by logger implementations to decide * whether a message must be invoked with the context or logged as-is. * * @template TContext - The context shape available to contextual messages * @template TOutput - The value type produced * @param message - The message to inspect * @returns `true` when the message is a context-consuming function * @example * ```typescript * const resolved = isContextualMessage(message) ? message(context) : message * ``` */ declare function isContextualMessage(message: LoggerMessage): message is ContextualLoggerMessage; /** * Creates a logger from a map of method definitions. Each definition becomes a * callable method on the returned logger, and the logger starts with an empty * context that can be extended via {@link Logger.withContext}. * * @template TDefs - The method definitions backing the logger * @param methods - Map of method name to its {@link MethodHandler} * @returns A logger exposing the defined methods plus `withContext` and `withMiddleware` * @example * ```typescript * const logger = createLogger({ * info: (context, ...messages) => console.info(...messages), * }) * * logger.withContext({ requestId: "req-1" }).info("started") * ``` */ declare function createLogger>(methods: TDefs): Logger<{}, TDefs>; export { createLogger, isContextualMessage }; export type { DefaultContext, Logger, LoggerMessage, LoggerWithContext, MethodHandler, SupportedOutput }; //# sourceMappingURL=logger.d.ts.map