AlexStocks commented on code in PR #3195: URL: https://github.com/apache/dubbo-go/pull/3195#discussion_r2899089798
########## logger/core/logrus/ctx_logger.go: ########## @@ -0,0 +1,162 @@ +/* + * 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 logrus + +import ( + "context" + "errors" + "fmt" +) + +import ( + dubbogoLogger "github.com/dubbogo/gost/log/logger" + + "github.com/sirupsen/logrus" + + "go.opentelemetry.io/otel/codes" + + "go.opentelemetry.io/otel/trace" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/logger" +) + +// LogrusCtxLogger wraps DubboLogger with context-aware logging support. +type LogrusCtxLogger struct { + *dubbogoLogger.DubboLogger + recordErrorToSpan bool +} + +var _ logger.CtxLogger = (*LogrusCtxLogger)(nil) + +// NewLogrusCtxLogger creates a new LogrusCtxLogger. +func NewLogrusCtxLogger(base *dubbogoLogger.DubboLogger, recordErrorToSpan bool) *LogrusCtxLogger { + return &LogrusCtxLogger{ + DubboLogger: base, + recordErrorToSpan: recordErrorToSpan, + } +} + +// withTraceFields injects trace information from context into logger. +func (l *LogrusCtxLogger) withTraceFields(ctx context.Context) *logrus.Entry { + logrusLogger, ok := l.Logger.(*logrus.Logger) + if !ok { + return nil + } + + fields := logger.ExtractTraceFields(ctx) + if fields.TraceID == "" { + return logrus.NewEntry(logrusLogger) // No trace information + } + + return logrusLogger.WithFields(logrus.Fields{ + "trace_id": fields.TraceID, + "span_id": fields.SpanID, + "trace_flags": fields.TraceFlags, Review Comment: 无 trace 信息时返回 `logrus.NewEntry(logrusLogger)`(非 nil),所有调用方的 `if entry != nil` 判断都走进 entry 路径,fallback 逻辑永远不触发。和 Zap 实现语义不一致(Zap 无 trace 时直接返回原始 logger)。 类型断言失败(`!ok`)时返回 nil,反而触发 fallback,与无 trace 时行为相反,逻辑混乱。 建议:无 trace 时改为 `return nil`,与 Zap 保持一致。 -- 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]
