|
|
@ -1,7 +1,6 @@
|
|
|
|
package logc
|
|
|
|
package logc
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
@ -11,14 +10,11 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx/logtest"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestAddGlobalFields(t *testing.T) {
|
|
|
|
func TestAddGlobalFields(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Info(context.Background(), "hello")
|
|
|
|
Info(context.Background(), "hello")
|
|
|
|
buf.Reset()
|
|
|
|
buf.Reset()
|
|
|
@ -34,155 +30,90 @@ func TestAddGlobalFields(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestAlert(t *testing.T) {
|
|
|
|
func TestAlert(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Alert(context.Background(), "foo")
|
|
|
|
Alert(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), "foo"), buf.String())
|
|
|
|
assert.True(t, strings.Contains(buf.String(), "foo"), buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
|
|
|
func TestError(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Error(context.Background(), "foo")
|
|
|
|
Error(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestErrorf(t *testing.T) {
|
|
|
|
func TestErrorf(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Errorf(context.Background(), "foo %s", "bar")
|
|
|
|
Errorf(context.Background(), "foo %s", "bar")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestErrorv(t *testing.T) {
|
|
|
|
func TestErrorv(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Errorv(context.Background(), "foo")
|
|
|
|
Errorv(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestErrorw(t *testing.T) {
|
|
|
|
func TestErrorw(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Errorw(context.Background(), "foo", Field("a", "b"))
|
|
|
|
Errorw(context.Background(), "foo", Field("a", "b"))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestInfo(t *testing.T) {
|
|
|
|
func TestInfo(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Info(context.Background(), "foo")
|
|
|
|
Info(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestInfof(t *testing.T) {
|
|
|
|
func TestInfof(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Infof(context.Background(), "foo %s", "bar")
|
|
|
|
Infof(context.Background(), "foo %s", "bar")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestInfov(t *testing.T) {
|
|
|
|
func TestInfov(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Infov(context.Background(), "foo")
|
|
|
|
Infov(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestInfow(t *testing.T) {
|
|
|
|
func TestInfow(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Infow(context.Background(), "foo", Field("a", "b"))
|
|
|
|
Infow(context.Background(), "foo", Field("a", "b"))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestDebug(t *testing.T) {
|
|
|
|
func TestDebug(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Debug(context.Background(), "foo")
|
|
|
|
Debug(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestDebugf(t *testing.T) {
|
|
|
|
func TestDebugf(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Debugf(context.Background(), "foo %s", "bar")
|
|
|
|
Debugf(context.Background(), "foo %s", "bar")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestDebugv(t *testing.T) {
|
|
|
|
func TestDebugv(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Debugv(context.Background(), "foo")
|
|
|
|
Debugv(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestDebugw(t *testing.T) {
|
|
|
|
func TestDebugw(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Debugw(context.Background(), "foo", Field("a", "b"))
|
|
|
|
Debugw(context.Background(), "foo", Field("a", "b"))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
|
|
@ -204,48 +135,28 @@ func TestMisc(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestSlow(t *testing.T) {
|
|
|
|
func TestSlow(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Slow(context.Background(), "foo")
|
|
|
|
Slow(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestSlowf(t *testing.T) {
|
|
|
|
func TestSlowf(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Slowf(context.Background(), "foo %s", "bar")
|
|
|
|
Slowf(context.Background(), "foo %s", "bar")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestSlowv(t *testing.T) {
|
|
|
|
func TestSlowv(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Slowv(context.Background(), "foo")
|
|
|
|
Slowv(context.Background(), "foo")
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestSloww(t *testing.T) {
|
|
|
|
func TestSloww(t *testing.T) {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf := logtest.NewCollector(t)
|
|
|
|
writer := logx.NewWriter(&buf)
|
|
|
|
|
|
|
|
old := logx.Reset()
|
|
|
|
|
|
|
|
logx.SetWriter(writer)
|
|
|
|
|
|
|
|
defer logx.SetWriter(old)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file, line := getFileLine()
|
|
|
|
file, line := getFileLine()
|
|
|
|
Sloww(context.Background(), "foo", Field("a", "b"))
|
|
|
|
Sloww(context.Background(), "foo", Field("a", "b"))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
|
|
|
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
|
|
|