44 lines
764 B
Go
44 lines
764 B
Go
|
package logger
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"log/slog"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestLogFmt(t *testing.T) {
|
||
|
|
||
|
var buf bytes.Buffer
|
||
|
jsonh := slog.NewJSONHandler(&buf, nil)
|
||
|
h := newLogFmtHandler(jsonh)
|
||
|
|
||
|
log := slog.New(h)
|
||
|
log.Info("test message", "id", 1010)
|
||
|
t.Logf("buf: %s", buf.String())
|
||
|
|
||
|
msg := map[string]any{}
|
||
|
|
||
|
err := json.Unmarshal(buf.Bytes(), &msg)
|
||
|
if err != nil {
|
||
|
t.Logf("couldn't unmarshal json log: %s", err)
|
||
|
t.Fail()
|
||
|
}
|
||
|
|
||
|
if msgTxt, ok := msg["msg"].(string); ok {
|
||
|
if !strings.Contains(msgTxt, "id=1010") {
|
||
|
t.Log("didn't find id in msg value")
|
||
|
t.Fail()
|
||
|
}
|
||
|
if strings.Contains(msgTxt, "level=") {
|
||
|
t.Log("msg value contains level=")
|
||
|
t.Fail()
|
||
|
}
|
||
|
} else {
|
||
|
t.Log("didn't find message in output")
|
||
|
t.Fail()
|
||
|
}
|
||
|
|
||
|
}
|