restructured logger from budigt
This commit is contained in:
parent
358ee6acc0
commit
b2fd12b1c8
11 changed files with 1227 additions and 1 deletions
65
stream.go
Normal file
65
stream.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package splinter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Level mirrors slog.Level so callers don't need to import slog directly.
|
||||
type Level = slog.Level
|
||||
|
||||
const (
|
||||
LevelDebug Level = slog.LevelDebug
|
||||
LevelInfo Level = slog.LevelInfo
|
||||
LevelWarn Level = slog.LevelWarn
|
||||
LevelError Level = slog.LevelError
|
||||
)
|
||||
|
||||
// LevelFromString parses a level name (case-insensitive). Accepted:
|
||||
// "debug", "info", "warn"/"warning", "error". Unknown input returns LevelInfo.
|
||||
func LevelFromString(s string) Level {
|
||||
var l slog.Level
|
||||
if err := l.UnmarshalText([]byte(s)); err != nil {
|
||||
return LevelInfo
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// Record is a self-contained log entry passed to every Stream.
|
||||
type Record struct {
|
||||
Time time.Time
|
||||
Level Level
|
||||
Message string
|
||||
Attrs map[string]any
|
||||
}
|
||||
|
||||
// LevelLabel returns a human-friendly label for the record's level.
|
||||
func (r Record) LevelLabel() string {
|
||||
switch {
|
||||
case r.Level < LevelInfo:
|
||||
return "DEBUG"
|
||||
case r.Level < LevelWarn:
|
||||
return "INFO"
|
||||
case r.Level < LevelError:
|
||||
return "WARN"
|
||||
default:
|
||||
return "ERROR"
|
||||
}
|
||||
}
|
||||
|
||||
// Stream is the abstraction for a log output destination.
|
||||
type Stream interface {
|
||||
// Name returns a short identifier used in error reporting (e.g. "console").
|
||||
Name() string
|
||||
|
||||
// Write handles a single record. Implementations must be safe for
|
||||
// concurrent use.
|
||||
Write(ctx context.Context, rec Record) error
|
||||
|
||||
// Enabled reports whether the stream cares about the given level.
|
||||
Enabled(level Level) bool
|
||||
|
||||
// Close flushes and releases any resources. Called once by Logger.Close.
|
||||
Close() error
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue