2023-10-17 01:48:50 +03:00
|
|
|
package slog
|
2020-02-29 03:31:59 +03:00
|
|
|
|
2020-03-26 01:00:43 +03:00
|
|
|
import (
|
2021-07-05 22:32:47 +03:00
|
|
|
"bytes"
|
2021-01-10 18:56:39 +03:00
|
|
|
"context"
|
2021-08-06 13:45:11 +03:00
|
|
|
"log"
|
2020-03-26 01:00:43 +03:00
|
|
|
"testing"
|
2023-10-17 01:48:50 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
2020-03-26 01:00:43 +03:00
|
|
|
)
|
2020-02-29 03:31:59 +03:00
|
|
|
|
2024-03-04 23:42:15 +03:00
|
|
|
func TestError(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf), logger.WithStacktrace(true))
|
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l.Error(ctx, "message")
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`"stacktrace":"`)) {
|
|
|
|
t.Fatalf("logger stacktrace not works, buf contains: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 15:29:21 +03:00
|
|
|
func TestContext(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2023-10-17 01:48:50 +03:00
|
|
|
l := NewLogger(logger.WithLevel(logger.TraceLevel), logger.WithOutput(buf))
|
2022-01-20 15:29:21 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-10-17 01:48:50 +03:00
|
|
|
nl, ok := logger.FromContext(logger.NewContext(ctx, l.Attrs("key", "val")))
|
2022-01-20 15:29:21 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("context without logger")
|
|
|
|
}
|
|
|
|
nl.Info(ctx, "message")
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) {
|
|
|
|
t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func TestAttrs(t *testing.T) {
|
2022-01-20 00:28:09 +03:00
|
|
|
ctx := context.TODO()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2023-10-17 01:48:50 +03:00
|
|
|
l := NewLogger(logger.WithLevel(logger.TraceLevel), logger.WithOutput(buf))
|
2022-01-20 00:28:09 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
nl := l.Attrs("key", "val")
|
2023-01-30 00:17:29 +03:00
|
|
|
|
|
|
|
nl.Info(ctx, "message")
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) {
|
|
|
|
t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromContextWithFields(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
var ok bool
|
2023-10-17 01:48:50 +03:00
|
|
|
l := NewLogger(logger.WithLevel(logger.TraceLevel), logger.WithOutput(buf))
|
2023-01-30 00:17:29 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-16 23:43:39 +03:00
|
|
|
nl := l.Attrs("key", "val")
|
2023-01-30 00:17:29 +03:00
|
|
|
|
2023-10-17 01:48:50 +03:00
|
|
|
ctx = logger.NewContext(ctx, nl)
|
2023-01-30 00:17:29 +03:00
|
|
|
|
2023-10-17 01:48:50 +03:00
|
|
|
l, ok = logger.FromContext(ctx)
|
2023-01-30 00:17:29 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("context does not have logger")
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Info(ctx, "message")
|
2022-01-20 00:28:09 +03:00
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) {
|
|
|
|
t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:19:35 +03:00
|
|
|
func TestClone(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2023-10-17 01:48:50 +03:00
|
|
|
l := NewLogger(logger.WithLevel(logger.TraceLevel), logger.WithOutput(buf))
|
2021-08-30 16:19:35 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-17 01:48:50 +03:00
|
|
|
nl := l.Clone(logger.WithLevel(logger.ErrorLevel))
|
2021-08-30 16:19:35 +03:00
|
|
|
if err := nl.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
nl.Info(ctx, "info message")
|
|
|
|
if len(buf.Bytes()) != 0 {
|
|
|
|
t.Fatal("message must not be logged")
|
|
|
|
}
|
|
|
|
l.Info(ctx, "info message")
|
|
|
|
if len(buf.Bytes()) == 0 {
|
|
|
|
t.Fatal("message must be logged")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:45:11 +03:00
|
|
|
func TestRedirectStdLogger(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2023-10-17 01:48:50 +03:00
|
|
|
l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf))
|
2021-08-06 13:45:11 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-17 01:48:50 +03:00
|
|
|
fn := logger.RedirectStdLogger(l, logger.ErrorLevel)
|
2021-08-06 13:45:11 +03:00
|
|
|
defer fn()
|
|
|
|
log.Print("test")
|
2023-10-14 18:59:27 +03:00
|
|
|
if !(bytes.Contains(buf.Bytes(), []byte(`"level":"error"`)) && bytes.Contains(buf.Bytes(), []byte(`"msg":"test"`))) {
|
2021-08-06 13:45:11 +03:00
|
|
|
t.Fatalf("logger error, buf %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdLogger(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2023-10-17 01:48:50 +03:00
|
|
|
l := NewLogger(logger.WithLevel(logger.TraceLevel), logger.WithOutput(buf))
|
2021-08-06 13:45:11 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-17 01:48:50 +03:00
|
|
|
lg := logger.NewStdLogger(l, logger.ErrorLevel)
|
2021-08-06 13:45:11 +03:00
|
|
|
lg.Print("test")
|
2023-10-14 18:59:27 +03:00
|
|
|
if !(bytes.Contains(buf.Bytes(), []byte(`"level":"error"`)) && bytes.Contains(buf.Bytes(), []byte(`"msg":"test"`))) {
|
2021-08-06 13:45:11 +03:00
|
|
|
t.Fatalf("logger error, buf %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 03:31:59 +03:00
|
|
|
func TestLogger(t *testing.T) {
|
2021-01-10 18:56:39 +03:00
|
|
|
ctx := context.TODO()
|
2021-07-05 23:04:20 +03:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2023-10-17 01:48:50 +03:00
|
|
|
l := NewLogger(logger.WithLevel(logger.TraceLevel), logger.WithOutput(buf))
|
2020-11-04 00:38:12 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-01-10 18:56:39 +03:00
|
|
|
l.Trace(ctx, "trace_msg1")
|
2023-10-14 18:59:27 +03:00
|
|
|
l.Warn(ctx, "warn_msg1")
|
2023-10-16 23:43:39 +03:00
|
|
|
l.Attrs("error", "test").Info(ctx, "error message")
|
2023-10-14 18:59:27 +03:00
|
|
|
l.Warn(ctx, "first second")
|
|
|
|
|
|
|
|
if !(bytes.Contains(buf.Bytes(), []byte(`"level":"trace"`)) && bytes.Contains(buf.Bytes(), []byte(`"msg":"trace_msg1"`))) {
|
2023-10-14 16:58:40 +03:00
|
|
|
t.Fatalf("logger tracer, buf %s", buf.Bytes())
|
|
|
|
}
|
2023-10-14 18:59:27 +03:00
|
|
|
if !(bytes.Contains(buf.Bytes(), []byte(`"level":"warn"`)) && bytes.Contains(buf.Bytes(), []byte(`"msg":"warn_msg1"`))) {
|
|
|
|
t.Fatalf("logger warn, buf %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
if !(bytes.Contains(buf.Bytes(), []byte(`"level":"info"`)) && bytes.Contains(buf.Bytes(), []byte(`"msg":"error message","error":"test"`))) {
|
|
|
|
t.Fatalf("logger info, buf %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
if !(bytes.Contains(buf.Bytes(), []byte(`"level":"warn"`)) && bytes.Contains(buf.Bytes(), []byte(`"msg":"first second"`))) {
|
|
|
|
t.Fatalf("logger warn, buf %s", buf.Bytes())
|
|
|
|
}
|
2020-02-29 03:31:59 +03:00
|
|
|
}
|