npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@vafast/request-logger

v0.5.0

Published

API request logging middleware for Vafast

Readme

@vafast/request-logger

HTTP 访问日志中间件:脱敏、异步上报远程服务、stdout 双写、熔断与错误节流。

urlservice 必填。应用内 logger.info 请用 @vafast/logger(不是中间件)。

安装

npm install @vafast/request-logger

快速开始

import { Server, defineRoute, defineRoutes, serve } from 'vafast'
import { requestId } from '@vafast/request-id'
import { requestLogger } from '@vafast/request-logger'

const routes = defineRoutes([
  defineRoute({
    method: 'GET',
    path: '/',
    handler: () => ({ ok: true }),
  }),
])

const server = new Server(routes)
server.use(requestId())
server.use(
  requestLogger({
    url: 'http://log-server:9005/api/logs/ingest',
    service: 'my-server',
  }),
)

serve({ fetch: server.fetch, port: 3000 })

用法

排除路径 / 路由关闭

requestLogger({
  url: '...',
  service: 'my-server',
  excludePaths: ['/health', /^\/metrics/],
})

defineRoute({
  method: 'GET',
  path: '/ready',
  log: false,
  handler: () => ({ ok: true }),
})

stdout / 采样 / 熔断

requestLogger({
  url: '...',
  service: 'gateway',
  sampleRate: 0.1,
  stdout: { format: 'json', includeResponse: false },
  circuitBreaker: { failureThreshold: 5, resetTimeout: 60_000 },
  errorThrottle: { interval: 60_000 },
})

自定义业务字段

requestLogger({
  url: '...',
  service: 'billing',
  getUserId: ({ req }) => req.__locals?.userInfo?.id,
  getAppId: async ({ body }) => lookupAppId(body),
  getAuthType: ({ headers }) =>
    headers.authorization?.startsWith('Bearer ak_') ? 'apiKey' : undefined,
})

API 完整参数

requestLogger(options)RequestLoggerOptions

| 参数 | 必填 | 默认 | 说明 | |------|------|------|------| | url | | — | 远程 ingest URL | | service | | — | 服务标识 | | headers | 否 | {} | 上报额外头 | | timeout | 否 | 5000 | 超时毫秒 | | enabled | 否 | true | 总开关 | | excludePaths | 否 | [] | 排除路径 | | sanitize | 否 | 内置 | removeFields / maskFields / placeholder / maxDepth | | onError | 否 | 结构化 warn | (error, { droppedCount }) | | circuitBreaker | 否 | 5 / 60000 | 熔断 | | errorThrottle | 否 | 60000 | 错误节流 | | stdout | 否 | 开启 JSON | enabled / format / includeBody / includeResponse | | sampleRate | 否 | 1 | 0–1 | | requestIdHeader | 否 | 'x-request-id' | 无 req.id 时读取 | | getAppId / getUserId / getAuthType | 否 | — | 自定义提取 |

createRequestLogger 为废弃别名。另导出 sanitize / sanitizeHeaders

最佳实践

  • requestId() 再本中间件
  • 健康检查用 excludePathslog: false默认排除 /health
  • 高流量用 sampleRate;stdout 默认不要带完整 response
  • 支付回调等用 getAppId 从 body 反查

端字段(Ones App Client)

在 headers 脱敏前读取约定头,并写入 ingest 顶层字段(log-server 不再从 headers 兜底解析):

| Header | 顶层字段 | 示例 | |--------|----------|------| | client-key | clientKey | web / desktop / ios / android | | x-platform | platform | browser / darwin / win32 / linux / ios / android | | x-app-version | appVersion | 1.2.3 |

缺失或空串时不写该顶层字段(可选,非必填;服务间调用通常没有)。

注意事项

  • 上报异步,失败不影响业务响应
  • 跳过日志的请求不会 clone 读 body
  • sanitize 字段名是 removeFields / maskFields,不是 fields / mask
  • JWT userId 默认解析不验签,仅日志归属

相关链接

License

MIT