Repository: zeromicro/go-zero
Pull request #2306 (2022-08-27 05:56:57)

fix: logx disable not working in some cases


Patch:
diff --git a/core/logx/logs.go b/core/logx/logs.go
index 451c8acc..76e75571 100644
--- a/core/logx/logs.go
+++ b/core/logx/logs.go
@@ -21,6 +21,7 @@ var (
 	logLevel   uint32
 	encoding   uint32 = jsonEncodingType
 	// use uint32 for atomic operations
+	disableLog  uint32
 	disableStat uint32
 	options     logOptions
 	writer      = new(atomicWriter)
@@ -73,6 +74,7 @@ func Close() error {
 
 // Disable disables the logging.
 func Disable() {
+	atomic.StoreUint32(&disableLog, 1)
 	writer.Store(nopWriter{})
 }
 
@@ -201,7 +203,9 @@ func SetLevel(level uint32) {
 
 // SetWriter sets the logging writer. It can be used to customize the logging.
 func SetWriter(w Writer) {
-	writer.Store(w)
+	if atomic.LoadUint32(&disableLog) == 0 {
+		writer.Store(w)
+	}
 }
 
 // SetUp sets up the logx. If already set up, just return nil.
diff --git a/core/trace/tracer.go b/core/trace/tracer.go
index 8d5fed62..1e3ce0f5 100644
--- a/core/trace/tracer.go
+++ b/core/trace/tracer.go
@@ -38,7 +38,7 @@ func (s *metadataSupplier) Keys() []string {
 	return out
 }
 
-// Inject injects the metadata into ctx.
+// Inject injects cross-cutting concerns from the ctx into the metadata.
 func Inject(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) {
 	p.Inject(ctx, &metadataSupplier{
 		metadata: metadata,
diff --git a/zrpc/internal/serverinterceptors/tracinginterceptor.go b/zrpc/internal/serverinterceptors/tracinginterceptor.go
index 1b2821de..788ca4cb 100644
--- a/zrpc/internal/serverinterceptors/tracinginterceptor.go
+++ b/zrpc/internal/serverinterceptors/tracinginterceptor.go
@@ -93,11 +93,8 @@ func (w *serverStream) SendMsg(m interface{}) error {
 }
 
 func startSpan(ctx context.Context, method string) (context.Context, trace.Span) {
-	var md metadata.MD
-	requestMetadata, ok := metadata.FromIncomingContext(ctx)
-	if ok {
-		md = requestMetadata.Copy()
-	} else {
+	md, ok := metadata.FromIncomingContext(ctx)
+	if !ok {
 		md = metadata.MD{}
 	}
 	bags, spanCtx := ztrace.Extract(ctx, otel.GetTextMapPropagator(), &md)


Test patch:
diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go
index 89faeb16..6c741539 100644
--- a/core/logx/logs_test.go
+++ b/core/logx/logs_test.go
@@ -599,6 +599,7 @@ func TestDisableStat(t *testing.T) {
 }
 
 func TestSetWriter(t *testing.T) {
+	atomic.StoreUint32(&disableLog, 0)
 	Reset()
 	SetWriter(nopWriter{})
 	assert.NotNil(t, writer.Load())
diff --git a/core/stores/mon/collection_test.go b/core/stores/mon/collection_test.go
index 75ddc493..225f008f 100644
--- a/core/stores/mon/collection_test.go
+++ b/core/stores/mon/collection_test.go
@@ -20,10 +20,6 @@ import (
 
 var errDummy = errors.New("dummy")
 
-func init() {
-	logx.Disable()
-}
-
 func TestKeepPromise_accept(t *testing.T) {
 	p := new(mockPromise)
 	kp := keepablePromise{
diff --git a/core/stores/mongo/collection_test.go b/core/stores/mongo/collection_test.go
index 4ae5f8c0..700fdcc2 100644
--- a/core/stores/mongo/collection_test.go
+++ b/core/stores/mongo/collection_test.go
@@ -17,10 +17,6 @@ import (
 
 var errDummy = errors.New("dummy")
 
-func init() {
-	logx.Disable()
-}
-
 func TestKeepPromise_accept(t *testing.T) {
 	p := new(mockPromise)
 	kp := keepablePromise{