2020-01-28 23:50:41 +00:00
|
|
|
package logging
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2018-10-03 19:37:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGRPCLogger(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
2020-01-28 23:50:41 +00:00
|
|
|
// Use a placeholder value for TimeFormat so we don't care about dates/times
|
|
|
|
logger := hclog.New(&hclog.LoggerOptions{
|
|
|
|
Level: hclog.Trace,
|
|
|
|
Output: &out,
|
|
|
|
TimeFormat: "timeformat",
|
|
|
|
})
|
2020-08-08 01:08:43 +00:00
|
|
|
grpclog.SetLoggerV2(NewGRPCLogger("TRACE", logger))
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
// All of these should output something
|
|
|
|
grpclog.Info("Info,")
|
|
|
|
grpclog.Infoln("Infoln")
|
2020-01-28 23:50:41 +00:00
|
|
|
grpclog.Infof("Infof: %d", 1)
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
grpclog.Warning("Warning,")
|
|
|
|
grpclog.Warningln("Warningln")
|
2020-01-28 23:50:41 +00:00
|
|
|
grpclog.Warningf("Warningf: %d", 1)
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
grpclog.Error("Error,")
|
|
|
|
grpclog.Errorln("Errorln")
|
2020-01-28 23:50:41 +00:00
|
|
|
grpclog.Errorf("Errorf: %d", 1)
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
// Fatal tests are hard... assume they are good!
|
2021-06-14 20:13:58 +00:00
|
|
|
expect := `timeformat [TRACE] Info,
|
|
|
|
timeformat [TRACE] Infoln
|
|
|
|
timeformat [TRACE] Infof: 1
|
2020-01-28 23:50:41 +00:00
|
|
|
timeformat [WARN] Warning,
|
|
|
|
timeformat [WARN] Warningln
|
|
|
|
timeformat [WARN] Warningf: 1
|
|
|
|
timeformat [ERROR] Error,
|
|
|
|
timeformat [ERROR] Errorln
|
|
|
|
timeformat [ERROR] Errorf: 1
|
2018-10-03 19:37:53 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
require.Equal(t, expect, out.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGRPCLogger_V(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
level string
|
|
|
|
v int
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"ERR", -1, false},
|
|
|
|
{"ERR", 0, false},
|
|
|
|
{"ERR", 1, false},
|
|
|
|
{"ERR", 2, false},
|
|
|
|
{"ERR", 3, false},
|
|
|
|
{"WARN", -1, false},
|
|
|
|
{"WARN", 0, false},
|
|
|
|
{"WARN", 1, false},
|
|
|
|
{"WARN", 2, false},
|
|
|
|
{"WARN", 3, false},
|
|
|
|
{"INFO", -1, true},
|
|
|
|
{"INFO", 0, true},
|
|
|
|
{"INFO", 1, false},
|
|
|
|
{"INFO", 2, false},
|
|
|
|
{"INFO", 3, false},
|
|
|
|
{"DEBUG", -1, true},
|
|
|
|
{"DEBUG", 0, true},
|
|
|
|
{"DEBUG", 1, true},
|
|
|
|
{"DEBUG", 2, false},
|
|
|
|
{"DEBUG", 3, false},
|
|
|
|
{"TRACE", -1, true},
|
|
|
|
{"TRACE", 0, true},
|
|
|
|
{"TRACE", 1, true},
|
|
|
|
{"TRACE", 2, true},
|
|
|
|
{"TRACE", 3, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s,%d", tt.level, tt.v), func(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := hclog.New(&hclog.LoggerOptions{
|
|
|
|
Name: t.Name(),
|
|
|
|
Level: hclog.Trace,
|
|
|
|
Output: &out,
|
|
|
|
})
|
2020-08-08 01:08:43 +00:00
|
|
|
grpclog.SetLoggerV2(NewGRPCLogger(tt.level, logger))
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
assert.Equal(t, tt.want, grpclog.V(tt.v))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|