Help us shape the future of Go! Take the 2026 Go Developer Survey

logger

package
v0.32.41 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

ctx.go — Ctx 系日志门面:业务排障日志带 trace_id 的一等入口。

痛点(可观测评估 2026-10-16):app 层业务日志 48 处 0 trace_id,排障只能靠 accesslog 兜。trace_id 写入点在 pkg/middleware.RequestID(ctx key CtxKeyTraceID),但 middleware 已依赖 logger(accesslog/ratelimit/recovery), logger 不能反向 import——故 ctx key 真相源下沉本包,middleware 以类型别名 复用(Go 类型同一性保证 WithValue/Value 配对,既有写入零迁移)。

ring.go 进程内日志环形缓冲(管理端「系统日志」页的真实数据源)。

此前 admin.GetSystemLogs 是返回模拟数据的 TODO 桩——管理面板展示虚构日志。 现将环形缓冲实现为 zapcore.Core 并在 Init 时 Tee 进主日志链:零额外调用 面(业务代码照常 logger.Info/Warn/Error),容量有界(满载丢最旧), 不持久化、重启即清(定位是"最近运行日志"面板,非审计存储——审计在 admin 域的 operation_logs 表)。

Index

Constants

This section is empty.

Variables

View Source
var Logger *zap.Logger

Functions

func CtxError added in v0.24.0

func CtxError(ctx context.Context, msg string, kv ...any)

func CtxInfo added in v0.24.0

func CtxInfo(ctx context.Context, msg string, kv ...any)

CtxInfo / CtxWarn / CtxError:排障日志入口——ctx 里带 trace_id(请求链路) 时自动附 trace_id 字段,与访问日志 / 响应包络同一真相源;无链路 ctx 的 调用点行为退化为普通 Info/Warn/Error。caller 归因与 Debug/Info 等门面 同深(facade 体直呼 zap,Init 的 AddCallerSkip(1) 同样生效)。

func CtxWarn added in v0.24.0

func CtxWarn(ctx context.Context, msg string, kv ...any)

func Debug

func Debug(msg string, fields ...zap.Field)

func Error

func Error(msg string, fields ...zap.Field)

func Fatal

func Fatal(msg string, fields ...zap.Field)

func Info

func Info(msg string, fields ...zap.Field)

func Init

func Init(cfg *Config) error

func Sync

func Sync()

func TraceIDFromContext added in v0.24.0

func TraceIDFromContext(ctx context.Context) string

TraceIDFromContext 从 context.Context 提取 trace_id,无则空串。

func Warn

func Warn(msg string, fields ...zap.Field)

func With

func With(fields ...zap.Field) *zap.Logger

Types

type Config

type Config struct {
	Level      string
	Filename   string
	MaxSize    int
	MaxBackups int
	MaxAge     int
	Compress   bool
}

type CtxKeyTraceID added in v0.24.0

type CtxKeyTraceID struct{}

CtxKeyTraceID context.Context 中 trace_id 的键(全仓唯一真相源; pkg/middleware 经类型别名 `CtxKeyTraceID = logger.CtxKeyTraceID` 复用)。

type RingEntry added in v0.15.1

type RingEntry struct {
	Seq     uint64    // 单调递增序号(跨绕回不回退,供前端做 ID)
	Time    time.Time // 记录时刻
	Level   string    // debug/info/warn/error/...(zap 小写级别名)
	Message string    // 日志消息正文
	Module  string    // logger 名(zap LoggerName,未命名则为空)
}

RingEntry 环形缓冲中的一条日志。

func RecentLogs added in v0.15.1

func RecentLogs(level string, offset, limit int) ([]RingEntry, int)

RecentLogs 查询环形缓冲(管理端系统日志页数据源)。

  • level: 空 = 全部;否则精确匹配级别名(大小写不敏感,如 "error")
  • offset/limit: 分页窗口(按时间序最旧在前;limit ≤ 0 表示不限)

返回 (窗口内条目, 过滤后总数)。

Jump to

Keyboard shortcuts

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