chore: let logx.SetWriter can be called anytime (#2186)

master
Kevin Wan 2 years ago committed by GitHub
parent 685d14e662
commit 9b334b5428
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -199,11 +199,8 @@ func SetLevel(level uint32) {
} }
// SetWriter sets the logging writer. It can be used to customize the logging. // SetWriter sets the logging writer. It can be used to customize the logging.
// Call Reset before calling SetWriter again.
func SetWriter(w Writer) { func SetWriter(w Writer) {
if writer.Load() == nil {
writer.Store(w) writer.Store(w)
}
} }
// SetUp sets up the logx. If already set up, just return nil. // SetUp sets up the logx. If already set up, just return nil.

@ -603,8 +603,9 @@ func TestSetWriter(t *testing.T) {
SetWriter(nopWriter{}) SetWriter(nopWriter{})
assert.NotNil(t, writer.Load()) assert.NotNil(t, writer.Load())
assert.True(t, writer.Load() == nopWriter{}) assert.True(t, writer.Load() == nopWriter{})
SetWriter(new(mockWriter)) mocked := new(mockWriter)
assert.True(t, writer.Load() == nopWriter{}) SetWriter(mocked)
assert.Equal(t, mocked, writer.Load())
} }
func TestWithGzip(t *testing.T) { func TestWithGzip(t *testing.T) {

@ -69,8 +69,7 @@ func logDuration(ctx context.Context, method string, req interface{}, duration t
if err != nil { if err != nil {
logx.WithContext(ctx).Errorf("%s - %s", addr, err.Error()) logx.WithContext(ctx).Errorf("%s - %s", addr, err.Error())
} else if duration > slowThreshold.Load() { } else if duration > slowThreshold.Load() {
logger.Slowf("[RPC] slowcall - %s - %s - %s", logger.Slowf("[RPC] slowcall - %s - %s - %s", addr, method, string(content))
addr, method, string(content))
} else { } else {
logger.Infof("%s - %s - %s", addr, method, string(content)) logger.Infof("%s - %s - %s", addr, method, string(content))
} }

Loading…
Cancel
Save