generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
50 lines (38 loc) · 1.62 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package splitslog_test
import (
"log/slog"
"os"
"atomicgo.dev/splitslog"
)
func Example_demo() {
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")
// 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),
}
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"}
}