瀏覽代碼

feat: support %w in logx.Errorf (#1278)

Kevin Wan 3 年之前
父節點
當前提交
de5ed6a677
共有 2 個文件被更改,包括 12 次插入1 次删除
  1. 1 1
      core/logx/logs.go
  2. 11 0
      core/logx/logs_test.go

+ 1 - 1
core/logx/logs.go

@@ -217,7 +217,7 @@ func ErrorCaller(callDepth int, v ...interface{}) {
 
 
 // ErrorCallerf writes v with context in format into error log.
 // ErrorCallerf writes v with context in format into error log.
 func ErrorCallerf(callDepth int, format string, v ...interface{}) {
 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.
 // Errorf writes v with format into error log.

+ 11 - 0
core/logx/logs_test.go

@@ -2,6 +2,7 @@ package logx
 
 
 import (
 import (
 	"encoding/json"
 	"encoding/json"
+	"errors"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"io/ioutil"
 	"io/ioutil"
@@ -242,6 +243,16 @@ func TestSetLevelWithDuration(t *testing.T) {
 	assert.Equal(t, 0, writer.builder.Len())
 	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) {
 func TestMustNil(t *testing.T) {
 	Must(nil)
 	Must(nil)
 }
 }