mcpField
W
winston

winston

github.com/claude-dev-suite开发4639 次安装2 天前更新
代码仓库

关于 winston

winston 技能为使用 Winston 的 JavaScript/TypeScript 开发者提供日志配置参考,涵盖多传输、结构化格式与日志轮转。

主要功能

  1. 01配置多种日志传输
  2. 02设置结构化日志格式
  3. 03创建自定义日志级别与格式
  4. 04添加带上下文的子 Logger
  5. 05配置按日期轮转文件
  6. 06排查常见日志问题

适用场景

  1. 01将日志同时写入文件、控制台或 HTTP
  2. 02为请求添加标识和模块上下文
  3. 03限制生产环境日志文件占用
  4. 04检查日志缺失或格式异常

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/claude-dev-suite/claude-dev-suite --skill winston

安装后在 Claude Code 中输入/winston确认已加载。

详细介绍

Winston Logger - 快速参考

何时使用此技能

  • 使用多个传输器进行日志记录(文件、控制台、HTTP)
  • 使用自定义级别进行结构化日志记录
  • 自动轮换日志文件

深入了解:使用 technology 为 winston 的 mcp__documentation__fetch_docs 获取完整文档。

基本设置

npm install winston

核心模式

Logger 基础
import winston from 'winston';

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  ),
  transports: [
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log' }),
  ],
});

// Add console in development
if (process.env.NODE_ENV !== 'production') {
  logger.add(new winston.transports.Console({
    format: winston.format.simple(),
  }));
}
自定义格式
const customFormat = winston.format.printf(({ level, message, timestamp, ...meta }) => {
  return `${timestamp} [${level.toUpperCase()}]: ${message} ${Object.keys(meta).length ? JSON.stringify(meta) : ''}`;
});

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.timestamp(),
    customFormat
  ),
  transports: [new winston.transports.Console()],
});
子 Logger
const childLogger = logger.child({ requestId: req.id, module: 'auth' });
childLogger.info('User authenticated', { userId: user.id });
按日轮换文件
import DailyRotateFile from 'winston-daily-rotate-file';

const transport = new DailyRotateFile({
  filename: 'logs/app-%DATE%.log',
  datePattern: 'YYYY-MM-DD',
  maxSize: '20m',
  maxFiles: '14d',
});

logger.add(transport);

何时不使用此技能

  • 高性能 API:改用 Pino,速度比 Winston 快 10 倍
  • 仅使用 JSON 的日志记录:Pino 对结构化 JSON 输出的优化程度更高
  • 无服务器/Lambda:优先使用更简单的控制台日志记录,或使用 Pino 以降低开销
  • Python/Java 项目:使用对应语言的日志框架
  • 简单脚本:基本调试使用内置的 console 即可

反模式

反模式不良之处解决方案
同步文件日志记录阻塞事件循环,降低性能使用带有 { stream: ... } 的异步传输器
不序列化就记录对象可能记录 [Object],而非数据本身使用 JSON.stringify() 或 Winston JSON 格式
不轮换日志生产环境中的磁盘空间会被占满使用 winston-daily-rotate-file
在紧密循环中记录日志I/O 负担过重,占满磁盘添加条件逻辑或对日志进行采样
字符串拼接即使禁用日志,也会始终执行使用格式字符串:logger.info('User %s', userId)
缺少错误堆栈跟踪丢失调试上下文使用 { error: err } 或 Winston 错误格式

快速故障排除

问题原因解决方案
日志未显示配置了错误的日志级别检查 logger.level 和传输器级别
性能下降同步写入文件使用异步传输器或减少日志详细程度
磁盘已满未轮换日志使用 maxFiles 配置 winston-daily-rotate-file
日志中出现 [Object]对象格式不当使用 winston.format.json() 或 winston.format.prettyPrint()
日志重复多个传输器指向同一目标检查传输器配置
未显示颜色控制台传输器缺少 colorize在控制台传输器中添加 winston.format.colorize()

评论

支持 Markdown