Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup #2

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 24 additions & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
package splitslog_test

import (
"atomicgo.dev/splitslog"
"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),
Expand Down
9 changes: 5 additions & 4 deletions splitslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

// Splitter is a map of log levels to handlers.
// The default log levels (slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError) must be present, otherwise the SplitHandler panics.
// The default log levels (slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError) must be present,
// otherwise the SplitHandler panics.
type Splitter map[slog.Level]slog.Handler

// SplitHandler is a handler that splits log records to different handlers based on their level.
Expand Down Expand Up @@ -41,8 +42,8 @@ func (h *SplitHandler) Enabled(ctx context.Context, level slog.Level) bool {
}

// Handle implements Handler.Handle.
func (h *SplitHandler) Handle(ctx context.Context, r slog.Record) error {
handler := h.getHandler(r.Level)
func (h *SplitHandler) Handle(ctx context.Context, record slog.Record) error {
handler := h.getHandler(record.Level)

for _, goa := range h.goas {
if goa.group != "" {
Expand All @@ -54,7 +55,7 @@ func (h *SplitHandler) Handle(ctx context.Context, r slog.Record) error {
}
}

return handler.Handle(ctx, r) //nolint:wrapcheck
return handler.Handle(ctx, record) //nolint:wrapcheck
}

// WithAttrs implements Handler.WithAttrs.
Expand Down
15 changes: 9 additions & 6 deletions splitslog_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package splitslog_test

import (
"atomicgo.dev/splitslog"
"bytes"
"encoding/json"
"log/slog"
"testing"
"testing/slogtest"

"atomicgo.dev/splitslog"
)

func TestSplitHandler(t *testing.T) {
t.Parallel()

var buf bytes.Buffer

splitter := splitslog.Splitter{
Expand All @@ -19,10 +22,10 @@ func TestSplitHandler(t *testing.T) {
slog.LevelError: slog.NewJSONHandler(&buf, nil),
}

h := splitslog.NewSplitHandler(splitter)
handler := splitslog.NewSplitHandler(splitter)

results := func() []map[string]any {
var ms []map[string]any
var resultMap []map[string]any

for _, line := range bytes.Split(buf.Bytes(), []byte{'\n'}) {
if len(line) == 0 {
Expand All @@ -34,13 +37,13 @@ func TestSplitHandler(t *testing.T) {
t.Fatal(err)
}

ms = append(ms, m)
resultMap = append(resultMap, m)
}

return ms
return resultMap
}
err := slogtest.TestHandler(h, results)

err := slogtest.TestHandler(handler, results)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading