pino-logging
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePino Logging
Pino 日志
High-performance JSON logger for Node.js. Transports run in worker threads to keep the main event loop free. Produces NDJSON by default with automatic , , , , and fields.
leveltimepidhostnamemsgWhen to use: Structured logging in Node.js applications, request-scoped logging with correlation IDs, sensitive data redaction, multi-destination log routing, framework logging integration.
When NOT to use: Browser-only logging (pino has limited browser support), simple debugging during development, projects that need human-readable logs by default (pino outputs JSON; use for dev).
console.logpino-prettyPackage: (v10+)
pino适用于 Node.js 的高性能 JSON 日志工具。传输进程运行在 worker 线程中,避免阻塞主事件循环。默认生成 NDJSON 格式日志,自动包含 、、、 和 字段。
leveltimepidhostnamemsg适用场景: Node.js 应用中的结构化日志、带关联 ID 的请求域日志、敏感数据脱敏、多目标日志路由、框架日志集成。
不适用场景: 仅浏览器端的日志记录(Pino 对浏览器支持有限)、开发阶段简单的 调试、默认需要人类可读格式日志的项目(Pino 输出 JSON 格式;开发环境可使用 美化输出)。
console.logpino-pretty包: (v10+)
pinoQuick Reference
速查参考
| Pattern | API | Key Points |
|---|---|---|
| Basic logger | | Defaults: level |
| Set level | | |
| Log with context | | First arg is merged object, second is message |
| Error logging | | Pass errors as |
| Child logger | | Bindings persist on all child logs |
| Redaction | | Paths use dot notation, supports wildcards |
| Transport (worker) | | Runs in worker thread, non-blocking |
| Multiple transports | | Different levels per destination |
| Pretty print (dev) | | Dev only — not for production |
| File transport | | Built-in, with |
| Rotating files | | Size and time-based rotation |
| HTTP middleware | | Auto request/response logging |
| Request ID | | Generate or forward |
| Serializers | | Transform objects before logging |
| Formatters | | Rename fields, transform output |
| Custom levels | | Add levels between built-in ones |
| Async destination | | Buffered async writes |
| Fastify | | Built-in pino, |
| Hono | | |
| 模式 | API 接口 | 核心要点 |
|---|---|---|
| 基础日志实例 | | 默认配置:日志级别 |
| 设置日志级别 | | 级别优先级: |
| 带上下文日志 | | 第一个参数为待合并的上下文对象,第二个为日志消息 |
| 错误日志记录 | | 将错误以 |
| 子日志实例 | | 绑定的上下文会保留在所有子日志的输出中 |
| 数据脱敏 | | 路径支持点表示法,可使用通配符 |
| 线程化传输 | | 在 worker 线程中运行,不会阻塞主线程 |
| 多目标传输 | | 可为不同目标设置不同的日志级别 |
| 开发环境美化 | | 仅用于开发环境——请勿在生产环境使用 |
| 文件传输 | | 内置功能,支持 |
| 日志轮转 | | 支持按大小和时间轮转日志文件 |
| HTTP 中间件 | | 自动记录请求/响应日志 |
| 请求 ID 配置 | pino-http 中的 | 生成或转发 |
| 序列化器 | | 日志输出前转换对象格式 |
| 格式化器 | | 重命名字段、转换输出格式 |
| 自定义级别 | | 可在内置级别之间添加自定义日志级别 |
| 异步写入目标 | | 基于缓冲的异步写入 |
| Fastify 集成 | | 内置 Pino 支持,可通过 |
| Hono 集成 | | 处理器中可通过 |
Common Mistakes
常见错误
| Mistake | Fix |
|---|---|
Using | Use JSON output in production; pretty-print only in dev via |
| Logging sensitive data (tokens, passwords) | Use |
| Creating a new logger per request | Use |
| Blocking main thread with file writes | Use |
Using | Pass errors as |
Not forwarding | Use |
| Logging inside hot loops | Check |
Missing | Transports are separate packages — install them as dependencies |
| 错误操作 | 修复方案 |
|---|---|
在生产环境使用 | 生产环境使用 JSON 格式输出;仅在开发环境通过 |
| 记录敏感数据(令牌、密码等) | 使用 |
| 每个请求创建新的日志实例 | 使用 |
| 文件写入阻塞主线程 | 使用 |
对错误使用 | 将错误以 |
未转发 | 在 pino-http 中使用 |
| 在高频循环中记录日志 | 在执行昂贵操作前,先通过 |
使用传输时缺少 | 传输模块是独立包 —— 需要将其作为依赖安装 |
Delegation
任务委托
If theskill is available, delegate Hono middleware and routing patterns to it. If thehonoskill is available, delegate test setup and mocking patterns to it. If thevitest-testingskill is available, delegate Sentry integration to it — it coverssentry-setup-loggingfor forwarding pino logs to Sentry. Otherwise, recommend:Sentry.pinoIntegration()pnpm dlx skills add getsentry/sentry-for-claude -s sentry-setup-logging -a claude-code -y
若技能可用,将 Hono 中间件和路由相关的模式委托给该技能处理。 若hono技能可用,将测试设置和模拟相关的模式委托给该技能处理。 若vitest-testing技能可用,将 Sentry 集成相关内容委托给该技能处理 —— 它涵盖了sentry-setup-logging用于将 Pino 日志转发至 Sentry 的配置。 否则,推荐执行:Sentry.pinoIntegration()pnpm dlx skills add getsentry/sentry-for-claude -s sentry-setup-logging -a claude-code -y
References
参考资料
- Setup, configuration, and core API
- Transports, destinations, and log routing
- Child loggers, serializers, and redaction
- HTTP logging, correlation IDs, and framework integrations
- 安装、配置与核心 API
- 传输、目标与日志路由
- 子日志实例、序列化器与数据脱敏
- HTTP 日志、关联 ID 与框架集成