fix: Errorv should generate JSON Object for content field in log (#3222)
Signed-off-by: soasurs <soasurs@gmail.com>
This commit is contained in:
@@ -65,7 +65,7 @@ func (l *richLogger) Errorf(format string, v ...any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *richLogger) Errorv(v any) {
|
func (l *richLogger) Errorv(v any) {
|
||||||
l.err(fmt.Sprint(v))
|
l.err(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *richLogger) Errorw(msg string, fields ...LogField) {
|
func (l *richLogger) Errorw(msg string, fields ...LogField) {
|
||||||
|
|||||||
@@ -66,6 +66,9 @@ func TestTraceDebug(t *testing.T) {
|
|||||||
l.WithDuration(time.Second).Debugv(testlog)
|
l.WithDuration(time.Second).Debugv(testlog)
|
||||||
validate(t, w.String(), true, true)
|
validate(t, w.String(), true, true)
|
||||||
w.Reset()
|
w.Reset()
|
||||||
|
l.WithDuration(time.Second).Debugv(testobj)
|
||||||
|
validateContentType(t, w.String(), map[string]any{}, true, true)
|
||||||
|
w.Reset()
|
||||||
l.WithDuration(time.Second).Debugw(testlog, Field("foo", "bar"))
|
l.WithDuration(time.Second).Debugw(testlog, Field("foo", "bar"))
|
||||||
validate(t, w.String(), true, true)
|
validate(t, w.String(), true, true)
|
||||||
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
||||||
@@ -103,6 +106,9 @@ 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).Errorv(testobj)
|
||||||
|
validateContentType(t, w.String(), map[string]any{}, true, true)
|
||||||
|
w.Reset()
|
||||||
l.WithDuration(time.Second).Errorw(testlog, Field("basket", "ball"))
|
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(), "basket"), w.String())
|
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||||||
@@ -137,6 +143,9 @@ 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).Infov(testobj)
|
||||||
|
validateContentType(t, w.String(), map[string]any{}, true, true)
|
||||||
|
w.Reset()
|
||||||
l.WithDuration(time.Second).Infow(testlog, Field("basket", "ball"))
|
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(), "basket"), w.String())
|
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||||||
@@ -173,6 +182,9 @@ func TestTraceInfoConsole(t *testing.T) {
|
|||||||
w.Reset()
|
w.Reset()
|
||||||
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()
|
||||||
|
l.WithDuration(time.Second).Infov(testobj)
|
||||||
|
validateContentType(t, w.String(), map[string]any{}, true, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTraceSlow(t *testing.T) {
|
func TestTraceSlow(t *testing.T) {
|
||||||
@@ -204,6 +216,9 @@ 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).Slowv(testobj)
|
||||||
|
validateContentType(t, w.String(), map[string]any{}, true, true)
|
||||||
|
w.Reset()
|
||||||
l.WithDuration(time.Second).Sloww(testlog, Field("basket", "ball"))
|
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(), "basket"), w.String())
|
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||||||
@@ -311,8 +326,32 @@ func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
|
|||||||
assert.Equal(t, expectedSpan, len(val.Span) > 0, body)
|
assert.Equal(t, expectedSpan, len(val.Span) > 0, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockValue struct {
|
func validateContentType(t *testing.T, body string, expectedType any, expectedTrace, expectedSpan bool) {
|
||||||
Trace string `json:"trace"`
|
var val mockValue
|
||||||
Span string `json:"span"`
|
dec := json.NewDecoder(strings.NewReader(body))
|
||||||
Foo string `json:"foo"`
|
|
||||||
|
for {
|
||||||
|
var doc mockValue
|
||||||
|
err := dec.Decode(&doc)
|
||||||
|
if err == io.EOF {
|
||||||
|
// all done
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val = doc
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.IsType(t, expectedType, val.Content, body)
|
||||||
|
assert.Equal(t, expectedTrace, len(val.Trace) > 0, body)
|
||||||
|
assert.Equal(t, expectedSpan, len(val.Span) > 0, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
type mockValue struct {
|
||||||
|
Trace string `json:"trace"`
|
||||||
|
Span string `json:"span"`
|
||||||
|
Foo string `json:"foo"`
|
||||||
|
Content any `json:"content"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import (
|
|||||||
|
|
||||||
const testlog = "Stay hungry, stay foolish."
|
const testlog = "Stay hungry, stay foolish."
|
||||||
|
|
||||||
|
var testobj = map[string]any{"foo": "bar"}
|
||||||
|
|
||||||
func TestCollectSysLog(t *testing.T) {
|
func TestCollectSysLog(t *testing.T) {
|
||||||
CollectSysLog()
|
CollectSysLog()
|
||||||
content := getContent(captureOutput(func() {
|
content := getContent(captureOutput(func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user