Repository: zeromicro/go-zero
Pull request #3649 (2023-10-19 03:47:52)

feat: optimize logx print error

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3649?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3649](https://app.codecov.io/gh/zeromicro/go-zero/pull/3649?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (bf38e07) into [master](https://app.codecov.io/gh/zeromicro/go-zero/commit/3ab4e821680264c96477179a8236d17222491e90?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3ab4e82) will **decrease** coverage by `0.02%`.
> Report is 1 commits behind head on master.
> The diff coverage is `100.00%`.


| [Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/3649?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/logx/writer.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3649?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9sb2d4L3dyaXRlci5nbw==) | `95.52% <100.00%> (ø)` | |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/3649/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/core/logx/writer.go b/core/logx/writer.go
index 74d247e8..d38fb2a6 100644
--- a/core/logx/writer.go
+++ b/core/logx/writer.go
@@ -7,10 +7,13 @@ import (
 	"io"
 	"log"
 	"path"
+	"reflect"
+	"runtime/debug"
 	"sync"
 	"sync/atomic"
 
 	fatihcolor "github.com/fatih/color"
+
 	"github.com/zeromicro/go-zero/core/color"
 )
 
@@ -332,7 +335,7 @@ func wrapLevelWithColor(level string) string {
 
 func writeJson(writer io.Writer, info any) {
 	if content, err := json.Marshal(info); err != nil {
-		log.Println(err.Error())
+		log.Printf("err: %s, type: %s\n\n%s\n", err.Error(), reflect.TypeOf(info).Name(), debug.Stack())
 	} else if writer == nil {
 		log.Println(string(content))
 	} else {
@@ -384,7 +387,7 @@ func writePlainValue(writer io.Writer, level string, val any, fields ...string)
 	buf.WriteString(level)
 	buf.WriteByte(plainEncodingSep)
 	if err := json.NewEncoder(&buf).Encode(val); err != nil {
-		log.Println(err.Error())
+		log.Printf("err: %s, type: %s\n\n%s\n", err.Error(), reflect.TypeOf(val).Name(), debug.Stack())
 		return
 	}
 


Test patch:
diff --git a/core/logx/writer_test.go b/core/logx/writer_test.go
index c88d0c7b..c2d55aec 100644
--- a/core/logx/writer_test.go
+++ b/core/logx/writer_test.go
@@ -129,6 +129,15 @@ func TestWriteJson(t *testing.T) {
 	buf.Reset()
 	writeJson(nil, make(chan int))
 	assert.Contains(t, buf.String(), "unsupported type")
+
+	buf.Reset()
+	type C struct {
+		RC func()
+	}
+	writeJson(nil, C{
+		RC: func() {},
+	})
+	assert.Contains(t, buf.String(), "runtime/debug.Stack")
 }
 
 func TestWritePlainAny(t *testing.T) {
@@ -165,6 +174,14 @@ func TestWritePlainAny(t *testing.T) {
 	writePlainAny(hardToWriteWriter{}, levelFatal, "foo")
 	assert.Contains(t, buf.String(), "write error")
 
+	buf.Reset()
+	type C struct {
+		RC func()
+	}
+	writePlainAny(nil, levelError, C{
+		RC: func() {},
+	})
+	assert.Contains(t, buf.String(), "runtime/debug.Stack")
 }
 
 func TestLogWithLimitContentLength(t *testing.T) {