-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
205 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package corelog | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
type namedHandler struct { | ||
name string | ||
attrs []slog.Attr | ||
group string | ||
} | ||
|
||
func (h namedHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
leveler := namedLeveler(h.name) | ||
return level >= leveler.Level() | ||
} | ||
|
||
func (h namedHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
return &namedHandler{ | ||
name: h.name, | ||
group: h.group, | ||
attrs: attrs, | ||
} | ||
} | ||
|
||
func (h namedHandler) WithGroup(name string) slog.Handler { | ||
return &namedHandler{ | ||
name: h.name, | ||
attrs: h.attrs, | ||
group: name, | ||
} | ||
} | ||
|
||
func (h namedHandler) Handle(ctx context.Context, record slog.Record) error { | ||
config := GetConfig(h.name) | ||
leveler := namedLeveler(h.name) | ||
opts := &slog.HandlerOptions{ | ||
AddSource: config.EnableSource, | ||
Level: leveler, | ||
ReplaceAttr: leveler.ReplaceAttr, | ||
} | ||
|
||
var output io.Writer | ||
switch config.Output { | ||
case OutputStderr: | ||
output = os.Stderr | ||
|
||
case OutputStdout: | ||
output = os.Stdout | ||
|
||
default: | ||
output = os.Stderr | ||
} | ||
|
||
var handler slog.Handler | ||
switch config.Format { | ||
case FormatText: | ||
handler = slog.NewTextHandler(output, opts) | ||
|
||
case FormatJSON: | ||
handler = slog.NewJSONHandler(output, opts) | ||
|
||
default: | ||
handler = slog.NewTextHandler(output, opts) | ||
} | ||
|
||
if len(h.attrs) > 0 { | ||
handler = handler.WithAttrs(h.attrs) | ||
} | ||
if len(h.group) > 0 { | ||
handler = handler.WithGroup(h.group) | ||
} | ||
return handler.Handle(ctx, record) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package corelog | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type TestHandler struct { | ||
attrs []slog.Attr | ||
group string | ||
records []slog.Record | ||
level slog.Level | ||
} | ||
|
||
var _ (slog.Handler) = (*TestHandler)(nil) | ||
|
||
func (h *TestHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
return level >= h.level | ||
} | ||
|
||
func (h *TestHandler) Handle(ctx context.Context, record slog.Record) error { | ||
h.records = append(h.records, record) | ||
return nil | ||
} | ||
|
||
func (h *TestHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
return &TestHandler{attrs: attrs, group: h.group, records: h.records} | ||
} | ||
|
||
func (h *TestHandler) WithGroup(name string) slog.Handler { | ||
return &TestHandler{attrs: h.attrs, group: name, records: h.records} | ||
} | ||
|
||
func TestHandlerWithLevelDebug(t *testing.T) { | ||
SetConfig(Config{Level: LevelDebug}) | ||
handler := namedHandler{name: "test"} | ||
|
||
assert.True(t, handler.Enabled(context.Background(), levelDebug)) | ||
assert.True(t, handler.Enabled(context.Background(), levelInfo)) | ||
assert.True(t, handler.Enabled(context.Background(), levelError)) | ||
assert.True(t, handler.Enabled(context.Background(), levelFatal)) | ||
} | ||
|
||
func TestHandlerWithLevelInfo(t *testing.T) { | ||
SetConfig(Config{Level: LevelInfo}) | ||
handler := namedHandler{name: "test"} | ||
|
||
assert.False(t, handler.Enabled(context.Background(), levelDebug)) | ||
assert.True(t, handler.Enabled(context.Background(), levelInfo)) | ||
assert.True(t, handler.Enabled(context.Background(), levelError)) | ||
assert.True(t, handler.Enabled(context.Background(), levelFatal)) | ||
} | ||
|
||
func TestHandlerWithLevelError(t *testing.T) { | ||
SetConfig(Config{Level: LevelError}) | ||
handler := namedHandler{name: "test"} | ||
|
||
assert.False(t, handler.Enabled(context.Background(), levelDebug)) | ||
assert.False(t, handler.Enabled(context.Background(), levelInfo)) | ||
assert.True(t, handler.Enabled(context.Background(), levelError)) | ||
assert.True(t, handler.Enabled(context.Background(), levelFatal)) | ||
} | ||
|
||
func TestHandlerWithLevelFatal(t *testing.T) { | ||
SetConfig(Config{Level: LevelFatal}) | ||
handler := namedHandler{name: "test"} | ||
|
||
assert.False(t, handler.Enabled(context.Background(), levelDebug)) | ||
assert.False(t, handler.Enabled(context.Background(), levelInfo)) | ||
assert.False(t, handler.Enabled(context.Background(), levelError)) | ||
assert.True(t, handler.Enabled(context.Background(), levelFatal)) | ||
} | ||
|
||
func TestHandlerWithAttrs(t *testing.T) { | ||
handler := namedHandler{name: "test"} | ||
attrs := []slog.Attr{slog.Any("extra", "value")} | ||
other := handler.WithAttrs(attrs) | ||
|
||
otherHandler, ok := other.(*namedHandler) | ||
require.True(t, ok) | ||
assert.Equal(t, "test", otherHandler.name) | ||
assert.Equal(t, "", otherHandler.group) | ||
assert.Equal(t, attrs, otherHandler.attrs) | ||
} | ||
|
||
func TestHandlerWithGroup(t *testing.T) { | ||
handler := namedHandler{name: "test"} | ||
other := handler.WithGroup("group") | ||
|
||
otherHandler, ok := other.(*namedHandler) | ||
require.True(t, ok) | ||
assert.Equal(t, "test", otherHandler.name) | ||
assert.Equal(t, "group", otherHandler.group) | ||
assert.Equal(t, []slog.Attr(nil), otherHandler.attrs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.