go-util

module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: GPL-3.0

README

go-util

logging

仓库内已新增终局版日志模块设计与第一版实现骨架:

  • 设计稿:DESIGN_logging_refactor.md
  • 实施清单:IMPLEMENTATION_CHECKLIST_logging_refactor.md
  • 新模块:logging/
  • 使用说明:logging/README.md

当前推荐使用 logging,不再以旧 log/ 作为后续新接入目标。

log/ 包状态

github.com/imdm/go-util/log 已标记为 deprecated

  • 仅保留给历史代码
  • 不再作为新代码接入入口
  • 后续统一收口到 github.com/imdm/go-util/logging
初始化
logger := logging.MustNewLogger(logging.Config{
	Service:      "example-service",
	Env:          "prod",
	Version:      "v1.0.0",
	Format:       logging.AutoFormat,
	Level:        logging.InfoLevel,
	AppWriter:    os.Stdout,
	AccessWriter: os.Stdout,
	ErrorWriter:  os.Stderr,
	AddCaller:    true,
})
net/http 接入
handler := logginghttp.NewAccessLogMiddleware(logger, logginghttp.AccessLogConfig{
	RequestIDHeader: "X-Request-ID",
	RecoverPanic:    true,
})(mux)
请求内追加字段
func Handle(w http.ResponseWriter, r *http.Request) {
	log := logging.MustFromContext(r.Context())
	log.AppendRequestLogField("order_id", "o_123")
	log.AppendRequestLogMessage("request handled")
	w.WriteHeader(http.StatusOK)
}

Directories

Path Synopsis
Package log is the legacy logging package.
Package log is the legacy logging package.
Package logging provides structured application logging, request-scoped log aggregation, and HTTP/Gin access log middleware.
Package logging provides structured application logging, request-scoped log aggregation, and HTTP/Gin access log middleware.
gin
Package logginggin provides Gin access log middleware built on top of the logging package.
Package logginggin provides Gin access log middleware built on top of the logging package.
http
Package logginghttp provides net/http access log middleware built on top of the logging package.
Package logginghttp provides net/http access log middleware built on top of the logging package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL