Alanxtl opened a new issue, #3339:
URL: https://github.com/apache/dubbo-go/issues/3339
## Background / 背景
Dubbo-go exposes a logger facade package at
`dubbo.apache.org/dubbo-go/v3/logger`, including APIs such as `SetLogger`,
`GetLogger`, and `SetLoggerLevel`.
Dubbo-go 暴露了 `dubbo.apache.org/dubbo-go/v3/logger` 这个 logger facade 包,包含
`SetLogger`、`GetLogger`、`SetLoggerLevel` 等 API。
However, the normal logger initialization path in `config/logger_config.go`
sets the logger into `github.com/dubbogo/gost/log/logger` and getty, but does
not synchronize the same logger into the dubbo-go facade package.
但是当前 `config/logger_config.go` 中的常规 logger 初始化路径,会把 logger 设置到
`github.com/dubbogo/gost/log/logger` 和 getty 中,却没有同步设置到 dubbo-go 自己的 logger
facade 包。
## Problem / 问题
`config.LoggerConfig.Init()` currently does roughly the following:
`config.LoggerConfig.Init()` 当前大致逻辑如下:
```go
log, err := extension.GetLogger(l.Driver, l.toURL())
logger.SetLogger(log) // github.com/dubbogo/gost/log/logger
getty.SetLogger(log)
```
The imported `logger` here is `github.com/dubbogo/gost/log/logger`, not
`dubbo.apache.org/dubbo-go/v3/logger`.
这里导入的 `logger` 是 `github.com/dubbogo/gost/log/logger`,不是
`dubbo.apache.org/dubbo-go/v3/logger`。
As a result, users may reasonably call:
因此,用户可能会合理地调用:
```go
dubboLogger.SetLoggerLevel("debug")
```
where `dubboLogger` is `dubbo.apache.org/dubbo-go/v3/logger`, expecting it
to update the active Dubbo logger level. But the facade package may hold a nil,
stale, or separately configured logger because it is not synchronized during
normal config initialization.
其中 `dubboLogger` 是 `dubbo.apache.org/dubbo-go/v3/logger`。用户会预期该调用能更新当前 Dubbo
实际使用的 logger level。但由于常规配置初始化没有同步这个 facade 包,它内部可能持有 nil、旧 logger,或者另一份独立配置的
logger。
This creates a state split between:
这会导致状态分裂:
- `github.com/dubbogo/gost/log/logger`, which is the logger actually
configured by `LoggerConfig.Init()`.
- `dubbo.apache.org/dubbo-go/v3/logger`, which exposes public-looking facade
APIs but is not kept in sync.
- `github.com/dubbogo/gost/log/logger`:由 `LoggerConfig.Init()` 实际配置的 logger。
- `dubbo.apache.org/dubbo-go/v3/logger`:暴露 facade API,但没有被同步维护。
## Why this matters / 影响
- `dubbo-go/v3/logger.SetLoggerLevel` can fail or operate on the wrong
logger after normal framework initialization.
- Users may be confused about whether they should import
`github.com/dubbogo/gost/log/logger` or `dubbo.apache.org/dubbo-go/v3/logger`.
- Context-aware logging introduced by `CtxLogger` makes the facade package
more visible, so this inconsistency becomes easier to hit.
- Dynamic log-level control should behave consistently no matter which
built-in logger driver is used.
- 正常框架初始化后,`dubbo-go/v3/logger.SetLoggerLevel` 可能失败,或者作用在错误的 logger 上。
- 用户不清楚应该导入 `github.com/dubbogo/gost/log/logger` 还是
`dubbo.apache.org/dubbo-go/v3/logger`。
- `CtxLogger` 引入后,dubbo-go logger facade 包更容易被用户直接使用,因此这个不一致更容易暴露。
- 动态日志级别调整应在不同内置 logger driver 下保持一致行为。
## Proposed Fix / 建议修复
Synchronize the dubbo-go logger facade during logger initialization. For
example, after creating the logger in `config.LoggerConfig.Init()`:
在 logger 初始化阶段同步 dubbo-go logger facade。例如,在 `config.LoggerConfig.Init()` 创建
logger 后:
```go
gostLogger.SetLogger(log)
dubboLogger.SetLogger(log)
getty.SetLogger(log)
```
where imports are explicitly aliased to avoid ambiguity:
其中 import 应显式使用 alias,避免歧义:
```go
gostLogger "github.com/dubbogo/gost/log/logger"
dubboLogger "dubbo.apache.org/dubbo-go/v3/logger"
```
Also consider whether `config/config_loader.go` default logger
initialization should follow the same synchronization rule.
同时建议检查 `config/config_loader.go` 中默认 logger 初始化是否也应遵循同样的同步规则。
## Acceptance Criteria / 验收标准
- After `LoggerConfig.Init()`,
`github.com/dubbogo/gost/log/logger.GetLogger()` and
`dubbo.apache.org/dubbo-go/v3/logger.GetLogger()` refer to the same active
logger or otherwise behave consistently.
- `dubbo.apache.org/dubbo-go/v3/logger.SetLoggerLevel("debug")` works after
normal logger config initialization.
- Tests cover both zap and logrus if practical.
- Tests cover the trace-integration enabled path to ensure `CtxLogger`
wrappers still support dynamic level updates.
- Imports in logger initialization code are aliased clearly to avoid
package-name ambiguity.
- `LoggerConfig.Init()` 后,`github.com/dubbogo/gost/log/logger.GetLogger()` 和
`dubbo.apache.org/dubbo-go/v3/logger.GetLogger()` 应指向同一个 active logger,或至少行为一致。
- 常规 logger
配置初始化后,`dubbo.apache.org/dubbo-go/v3/logger.SetLoggerLevel("debug")` 可以正常生效。
- 可行时测试覆盖 zap 和 logrus。
- 测试覆盖开启 trace-integration 的路径,确保 `CtxLogger` wrapper 仍支持动态日志级别更新。
- logger 初始化代码中的 import 使用清晰 alias,避免包名歧义。
## Related Code / 相关代码
- `config/logger_config.go`
- `config/config_loader.go`
- `logger/logger.go`
- `logger/core/zap/*`
- `logger/core/logrus/*`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]