Alanxtl commented on code in PR #146:
URL: https://github.com/apache/dubbo-getty/pull/146#discussion_r4640487762
##########
logger.go:
##########
@@ -58,8 +62,43 @@ const (
LoggerLevelFatal = LoggerLevel(zapcore.FatalLevel)
)
+// loggerHolder atomically stores the process logger so SetLogger can
+// be called without racing in-flight log calls.
+type loggerHolder struct {
+ inner atomic.Value // Logger
+}
+
+type loggerBox struct {
+ logger Logger
+}
+
+func (l *loggerHolder) current() Logger {
+ v := l.inner.Load()
+ if v == nil {
+ return nil
+ }
+ return v.(loggerBox).logger
+}
+
+func (l *loggerHolder) Info(args ...any) { l.current().Info(args...) }
+func (l *loggerHolder) Warn(args ...any) { l.current().Warn(args...) }
+func (l *loggerHolder) Error(args ...any) { l.current().Error(args...) }
+func (l *loggerHolder) Debug(args ...any) { l.current().Debug(args...) }
+func (l *loggerHolder) Infof(fmt string, args ...any) {
+ l.current().Infof(fmt, args...)
+}
+func (l *loggerHolder) Warnf(fmt string, args ...any) {
+ l.current().Warnf(fmt, args...)
+}
+func (l *loggerHolder) Errorf(fmt string, args ...any) {
+ l.current().Errorf(fmt, args...)
+}
+func (l *loggerHolder) Debugf(fmt string, args ...any) {
+ l.current().Debugf(fmt, args...)
+}
Review Comment:
日志的 caller 指向了转发层。 `loggerHolder` 给每次日志调用多加了一层。用启用了 caller 的 Zap
做定向测试时,基线记录的是实际调用位置,PR 记录的是 `logger.go:88`。默认 Zap 和通过 `SetLogger` 注入的同类 logger
都会受影响。建议在调用点使用 `GetLogger().Infof(...)`,保留原子读取和准确的调用位置。
--
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]