UNPKG

takin

Version:

Front end engineering base toolchain and scaffold

152 lines (151 loc) 3.96 kB
import chalk from 'chalk'; import debug from 'debug'; import { CliTableOptions } from './table'; export * from './table'; export declare type LogType = 'error' | 'warn' | 'info' | 'success'; export declare type LogLevel = LogType | 'silent'; export interface Logger { /** * 初始化 logger, 多次调用会重复初始化同一个 logger */ init(level: LogLevel, options: LoggerOptions): void; /** * 返回携带 特定 options 的 Logger 实例 */ withOptions(options: LogOptions): Logger; /** * 创建 loading 日志对象 */ createLoading(msg: any, options?: LogOptions): LoadingLogger; /** * info 日志输出 */ info(msg: any, options?: LogOptions): void; /** * success 日志输出 */ success(msg: any, options?: LogOptions): void; /** * warn 日志输出 */ warn(msg: any, options?: LogOptions): void; /** * warn 日志输出, 相同信息只输出一次 */ warnOnce(msg: any, options?: LogOptions): void; /** * error 日志输出 */ error(msg: any, options?: LogErrorOptions): void; /** * deprecate 日志输出 */ deprecate(deprecatedMsg: any, hint: any, error?: Error): void; /** * debug 日志输出, 基于 debug npm */ debug(msg: any, ...args: any[]): void; /** * table 表格输出 */ table(tableOptions: CliTableOptions, type?: LogType, options?: LogErrorOptions): void; /** * 耗时性能日志输出, 需要配合 timeEnd 一起使用 */ time(label: string): void; /** * 耗时性能日志输出, 需要配合 time 一起使用 */ timeEnd(label: string): string; /** * 是否清空当前屏幕 */ clearScreen(type: LogType): void; /** * 当前错误是否已输出 */ hasErrorLogged(error: Error): boolean; hasWarned: boolean; hasErrored: boolean; options: LogOptions; } export declare type LoadingLogger = Pick<Logger, 'error' | 'success'> & { fail: Logger['error']; /** * 更新 loading 信息 */ update: (msg: string) => LoadingLogger; /** * 开始 loading */ start: (msg?: string, options?: LogOptions) => LoadingLogger; /** * 停止 loadding */ stop: () => LoadingLogger; }; export interface LogOptions { clear?: boolean; timestamp?: boolean; color?: boolean; align?: boolean; symbol?: boolean | string; update?: boolean; depth?: null | number; } export interface LogErrorOptions extends LogOptions { error?: Error | null; } export interface LoggerOptions { /** * 日志前缀 */ prefix?: string; /** * debug 前缀 */ debugPrefix?: string; /** * 是否清空屏幕 */ allowClearScreen?: boolean; /** * 自定义 logger 对象 */ customLogger?: Logger; } export declare type TakinDebugScope = `${string}:${string}` | string; export declare type Debug = debug.Debugger['log']; /** * 用于过滤日志参数, 并替换为 ******** */ export declare const FILTER_LOGGER_PARAMETERS: Set<string>; export declare const LogLevels: Record<LogLevel, number>; export declare const COLORS: { error: chalk.Chalk; warn: chalk.Chalk; success: chalk.Chalk; info: chalk.Chalk; }; /** * 创建 debugger * @param namespace - 命名空间 * @returns debugger */ export declare function createDebugger(namespace: TakinDebugScope): debug.Debugger; /** * 开启特定命名空间的 debugger * @param namespaces - 需要开启 debug 的命名空间 */ export declare function enableDebugger(namespaces: string): void; /** * 创建日志 * @param level - 日志级别 * @param options - 日志创建选项 * @returns logger 实例 */ export declare function createLogger(level?: LogLevel, options?: LoggerOptions): Logger; /** * 默认 logger */ export declare const logger: Logger;