From e36a26ef5f3718e4880cff66cd5df7ac487f7873 Mon Sep 17 00:00:00 2001 From: MarvinJWendt Date: Fri, 19 Jul 2024 23:59:37 +0200 Subject: [PATCH] docs: better example --- doc.go | 21 --------------------- examples_test.go | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/doc.go b/doc.go index 0339ae5..e403686 100644 --- a/doc.go +++ b/doc.go @@ -2,26 +2,5 @@ Package splitslog provides a handler that splits log records to different handlers based on their level. The most common use case is to split logs to stdout and stderr based on their level. - - func main() { - splitter := splitslog.Splitter{ - // Debug and info messages are printed to stdout. - slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil), - slog.LevelInfo: slog.NewJSONHandler(os.Stdout, nil), - // Warn and error messages are printed to stderr. - slog.LevelWarn: slog.NewJSONHandler(os.Stderr, nil), - slog.LevelError: slog.NewJSONHandler(os.Stderr, nil), - } - - handler := splitslog.NewSplitHandler(splitter) - logger := slog.New(handler) - - logger.Info("info message prints to stdout") - logger.Error("error message prints to stderr") - - // Output: - // stdout: {"time":"2023-09-07T16:56:22.563817+02:00","level":"INFO","msg":"info message prints to stdout"} - // stderr: {"time":"2023-09-07T16:56:22.564103+02:00","level":"ERROR","msg":"error message prints to stderr"} - } */ package splitslog diff --git a/examples_test.go b/examples_test.go index ef3a126..a24f473 100644 --- a/examples_test.go +++ b/examples_test.go @@ -7,11 +7,33 @@ import ( "atomicgo.dev/splitslog" ) +func Example_demo := splitslog.Splitter{ + // Debug and info messages are printed to stdout. + slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil), + slog.LevelInfo: slog.NewJSONHandler(os.Stdout, nil), + + // Warn and error messages are printed to stderr. + slog.LevelWarn: slog.NewJSONHandler(os.Stderr, nil), + slog.LevelError: slog.NewJSONHandler(os.Stderr, nil), + } + + handler := splitslog.NewSplitHandler(splitter) + logger := slog.New(handler) + + logger.Info("info message prints to stdout") + logger.Error("error message prints to stderr") + + // stdout: {"time":"2023-09-07T16:56:22.563817+02:00","level":"INFO","msg":"info message prints to stdout"} + // stderr: {"time":"2023-09-07T16:56:22.564103+02:00","level":"ERROR","msg":"error message prints to stderr"} +} + + func ExampleNewSplitHandler() { splitter := splitslog.Splitter{ // Debug and info messages are printed to stdout. slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil), slog.LevelInfo: slog.NewJSONHandler(os.Stdout, nil), + // Warn and error messages are printed to stderr. slog.LevelWarn: slog.NewJSONHandler(os.Stderr, nil), slog.LevelError: slog.NewJSONHandler(os.Stderr, nil),