Skip to content

Commit

Permalink
docs: better example
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jul 19, 2024
1 parent b1ec1d5 commit e36a26e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
21 changes: 0 additions & 21 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,33 @@ import (
"atomicgo.dev/splitslog"
)

func Example_demo := splitslog.Splitter{

Check failure on line 10 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected :=, expected (

Check failure on line 10 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

expected '(', found ':=' (typecheck)

Check failure on line 10 in examples_test.go

View workflow job for this annotation

GitHub Actions / Test Go code (ubuntu-latest)

expected '(', found ':='

Check failure on line 10 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected :=, expected (

Check failure on line 10 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

expected '(', found ':=' (typecheck)
// Debug and info messages are printed to stdout.
slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil),

Check failure on line 12 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected comma after top level declaration (typecheck)

Check failure on line 12 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in parameter list (typecheck)

Check failure on line 12 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected comma after top level declaration (typecheck)

Check failure on line 12 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in parameter list (typecheck)
slog.LevelInfo: slog.NewJSONHandler(os.Stdout, nil),

Check failure on line 13 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

expected declaration, found slog (typecheck)

Check failure on line 13 in examples_test.go

View workflow job for this annotation

GitHub Actions / lint

expected declaration, found slog (typecheck)

// 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),
Expand Down

0 comments on commit e36a26e

Please sign in to comment.