GoWindGoWind
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
  • 介绍

    • GoWind 框架
    • 框架整体架构
  • go-wind 核心

    • go-wind 核心框架
    • App 生命周期管理
    • Context 传播
    • Transport 抽象
    • Log 门面接口
  • go-wind-plugins 插件

    • go-wind-plugins 插件总览
    • 插件配置系统
    • 插件注册机制(SPI)
    • 日志适配插件
    • 传输协议插件
    • 消息中间件插件
    • 编码解码插件
    • 安全与认证插件
    • 链路追踪插件
    • 缓存插件
    • 对象存储插件(OSS)
    • 限流插件
    • 指标监控插件
    • AI 插件
    • 工作流插件
    • 数据库与缓存插件
    • Bootstrap 集成与网关插件
    • 压缩与序列化插件
    • 模板渲染与验证插件
  • go-wind-bootstrap 启动器

    • go-wind-bootstrap 声明式启动器
    • Bootstrap 配置系统
    • Bootstrap SPI 机制
    • 声明式中间件编排
    • Bootstrap 命令行集成
    • Bootstrap 实战示例
  • go-wind-flow 流程引擎

    • GoWind Flow 流程引擎
  • 教程

    • 快速入门教程
    • 自定义插件开发教程
    • 多协议同时监听教程
    • 框架迁移指南

Log 门面接口

go-wind 的 Log 模块是一个极简日志门面,只定义接口不提供实现。核心内置 nop 空实现兜底,具体适配器(slog adapter、LevelFilter、MultiLogger 等)由 go-wind-plugins 提供,适配 slog / zap / zerolog 等日志后端只需几行胶水代码。

一、Logger 接口

// log/log.go
type Level int

const (
    LevelDebug Level = iota
    LevelInfo
    LevelWarn
    LevelError
)

type Logger interface {
    // 4 个日志方法:第一个参数恒为 context.Context,
    // 便于日志后端自动提取 TraceID 等请求级元数据
    Debug(ctx context.Context, msg string, args ...any)
    Info(ctx context.Context, msg string, args ...any)
    Warn(ctx context.Context, msg string, args ...any)
    Error(ctx context.Context, msg string, args ...any)

    // Enabled 在构造昂贵参数前检查级别,避免无谓开销
    Enabled(level Level) bool

    // With 返回携带固定键值对的子 Logger
    With(args ...any) Logger
}

级别从低到高为 LevelDebug → LevelInfo → LevelWarn → LevelError,每个 Level 都有可读的 String() 表示。

二、使用方式

2.1 全局 Logger

import windlog "github.com/tx7do/go-wind/log"

// 包级便捷函数,第一个参数为 ctx
windlog.Info(ctx, "service started", "addr", ":8080")
windlog.Error(ctx, "connection failed", "error", err)
windlog.Debug(ctx, "debug info", "count", 42)

2.2 Enabled 守卫

昂贵参数构造前先检查级别:

logger := windlog.GetLogger()
if logger.Enabled(windlog.LevelDebug) {
    logger.Debug(ctx, "detail", computeExpensiveData())
}

2.3 带固定字段的子 Logger

logger := windlog.With("service", "order", "trace_id", traceID)
logger.Info(ctx, "processing order") // 自动携带 service 和 trace_id
logger.Error(ctx, "order failed", "error", err)

2.4 App 级别日志器

app := wind.New(wind.WithLogger(myLogger), ...)

// App 级别日志器,未设置时回退到全局 logger
app.Logger().Info(ctx, "starting")

三、全局注册

// log/global.go
var global Logger = &nopLogger{} // 默认 nop,未设置时静默丢弃

func SetLogger(l Logger) { ... } // 安装全局 logger(并发安全)
func GetLogger() Logger { ... }  // 获取全局 logger

接入你的日志后端只需实现 6 个方法,一行注册:

windlog.SetLogger(myZapAdapter{})

// 或使用 go-wind-plugins 提供的适配器:
//   import pluginslog "github.com/tx7do/go-wind-plugins/log/slog"
//   windlog.SetLogger(pluginslog.New(mySlogLogger))

四、NoOp 实现

核心框架内置 nopLogger,确保在没有安装任何日志器时不会 panic:

type nopLogger struct{}

func (nopLogger) Debug(context.Context, string, ...any) {}
func (nopLogger) Info(context.Context, string, ...any)  {}
func (nopLogger) Warn(context.Context, string, ...any)  {}
func (nopLogger) Error(context.Context, string, ...any) {}
func (nopLogger) Enabled(Level) bool                    { return false }
func (nopLogger) With(...any) Logger                    { return nopLogger{} }

五、适配器

适配器来源说明
slog adaptergo-wind-plugins适配 Go 标准库 slog
LevelFiltergo-wind-plugins按级别过滤
MultiLoggergo-wind-plugins多后端同时输出
自定义适配自行实现 log.Loggerzap / zerolog / logrus / kratos log 等只需几行胶水代码

声明式日志配置(zap / zerolog 滚动日志等)见 Bootstrap 配置系统。

相关文档

  • 核心框架介绍
  • 日志适配插件
  • 声明式启动器介绍
Edit this page
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
Transport 抽象