Skip to content

Commit

Permalink
fix: Write logs to stderr by default
Browse files Browse the repository at this point in the history
  • Loading branch information
macie committed Jan 1, 2024
1 parent 81a5246 commit b3661c7
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package boludo

import (
"context"
"fmt"
"io"
"log/slog"
"strings"
"os"
)

// UnstructuredHandler is a slog.Handler that writes all log records to stderr
Expand All @@ -19,6 +19,10 @@ type UnstructuredHandler struct {

// Level specifies a minimum level for log records.
Level slog.Level

// Output specifies a destination for log records.
// If nil, os.Stderr is used.
Output io.Writer
}

// WithAttrs implements slog.Handler.
Expand All @@ -36,23 +40,24 @@ func (u UnstructuredHandler) Enabled(_ context.Context, l slog.Level) bool {

// Handle writes log records to stderr.
func (u UnstructuredHandler) Handle(_ context.Context, r slog.Record) error {
line := strings.Builder{}
output := u.Output
if output == nil {
output = os.Stderr
}

if u.Prefix != "" {
line.WriteString(u.Prefix)
line.WriteString(" ")
output.Write([]byte(u.Prefix))
output.Write([]byte{' '})
}
line.WriteString(r.Level.String())
line.WriteString(" ")
line.WriteString(r.Message)

output.Write([]byte(r.Level.String()))
output.Write([]byte{' '})
output.Write([]byte(r.Message))
r.Attrs(func(a slog.Attr) bool {
line.WriteString(" ")
line.WriteString(a.String())
output.Write([]byte{' '})
output.Write([]byte(a.String()))
return true
})

fmt.Println(line.String())
output.Write([]byte{'\n'})

return nil
}

0 comments on commit b3661c7

Please sign in to comment.