Add testcase && hook logger for server
Some checks failed
lint / lint (pull_request) Has been cancelled
pr / test (pull_request) Has been cancelled

This commit is contained in:
2024-11-24 01:15:07 +03:00
parent 7cd7fb0c0a
commit f33dcd628a
6 changed files with 241 additions and 36 deletions

View File

@@ -69,6 +69,7 @@ func (s *slogLogger) renameAttr(_ []string, a slog.Attr) slog.Attr {
type slogLogger struct {
leveler *slog.LevelVar
handler slog.Handler
slog slog.Logger
opts logger.Options
mu sync.RWMutex
}

View File

@@ -235,3 +235,29 @@ func Test_WithContextAttrFunc(t *testing.T) {
t.Fatalf("logger info, buf %s", buf.Bytes())
}
}
func Test_CloneAttrInHandler(t *testing.T) {
ctx := context.TODO()
buf := bytes.NewBuffer(nil)
log := NewLogger(logger.WithLevel(logger.DebugLevel), logger.WithOutput(buf), logger.WithAddSource(false))
if err := log.Init(); err != nil {
t.Fatal(err)
}
log = log.Fields("key", "val")
nlog := log.Clone(logger.WithLevel(logger.InfoLevel))
if err := nlog.Init(); err != nil {
t.Fatal(err)
}
nlog.Info(ctx, "new log info message")
log.Info(ctx, "original log info message")
/* Buffer has:
{"timestamp":"2024-11-24T01:12:30.752866543+03:00","level":"info","msg":"new log info message"}
{"timestamp":"2024-11-24T01:12:35.528170034+03:00","level":"info","msg":"original log info message","key":"val"}
*/
}