@cmtlyt/logger
Version:
58 lines (57 loc) • 1.9 kB
TypeScript
import type { SyncOutputAdapter } from '../../types';
import type { NodeConsoleAdapterOptions } from './types';
/**
* Node.js控制台适配器,用于在Node.js环境中输出格式化的日志信息
*
* 支持的功能:
* - 彩色输出(基于yoctocolors)
* - 自定义输出格式:[type][label][date] message
* - 多种输出级别
* - 类型过滤和环境检测
* - 自定义消息格式化
*
* @template T 数据类型
* @param options 配置选项,可选
* @returns 适配器函数
*
* @example
* ```typescript
* // 基础用法
* const adapter = nodeConsoleAdapter();
*
* // 自定义配置
* const adapter = nodeConsoleAdapter({
* enableColors: true,
* outputLevel: 'info',
* allowTypes: ['info', 'warn', 'error'],
* format: '[%type]-%date %message'
* });
*
* // 使用自定义着色器
* const adapter = nodeConsoleAdapter({
* customColorizer: ({ text, colorName, colors, enableColors, part }) => {
* if (!enableColors || !colors) return text;
* // 自定义着色逻辑,例如根据part类型添加不同的背景色
* if (part === 'type') {
* return colors.bgBlue?.(colors[colorName]?.(text)) || text;
* }
* return colors[colorName]?.(text) || text;
* },
* format: '[%type] %message'
* });
*
* // 使用自定义文本清理器
* const adapter = nodeConsoleAdapter({
* format: '(%type)(%label)(%date) %message',
* textCleaner: (text) => {
* return text
* .replace(/\(\s*\)/g, '') // 移除空的圆括号
* .replace(/\[\s*\]/g, '') // 移除空的方括号
* .replace(/\s+/g, ' ') // 合并多个空格
* .trim(); // 移除首尾空格
* }
* });
* ```
*/
export declare const nodeConsoleAdapter: <T>(options?: NodeConsoleAdapterOptions<T>) => Promise<SyncOutputAdapter<T>>;
export type { NodeConsoleAdapterOptions } from './types';