mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-28 03:57:02 +02:00
test(log): increase tests coverage (#2197)
This commit is contained in:
parent
1451b9e0c0
commit
85af73a84b
@ -11,7 +11,12 @@ import (
|
||||
|
||||
func TestGlobalLog(t *testing.T) {
|
||||
buffer := &bytes.Buffer{}
|
||||
SetLogger(NewStdLogger(buffer))
|
||||
logger := NewStdLogger(buffer)
|
||||
SetLogger(logger)
|
||||
|
||||
if GetLogger() != logger {
|
||||
t.Error("GetLogger() is not equal to logger")
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
level Level
|
||||
@ -48,19 +53,38 @@ func TestGlobalLog(t *testing.T) {
|
||||
msg := fmt.Sprintf(tc.content[0].(string), tc.content[1:]...)
|
||||
switch tc.level {
|
||||
case LevelDebug:
|
||||
Debug(msg)
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "DEBUG", msg))
|
||||
Debugf(tc.content[0].(string), tc.content[1:]...)
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "DEBUG", msg))
|
||||
Debugw("log", msg)
|
||||
expected = append(expected, fmt.Sprintf("%s log=%s", "DEBUG", msg))
|
||||
case LevelInfo:
|
||||
Info(msg)
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "INFO", msg))
|
||||
Infof(tc.content[0].(string), tc.content[1:]...)
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "INFO", msg))
|
||||
Infow("log", msg)
|
||||
expected = append(expected, fmt.Sprintf("%s log=%s", "INFO", msg))
|
||||
case LevelWarn:
|
||||
Warn(msg)
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "WARN", msg))
|
||||
Warnf(tc.content[0].(string), tc.content[1:]...)
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "WARN", msg))
|
||||
Warnw("log", msg)
|
||||
expected = append(expected, fmt.Sprintf("%s log=%s", "WARN", msg))
|
||||
case LevelError:
|
||||
Error(msg)
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "ERROR", msg))
|
||||
Errorf(tc.content[0].(string), tc.content[1:]...)
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "ERROR", msg))
|
||||
Errorw("log", msg)
|
||||
expected = append(expected, fmt.Sprintf("%s log=%s", "ERROR", msg))
|
||||
}
|
||||
}
|
||||
Log(LevelInfo, DefaultMessageKey, "test log")
|
||||
expected = append(expected, fmt.Sprintf("%s msg=%s", "INFO", "test log"))
|
||||
|
||||
expected = append(expected, "")
|
||||
|
||||
t.Logf("Content: %s", buffer.String())
|
||||
@ -85,7 +109,7 @@ func TestGlobalLogUpdate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGolbalContext(t *testing.T) {
|
||||
func TestGlobalContext(t *testing.T) {
|
||||
buffer := &bytes.Buffer{}
|
||||
SetLogger(NewStdLogger(buffer))
|
||||
Context(context.Background()).Infof("111")
|
||||
|
@ -7,7 +7,8 @@ import (
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
logger := DefaultLogger
|
||||
logger = With(logger, "ts", DefaultTimestamp, "caller", DefaultCaller)
|
||||
logger = With(logger, "ts", DefaultTimestamp)
|
||||
logger = With(logger, "caller", DefaultCaller)
|
||||
_ = logger.Log(LevelInfo, "key1", "value1")
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user