本示例演示如何配置 dubbo-go 框架日志组件,将框架运行态日志保存到指定的位置。可在此查看 完整示例源码地址。
如下所示,可以通过 log.WithZap()、log.WithLevel("warn") 设置 dubbo 框架日志行为:
ins, err := dubbo.NewInstance(
dubbo.WithLogger(
log.WithLevel("warn"),
log.WithZap(),
),
)
注意,这里配置的只是 dubbo-go 框架自身的日志组件行为(即框架内部使用的日志),不影响业务日志框架的使用!
通过以下方式,业务应用也可以选择复用这个日志组件:
import app_logger "github.com/dubbogo/gost/log/logger"
app_logger.Info("hello")
日志 Interface
type Logger interface {
Info(args ...interface{})
Warn(args ...interface{})
Error(args ...interface{})
Debug(args ...interface{})
Fatal(args ...interface{})
Infof(fmt string, args ...interface{})
Warnf(fmt string, args ...interface{})
Errorf(fmt string, args ...interface{})
Debugf(fmt string, args ...interface{})
Fatalf(fmt string, args ...interface{})
}
当前 dubbo-go 框架支持 zap、logrus 两个日志框架,如果您想让 dubbo 框架内核使用其他日志框架打印日志,推荐以标准扩展形式增加支持,具体可参考核心库中内置的 源码实现。
可以通过以下方式配置开启访问日志:
srv, err := server.NewServer(
server.WithAccesslog("true"),
// server.WithAccesslog("default"),
// server.WithAccesslog("/your/path/to/store/the/log/logfile"),
)
对于 true 和 default 而言,访问日志会使用 Dubbo 中的 logger 组件打印出来。如果指定了具体的日志文件路径,则直接写入到该文件。
可以通过 log.WithTraceIntegration(true) 启用 trace 集成:
ins, err := dubbo.NewInstance(
dubbo.WithLogger(
log.WithLevel("warn"),
log.WithZap(),
log.WithTraceIntegration(true), // 启用 trace 集成
log.WithRecordErrorToSpan(true), // 错误记录到 span
),
)
OpenTelemetry tracer 启用后,可以通过 CtxLogger 记录带有 trace 的日志:
ctxLog.CtxInfo(ctx, "hello dubbogo this is info log")
ctxLog.CtxDebug(ctx, "hello dubbogo this is debug log")
ctxLog.CtxWarn(ctx, "hello dubbogo this is warn log")
ctxLog.CtxError(ctx, "hello dubbogo this is error log")
ctxLog.CtxInfof(ctx, "user: %s", "alice")
ctxLog.CtxDebugf(ctx, "value: %d", 42)
ctxLog.CtxWarnf(ctx, "latency: %dms", 150)
ctxLog.CtxErrorf(ctx, "failed: %v", err)