logger/slog: add ability to pass func that creates slog.Handler compatible interface
All checks were successful
test / test (push) Successful in 42s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2024-12-07 16:16:45 +03:00
parent 0e66688f8f
commit d9b822deff
2 changed files with 35 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log"
"log/slog"
"strings"
"testing"
@@ -15,6 +16,23 @@ import (
"go.unistack.org/micro/v3/logger"
)
func TestWithHandlerFunc(t *testing.T) {
ctx := context.TODO()
buf := bytes.NewBuffer(nil)
l := NewLogger(logger.WithLevel(logger.InfoLevel), logger.WithOutput(buf),
WithHandlerFunc(slog.NewTextHandler),
)
if err := l.Init(); err != nil {
t.Fatal(err)
}
l.Info(ctx, "msg1")
if !bytes.Contains(buf.Bytes(), []byte(`msg=msg1`)) {
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
}
}
func TestWithAddFields(t *testing.T) {
ctx := context.TODO()
buf := bytes.NewBuffer(nil)