瀏覽代碼

fix: only setup logx once (#2188)

* fix: only setup logx once

* fix: test failure

* chore: not reset logging level in reset

* chore: refactoring
Kevin Wan 2 年之前
父節點
當前提交
5cd9229986
共有 3 個文件被更改,包括 21 次插入29 次删除
  1. 14 14
      core/logx/contextlogger_test.go
  2. 7 2
      core/logx/logs.go
  3. 0 13
      rest/server_test.go

+ 14 - 14
core/logx/contextlogger_test.go

@@ -29,7 +29,7 @@ func TestTraceLog(t *testing.T) {
 	otel.SetTracerProvider(tp)
 	defer otel.SetTracerProvider(otp)
 
-	ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
+	ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
 	defer span.End()
 
 	WithContext(ctx).Info(testlog)
@@ -50,7 +50,7 @@ func TestTraceError(t *testing.T) {
 	otel.SetTracerProvider(tp)
 	defer otel.SetTracerProvider(otp)
 
-	ctx, span := tp.Tracer("foo1").Start(context.Background(), "bar")
+	ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
 	defer span.End()
 
 	var nilCtx context.Context
@@ -67,10 +67,10 @@ func TestTraceError(t *testing.T) {
 	l.WithDuration(time.Second).Errorv(testlog)
 	validate(t, w.String(), true, true)
 	w.Reset()
-	l.WithDuration(time.Second).Errorw(testlog, Field("foo1", "bar"))
+	l.WithDuration(time.Second).Errorw(testlog, Field("basket", "ball"))
 	validate(t, w.String(), true, true)
-	assert.True(t, strings.Contains(w.String(), "foo1"), w.String())
-	assert.True(t, strings.Contains(w.String(), "bar"), w.String())
+	assert.True(t, strings.Contains(w.String(), "basket"), w.String())
+	assert.True(t, strings.Contains(w.String(), "ball"), w.String())
 }
 
 func TestTraceInfo(t *testing.T) {
@@ -87,7 +87,7 @@ func TestTraceInfo(t *testing.T) {
 	otel.SetTracerProvider(tp)
 	defer otel.SetTracerProvider(otp)
 
-	ctx, span := tp.Tracer("foo1").Start(context.Background(), "bar")
+	ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
 	defer span.End()
 
 	SetLevel(InfoLevel)
@@ -101,10 +101,10 @@ func TestTraceInfo(t *testing.T) {
 	l.WithDuration(time.Second).Infov(testlog)
 	validate(t, w.String(), true, true)
 	w.Reset()
-	l.WithDuration(time.Second).Infow(testlog, Field("foo1", "bar"))
+	l.WithDuration(time.Second).Infow(testlog, Field("basket", "ball"))
 	validate(t, w.String(), true, true)
-	assert.True(t, strings.Contains(w.String(), "foo1"), w.String())
-	assert.True(t, strings.Contains(w.String(), "bar"), w.String())
+	assert.True(t, strings.Contains(w.String(), "basket"), w.String())
+	assert.True(t, strings.Contains(w.String(), "ball"), w.String())
 }
 
 func TestTraceInfoConsole(t *testing.T) {
@@ -124,7 +124,7 @@ func TestTraceInfoConsole(t *testing.T) {
 	otel.SetTracerProvider(tp)
 	defer otel.SetTracerProvider(otp)
 
-	ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
+	ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
 	defer span.End()
 
 	l := WithContext(ctx)
@@ -153,7 +153,7 @@ func TestTraceSlow(t *testing.T) {
 	otel.SetTracerProvider(tp)
 	defer otel.SetTracerProvider(otp)
 
-	ctx, span := tp.Tracer("foo1").Start(context.Background(), "bar")
+	ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
 	defer span.End()
 
 	l := WithContext(ctx)
@@ -168,10 +168,10 @@ func TestTraceSlow(t *testing.T) {
 	l.WithDuration(time.Second).Slowv(testlog)
 	validate(t, w.String(), true, true)
 	w.Reset()
-	l.WithDuration(time.Second).Sloww(testlog, Field("foo1", "bar"))
+	l.WithDuration(time.Second).Sloww(testlog, Field("basket", "ball"))
 	validate(t, w.String(), true, true)
-	assert.True(t, strings.Contains(w.String(), "foo1"), w.String())
-	assert.True(t, strings.Contains(w.String(), "bar"), w.String())
+	assert.True(t, strings.Contains(w.String(), "basket"), w.String())
+	assert.True(t, strings.Contains(w.String(), "ball"), w.String())
 }
 
 func TestTraceWithoutContext(t *testing.T) {

+ 7 - 2
core/logx/logs.go

@@ -23,6 +23,7 @@ var (
 	disableStat uint32
 	options     logOptions
 	writer      = new(atomicWriter)
+	setupOnce   uint32
 )
 
 type (
@@ -189,7 +190,6 @@ func MustSetup(c LogConf) {
 
 // Reset clears the writer and resets the log level.
 func Reset() Writer {
-	SetLevel(InfoLevel)
 	return writer.Swap(nil)
 }
 
@@ -206,8 +206,13 @@ func SetWriter(w Writer) {
 // SetUp sets up the logx. If already set up, just return nil.
 // we allow SetUp to be called multiple times, because for example
 // we need to allow different service frameworks to initialize logx respectively.
-// the same logic for SetUp
 func SetUp(c LogConf) error {
+	// Just ignore the subsequent SetUp calls.
+	// Because multiple services in one process might call SetUp respectively.
+	if !atomic.CompareAndSwapUint32(&setupOnce, 0, 1) {
+		return nil
+	}
+
 	setupLogLevel(c)
 
 	if len(c.TimeFormat) > 0 {

+ 0 - 13
rest/server_test.go

@@ -16,7 +16,6 @@ import (
 	"github.com/stretchr/testify/assert"
 	"github.com/zeromicro/go-zero/core/conf"
 	"github.com/zeromicro/go-zero/core/logx"
-	"github.com/zeromicro/go-zero/core/service"
 	"github.com/zeromicro/go-zero/rest/chain"
 	"github.com/zeromicro/go-zero/rest/httpx"
 	"github.com/zeromicro/go-zero/rest/router"
@@ -105,18 +104,6 @@ Port: 54321
 	}
 }
 
-func TestNewServerError(t *testing.T) {
-	_, err := NewServer(RestConf{
-		ServiceConf: service.ServiceConf{
-			Log: logx.LogConf{
-				// file mode, no path specified
-				Mode: "file",
-			},
-		},
-	})
-	assert.NotNil(t, err)
-}
-
 func TestWithMaxBytes(t *testing.T) {
 	const maxBytes = 1000
 	var fr featuredRoutes