上下文配置

工程开发 社区

技能解读

当前原文的自动摘译,尚未人工审校。

使用时间

用户希望在 Echo 应用程序中记录 Zap 请求。代码导入github.com/adlandh/echo-zap-middleware/v2或需要配置此中间件。该任务提到了ZapConfig、Middleware、MiddlewareWithContextLogger、BodySkipper、请求/响应正文日志记录或 Echo 请求 ID。

主要事实

该中间件针对github.com/labstack/echo/v5;不要将其与 Echo v4 导入混合。 Echo v5 处理程序和船长使用*echo.Context,而不是echo.Context。导入路径为github.com/adlandh/echo-zap-middleware/v2;除非有别名,否则包名称为 echozapmiddleware。 Middleware(logger, config...) 是*zap.Logger 的正常入口点。 MiddlewareWithContextLogger(ctxLogger, config...) 适用于github.com/adlandh/context-logger 提取器。

实施指南

当生成的请求 ID 应出现在日志中时,在此中间件之前添加middleware.RequestID()。对于热路径,默认关闭主体转储;仅当服务能够承担人体捕获时才启用IsBodyDump。对于敏感或二进制有效负载使用BodySkipper,但对于大型主体仍设置LimitHTTPBody和LimitSize。如果更改此存储库中的中间件行为,请在捕获和更新 README 配置文档后保留下游请求正文重放。

验证

在此存储库中,运行go test ./...进行快速检查。对于身体限制更改,请运行go test -run TestLimitBody ./...和go test -run 'TestMiddleware/TestBodyLimitApplied' ./...。 CI 使用go test -race -coverprofile=coverage.txt -covermode=atomic ./...。来源:adlandh/echo-zap-middleware — 由 TomeVault 分发。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
工程开发
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@tomevault-io · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
即装即用
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# golang-echo-zap

Use this skill when adding or configuring `github.com/adlandh/echo-zap-middleware/v2` in a Go Echo service.

## Use When
- The user wants Zap request logging in an Echo app.
- Code imports `github.com/adlandh/echo-zap-middleware/v2` or needs this middleware configured.
- The task mentions `ZapConfig`, `Middleware`, `MiddlewareWithContextLogger`, `BodySkipper`, request/response body logging, or Echo request IDs.

## Key Facts
- This middleware targets `github.com/labstack/echo/v5`; do not mix it with Echo v4 imports.
- Echo v5 handlers and skippers use `*echo.Context`, not `echo.Context`.
- Import path is `github.com/adlandh/echo-zap-middleware/v2`; package name is `echozapmiddleware` unless aliased.
- `Middleware(logger, config...)` is the normal entrypoint for a `*zap.Logger`.
- `MiddlewareWithContextLogger(ctxLogger, config...)` is for `github.com/adlandh/context-logger` extractors.
- `ZapConfig` fills defaults only for nil `Skipper` and nil `BodySkipper`; bool and int zero values are meaningful.
- `LimitHTTPBody: true` with `LimitSize <= 0` disables body limiting.
- `BodySkipper` does not prevent body capture; it replaces non-empty logged bodies with `[excluded]`.
- Request IDs are logged from request `X-Request-Id` first, then from Echo's response header.

## Minimal Setup
```go
import (
    echozapmiddleware "github.com/adlandh/echo-zap-middleware/v2"
    "github.com/labstack/echo/v5"
    "github.com/labstack/echo/v5/middleware"
    "go.uber.org/zap"
)

logger, err := zap.NewProduction()
if err != nil {
    return err
}

e := echo.New()
e.Use(middleware.RequestID())
e.Use(echozapmiddleware.Middleware(logger))
```

## Body And Header Logging
```go
e.Use(echozapmiddleware.Middleware(logger, echozapmiddleware.ZapConfig{
    AreHeadersDump: true,
    IsBodyDump:     true,
    LimitHTTPBody:  true,
    LimitSize:      500,
    Skipper: func(c *echo.Context) bool {
        return c.Path() == "/health"
    },
    BodySkipper: func(c *echo.Context) (skipReqBody, skipRespBody bool) {
        if c.Path() == "/upload" {
            return true, false
        }
        if c.Request().Header.Get("Content-Encoding") == "gzip" {
            return true, true
        }
        return false, false
    },
}))
```

## Context Logger Setup
```go
import contextlogger "github.com/adlandh/context-logger"

ctxLogger := contextlogger.WithContext(logger)
e.Use(echozapmiddleware.MiddlewareWithContextLogger(ctxLogger))
```

## Implementation Guidance
- Add `middleware.RequestID()` before this middleware when generated request IDs should appear in logs.
- Keep body dumping off by default for hot paths; enable `IsBodyDump` only when the service can afford body capture.
- Use `BodySkipper` for sensitive or binary payloads, but still set `LimitHTTPBody` and `LimitSize` for large bodies.
- If changing middleware behavior in this repo, preserve downstream request body replay after capture and update README configuration docs.

## Verify
- In this repo, run `go test ./...` for a fast check.
- For body limit changes, run `go test -run TestLimitBody ./...` and `go test -run 'TestMiddleware/TestBodyLimitApplied' ./...`.
- CI uses `go test -race -coverprofile=coverage.txt -covermode=atomic ./...`.

---
> Source: [adlandh/echo-zap-middleware](https://github.com/adlandh/echo-zap-middleware) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-05-23 -->

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。