From 5cd9229986ec9537ba3c10afec5874b82b6b3cab Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Thu, 28 Jul 2022 22:08:48 +0800 Subject: [PATCH] fix: only setup logx once (#2188) * fix: only setup logx once * fix: test failure * chore: not reset logging level in reset * chore: refactoring --- core/logx/contextlogger_test.go | 28 ++++++++++++++-------------- core/logx/logs.go | 9 +++++++-- rest/server_test.go | 13 ------------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/core/logx/contextlogger_test.go b/core/logx/contextlogger_test.go index 52fbd839..8f931481 100644 --- a/core/logx/contextlogger_test.go +++ b/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) { diff --git a/core/logx/logs.go b/core/logx/logs.go index aa72fe73..0260fa54 100644 --- a/core/logx/logs.go +++ b/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 { diff --git a/rest/server_test.go b/rest/server_test.go index d9cc4011..bc99f125 100644 --- a/rest/server_test.go +++ b/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