From de5ed6a67708aa6c2faf347da1a236bf2a60cfef Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 26 Nov 2021 15:57:23 +0800 Subject: [PATCH] feat: support %w in logx.Errorf (#1278) --- core/logx/logs.go | 2 +- core/logx/logs_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/logx/logs.go b/core/logx/logs.go index 81f23401..d3abc073 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -217,7 +217,7 @@ func ErrorCaller(callDepth int, v ...interface{}) { // ErrorCallerf writes v with context in format into error log. func ErrorCallerf(callDepth int, format string, v ...interface{}) { - errorTextSync(fmt.Sprintf(format, v...), callDepth+callerInnerDepth) + errorTextSync(fmt.Errorf(format, v...).Error(), callDepth+callerInnerDepth) } // Errorf writes v with format into error log. diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index cd3f37f5..03c77fcc 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -2,6 +2,7 @@ package logx import ( "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -242,6 +243,16 @@ func TestSetLevelWithDuration(t *testing.T) { assert.Equal(t, 0, writer.builder.Len()) } +func TestErrorfWithWrappedError(t *testing.T) { + SetLevel(ErrorLevel) + const message = "there" + writer := new(mockWriter) + errorLog = writer + atomic.StoreUint32(&initialized, 1) + Errorf("hello %w", errors.New(message)) + assert.True(t, strings.Contains(writer.builder.String(), "hello there")) +} + func TestMustNil(t *testing.T) { Must(nil) }