1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-02-05 13:15:11 +02:00
kratos/log/global_test.go
realityone 0ed2e0f379
feat(log): add global logger appliance, as process level default logger (#1761)
* add global logger appliance, as process level default logger

* replace DefaultLogger as global logger
2022-01-12 15:34:49 +08:00

69 lines
1.5 KiB
Go

package log
import (
"bytes"
"fmt"
"strings"
"testing"
)
func TestGlobalLog(t *testing.T) {
buffer := &bytes.Buffer{}
SetLogger(NewStdLogger(buffer))
testCases := []struct {
level Level
content []interface{}
}{
{
LevelDebug,
[]interface{}{"test debug"},
},
{
LevelInfo,
[]interface{}{"test info"},
},
{
LevelInfo,
[]interface{}{"test %s", "info"},
},
{
LevelWarn,
[]interface{}{"test warn"},
},
{
LevelError,
[]interface{}{"test error"},
},
{
LevelError,
[]interface{}{"test %s", "error"},
},
}
expected := []string{}
for _, tc := range testCases {
msg := fmt.Sprintf(tc.content[0].(string), tc.content[1:]...)
switch tc.level {
case LevelDebug:
Debugf(tc.content[0].(string), tc.content[1:]...)
expected = append(expected, fmt.Sprintf("%s msg=%s", "DEBUG", msg))
case LevelInfo:
Infof(tc.content[0].(string), tc.content[1:]...)
expected = append(expected, fmt.Sprintf("%s msg=%s", "INFO", msg))
case LevelWarn:
Warnf(tc.content[0].(string), tc.content[1:]...)
expected = append(expected, fmt.Sprintf("%s msg=%s", "WARN", msg))
case LevelError:
Errorf(tc.content[0].(string), tc.content[1:]...)
expected = append(expected, fmt.Sprintf("%s msg=%s", "ERROR", msg))
}
}
expected = append(expected, "")
t.Logf("Content: %s", buffer.String())
if buffer.String() != strings.Join(expected, "\n") {
t.Errorf("Expected: %s, got: %s", strings.Join(expected, "\n"), buffer.String())
}
}