@cmtlyt/logger
Version:
124 lines (123 loc) • 3.85 kB
TypeScript
import type { OutputAdapter } from '../types';
/**
* 定义输出适配器的辅助函数,提供类型安全的适配器创建
*
* @template T 数据类型
* @template C 回调函数类型
* @param callback 适配器创建函数
* @returns 原始回调函数,但具有正确的类型推断
*
* @example
* ```typescript
* const myAdapter = defineAdapter((options) => {
* return (type) => {
* // 适配器逻辑
* return outputFunc;
* };
* });
* ```
*/
export declare function defineAdapter<T, C extends (...args: any[]) => OutputAdapter<T> | Promise<OutputAdapter<T>>>(callback: C): C;
/**
* 将对象转换为格式化的JSON字符串,支持函数和循环引用处理
*
* 特性:
* - 自动处理函数类型,保留函数定义
* - 检测并标记循环引用,避免无限递归
* - 使用非断行空格进行缩进,保持格式美观
*
* @param obj 要序列化的对象
* @returns 格式化的JSON字符串
*
* @example
* ```typescript
* const obj = {
* name: 'test',
* fn: () => console.log('hello'),
* circular: null
* };
* obj.circular = obj; // 创建循环引用
*
* const result = objectStringify(obj);
* // 输出包含函数定义和循环引用标记的格式化字符串
* ```
*/
export declare function objectStringify(obj: any): string;
/**
* 检测当前环境是否为Web浏览器环境
*
* 通过检查全局对象中是否存在window对象来判断运行环境
*
* @returns true表示Web环境,false表示非Web环境(如Node.js)
*
* @example
* ```typescript
* if (isWeb()) {
* // 在浏览器环境中执行的代码
* console.log('Running in browser');
* } else {
* // 在Node.js或其他环境中执行的代码
* console.log('Running in Node.js');
* }
* ```
*/
export declare function isWeb(): boolean;
/**
* 检测当前环境是否为Node.js环境
*
* 通过检查是否存在process对象和console对象来判断运行环境
*
* @returns true表示Node.js环境,false表示非Node.js环境
*
* @example
* ```typescript
* if (isNode()) {
* // 在Node.js环境中执行的代码
* console.log('Running in Node.js');
* } else {
* // 在浏览器或其他环境中执行的代码
* console.log('Running in browser');
* }
* ```
*/
export declare function isNode(): boolean;
/**
* 创建日志类型检查器
*
* 支持三种配置模式:
* - string[]: 扩展模式,在默认类型基础上添加指定类型
* - Set<string>: 替换模式,完全替换默认类型
* - function: 函数模式,通过函数判断是否允许某个类型
*
* @param allowTypes 允许的日志类型配置
* @returns 类型检查器(Set或函数)
*
* @example
* ```typescript
* // 扩展模式:在默认类型基础上添加自定义类型
* const checker1 = createAllowTypesChecker(['custom', 'special']);
*
* // 替换模式:完全替换默认类型
* const checker2 = createAllowTypesChecker(new Set(['info', 'error']));
*
* // 函数模式:自定义判断逻辑
* const checker3 = createAllowTypesChecker((type) => type.startsWith('app_'));
* ```
*/
export declare function createAllowTypesChecker(allowTypes?: string[] | Set<string> | ((type: string) => boolean)): Set<string> | ((type: string) => boolean);
/**
* 检查指定的日志类型是否被允许
*
* @param logType 要检查的日志类型
* @param checker 类型检查器(由createAllowTypesChecker创建)
* @returns true表示允许,false表示不允许
*
* @example
* ```typescript
* const checker = createAllowTypesChecker(['info', 'error']);
*
* console.log(isTypeAllowed('info', checker)); // true
* console.log(isTypeAllowed('debug', checker)); // false
* ```
*/
export declare function isTypeAllowed(logType: string, checker: Set<string> | ((type: string) => boolean)): boolean;