/** * 输出信息接口,包含日志类型、消息内容和转换后的数据 * @template T 转换数据的类型 */ export interface OuputInfo { /** 日志类型,如 'info', 'debug', 'warn' 等 */ type: string; /** 原始消息数组 */ messages: any[]; /** 经过 transform 函数处理后的数据 */ transformData: T; } /** * 信息处理函数类型 * @template T 输入数据类型 * @template R 返回值类型 */ export type InfoFunc = (info: OuputInfo) => R; /** * 输出函数类型,用于处理最终的日志输出 * @template T 数据类型 */ export type OutputFunc = InfoFunc; /** * 适配器函数类型,根据日志类型返回对应的输出函数 * @template T 数据类型 * @param type 日志类型 * @returns 输出函数或null/undefined */ export type AdapterFunc = (type: string) => OutputFunc | null | undefined; export type SyncOutputAdapter = Record> | AdapterFunc; export type PromiseOutputAdapter = Promise>; /** * 输出适配器类型,可以是对象映射或适配器函数 * @template T 数据类型 */ export type OutputAdapter = SyncOutputAdapter | PromiseOutputAdapter; /** * Transform 函数的输入参数接口 */ export interface TransformProps { /** 日志类型 */ type: string; /** 原始消息数组 */ messages: any[]; /** 是否为嵌套调用,主调用为false,嵌套调用为true */ isNestingCall: boolean; /** 时间戳 */ timestamp: number; } /** * Logger 配置选项接口 * @template T Transform 函数返回的数据类型 * @template D Report 函数接收的数据类型,默认与T相同 */ export interface LoggerOptions { /** * 是否启用日志输出,默认为true * 当设置为false时,transform和report仍会执行,但不会调用输出适配器进行实际输出 * 支持传入函数进行动态控制,函数参数与report方法相同 * 这是一个输出控制开关,可以用于生产环境中禁用日志输出但保留数据处理逻辑 */ enableOutput?: boolean | ((options: TransformProps & { data: D; }) => boolean); /** * 数据转换函数,用于处理原始日志数据 * @param options 包含日志类型、消息和嵌套标识的参数 * @returns 转换后的数据或undefined */ transform?: ((options: TransformProps) => T | undefined) | null; /** * 数据上报函数,用于处理转换后的数据 * @param options 包含原始信息和转换后数据的参数 */ report?: ((options: TransformProps & { data: D; }) => void) | null; /** * 最大嵌套深度,默认为3 * 主调用不计入深度,嵌套调用从1开始计数 */ maxNestingDepth?: number; /** * 输出适配器数组,用于处理不同类型的日志输出 * 可以是对象映射数组或适配器函数数组 */ outputAdapters?: OutputAdapter[] | null; } /** * Logger 实例类型,支持动态属性访问 * 任何属性都会返回一个日志函数 */ export type Logger = Record void>; /** * 规范化类型工具,将所有属性变为必需且非空 * @template T 需要规范化的类型 */ export type Normalize> = { [K in keyof T]-?: NonNullable; }; /** * Logger 上下文接口,内部使用 * @template T 数据类型 */ export interface LoggerCtx { /** 规范化后的配置选项 */ options: Omit>, 'outputAdapters'> & { outputAdapters: AdapterFunc[]; enableOutput: NonNullable['enableOutput']> & ((...args: any[]) => boolean); }; }