@cmtlyt/logger
Version:
44 lines (41 loc) • 1.62 kB
text/typescript
type Kind = 'info' | 'warn' | 'error' | 'debug' | 'success';
type LoggerConfigObj = Record<Kind, LoggerConfig>;
type Logger = Record<Kind, (...args: unknown[]) => void>;
interface StyleConfig {
tagColor?: string;
tagBg?: string;
contentColor?: string;
contentBg?: string;
borderColor?: string;
style?: (config: LoggerConfig) => {
tagStyle: string;
contentStyle: string;
};
}
interface LoggerConfig<T extends string = Kind> extends StyleConfig {
kind: Kind | T;
inherit?: Kind | T;
noOutput?: boolean;
needTrace?: boolean;
}
interface LogEvent<T extends string = Kind> {
kind: Kind | T;
messages: unknown[];
logConf: LoggerConfig<T>;
preventDefault: () => void;
changeMessages: <M extends any[]>(newMessages: M) => void;
}
type LoggerOptions<T extends string = Kind, E = unknown> = {
needTrace?: boolean;
noOutput?: boolean;
logConfig?: Partial<LoggerConfigObj> & {
[K in T]: LoggerConfig<T>;
};
/** 该配置会覆盖 getPrintFunc 配置, 推荐优先使用 getPrintFunc */
printFunc?: ((...args: unknown[]) => void) | null;
getPrintFunc?: (this: LoggerOptions<T, E>, kind: Kind | T) => ((...args: unknown[]) => void) | null;
onLogBefore?: (this: LoggerOptions<T, E>, event: LogEvent<T>) => void;
} & E;
declare function createLogger<T extends string, E = unknown>(options?: LoggerOptions<T, E>): Logger & Record<T, (...args: unknown[]) => void>;
declare const logger: Logger & Record<string, (...args: unknown[]) => void>;
export { type Kind, type Logger, type LoggerOptions, createLogger, logger };