AlexStocks commented on code in PR #3195:
URL: https://github.com/apache/dubbo-go/pull/3195#discussion_r2899090238


##########
logger/core/zap/ctx_logger.go:
##########
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package zap
+
+import (
+       "context"
+       "errors"
+       "fmt"
+)
+
+import (
+       dubbogoLogger "github.com/dubbogo/gost/log/logger"
+
+       "go.opentelemetry.io/otel/codes"
+
+       "go.opentelemetry.io/otel/trace"
+
+       "go.uber.org/zap"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/logger"
+)
+
+// ZapCtxLogger wraps DubboLogger with context-aware logging support.
+type ZapCtxLogger struct {
+       *dubbogoLogger.DubboLogger
+       recordErrorToSpan bool
+}
+
+var _ logger.CtxLogger = (*ZapCtxLogger)(nil)
+
+func NewZapCtxLogger(base *dubbogoLogger.DubboLogger, recordErrorToSpan bool) 
*ZapCtxLogger {
+       return &ZapCtxLogger{
+               DubboLogger:       base,
+               recordErrorToSpan: recordErrorToSpan,
+       }
+}
+
+// withTraceFields injects trace information from context into logger.
+func (l *ZapCtxLogger) withTraceFields(ctx context.Context) *zap.SugaredLogger 
{
+       zapLogger, ok := l.Logger.(*zap.SugaredLogger)
+       if !ok {
+               return nil
+       }
+
+       fields := logger.ExtractTraceFields(ctx)
+       if fields.TraceID == "" {
+               return zapLogger
+       }

Review Comment:
   `l.Logger.(*zap.SugaredLogger)` 类型断言失败时静默返回 nil,所有 trace 字段注入被跳过,没有任何 
warning,问题只在运行时暴露,排查困难。构造函数 `NewZapCtxLogger` 里也没有做类型校验。
   
   建议:类型断言失败时至少 log 一次 warn;或者在构造函数里提前做校验,直接 panic/return error。



-- 
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]

Reply via email to