fix: only setup logx once (#2188)

* fix: only setup logx once

* fix: test failure

* chore: not reset logging level in reset

* chore: refactoring
master
Kevin Wan 2 years ago committed by GitHub
parent 3d38d36605
commit 5cd9229986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -23,6 +23,7 @@ var (
disableStat uint32 disableStat uint32
options logOptions options logOptions
writer = new(atomicWriter) writer = new(atomicWriter)
setupOnce uint32
) )
type ( type (
@ -189,7 +190,6 @@ func MustSetup(c LogConf) {
// Reset clears the writer and resets the log level. // Reset clears the writer and resets the log level.
func Reset() Writer { func Reset() Writer {
SetLevel(InfoLevel)
return writer.Swap(nil) return writer.Swap(nil)
} }
@ -206,8 +206,13 @@ func SetWriter(w Writer) {
// SetUp sets up the logx. If already set up, just return nil. // SetUp sets up the logx. If already set up, just return nil.
// we allow SetUp to be called multiple times, because for example // we allow SetUp to be called multiple times, because for example
// we need to allow different service frameworks to initialize logx respectively. // we need to allow different service frameworks to initialize logx respectively.
// the same logic for SetUp
func SetUp(c LogConf) error { 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) setupLogLevel(c)
if len(c.TimeFormat) > 0 { if len(c.TimeFormat) > 0 {

@ -16,7 +16,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx" "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/chain"
"github.com/zeromicro/go-zero/rest/httpx" "github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/router" "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) { func TestWithMaxBytes(t *testing.T) {
const maxBytes = 1000 const maxBytes = 1000
var fr featuredRoutes var fr featuredRoutes

Loading…
Cancel
Save