-
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.
Merge branch 'main' into config-prompt
- Loading branch information
Showing
10 changed files
with
303 additions
and
3 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
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
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
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
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
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
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,123 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"runtime" | ||
"strconv" | ||
"sync" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
type Handler struct { | ||
opts slog.HandlerOptions | ||
prefix string | ||
preformat string | ||
mu *sync.Mutex | ||
w io.Writer | ||
} | ||
|
||
func New(w io.Writer, opts *slog.HandlerOptions) *Handler { | ||
h := &Handler{w: w, mu: &sync.Mutex{}} | ||
if opts != nil { | ||
h.opts = *opts | ||
} | ||
if h.opts.Level == nil { | ||
h.opts.Level = slog.LevelInfo | ||
} | ||
return h | ||
} | ||
|
||
func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool { | ||
return level >= h.opts.Level.Level() | ||
} | ||
|
||
func (h *Handler) Handle(ctx context.Context, r slog.Record) error { | ||
level := r.Level.String() | ||
buf := make([]byte, 0, 1024) | ||
|
||
if !r.Time.IsZero() { | ||
buf = r.Time.AppendFormat(buf, time.RFC3339) | ||
buf = append(buf, ' ') | ||
} | ||
|
||
switch r.Level { | ||
case slog.LevelDebug: | ||
level = color.MagentaString(level) | ||
case slog.LevelInfo: | ||
level = color.CyanString(level) | ||
case slog.LevelWarn: | ||
level = color.YellowString(level) | ||
case slog.LevelError: | ||
level = color.RedString(level) | ||
} | ||
|
||
buf = append(buf, level...) | ||
buf = append(buf, ' ') | ||
if h.opts.AddSource && r.PC != 0 { | ||
fr := runtime.CallersFrames([]uintptr{r.PC}) | ||
f, _ := fr.Next() | ||
buf = append(buf, f.File...) | ||
buf = append(buf, ':') | ||
buf = strconv.AppendInt(buf, int64(f.Line), 10) | ||
buf = append(buf, ' ') | ||
} | ||
buf = append(buf, color.CyanString(r.Message)...) | ||
buf = append(buf, h.preformat...) | ||
r.Attrs(func(a slog.Attr) bool { | ||
buf = h.appendAttr(buf, h.prefix, a) | ||
return true | ||
}) | ||
buf = append(buf, '\n') | ||
h.mu.Lock() | ||
defer h.mu.Unlock() | ||
_, err := h.w.Write(buf) | ||
return err | ||
} | ||
|
||
func (h *Handler) appendAttr(buf []byte, prefix string, a slog.Attr) []byte { | ||
if a.Equal(slog.Attr{}) { | ||
return buf | ||
} | ||
if a.Value.Kind() != slog.KindGroup { | ||
buf = append(buf, ' ') | ||
buf = append(buf, prefix...) | ||
buf = append(buf, a.Key...) | ||
buf = append(buf, '=') | ||
return fmt.Appendf(buf, "%v", a.Value.Any()) | ||
} | ||
|
||
if a.Key != "" { | ||
prefix += a.Key + "." | ||
} | ||
for _, a := range a.Value.Group() { | ||
buf = h.appendAttr(buf, prefix, a) | ||
} | ||
return buf | ||
} | ||
|
||
func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
var buf []byte | ||
for _, a := range attrs { | ||
buf = h.appendAttr(buf, h.prefix, a) | ||
} | ||
return &Handler{ | ||
w: h.w, | ||
opts: h.opts, | ||
prefix: h.prefix, | ||
preformat: h.preformat + string(buf), | ||
} | ||
} | ||
|
||
func (h *Handler) WithGroup(name string) slog.Handler { | ||
return &Handler{ | ||
w: h.w, | ||
opts: h.opts, | ||
preformat: h.preformat, | ||
prefix: h.prefix + name + ".", | ||
} | ||
} |
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,138 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"time" | ||
|
||
"log/slog" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var testTime = time.Date(2023, time.September, 17, 06, 44, 13, 0, time.UTC) | ||
|
||
func TestInfoLevelOutput(t *testing.T) { | ||
assert := assert.New(t) | ||
var buf bytes.Buffer | ||
|
||
h := New(&buf, nil) | ||
logger := slog.New(logtimeHandler{testTime, h}) | ||
logger.Log(context.Background(), slog.LevelInfo, "testing jerm custom slog handler") | ||
|
||
actual := buf.String() | ||
actual = actual[:len(actual)-1] | ||
expected := "2023-09-17T06:44:13Z INFO testing jerm custom slog handler" | ||
assert.Equal(expected, actual) | ||
} | ||
|
||
func TestDebugLevelOutput(t *testing.T) { | ||
assert := assert.New(t) | ||
var buf bytes.Buffer | ||
|
||
logLevel := &slog.LevelVar{} | ||
logLevel.Set(slog.LevelDebug) | ||
h := New(&buf, &slog.HandlerOptions{ | ||
Level: logLevel, | ||
}) | ||
logger := slog.New(logtimeHandler{testTime, h}) | ||
logger.Log(context.Background(), slog.LevelDebug, "testing jerm custom slog handler") | ||
|
||
actual := buf.String() | ||
actual = actual[:len(actual)-1] | ||
expected := "2023-09-17T06:44:13Z DEBUG testing jerm custom slog handler" | ||
assert.Equal(expected, actual) | ||
} | ||
|
||
func TestErrorLevelOutput(t *testing.T) { | ||
assert := assert.New(t) | ||
var buf bytes.Buffer | ||
|
||
logLevel := &slog.LevelVar{} | ||
logLevel.Set(slog.LevelError) | ||
h := New(&buf, &slog.HandlerOptions{ | ||
Level: logLevel, | ||
}) | ||
logger := slog.New(logtimeHandler{testTime, h}) | ||
logger.Log(context.Background(), slog.LevelError, "testing jerm custom slog handler") | ||
|
||
actual := buf.String() | ||
actual = actual[:len(actual)-1] | ||
expected := "2023-09-17T06:44:13Z ERROR testing jerm custom slog handler" | ||
assert.Equal(expected, actual) | ||
} | ||
|
||
func TestWarnLevelOutput(t *testing.T) { | ||
assert := assert.New(t) | ||
var buf bytes.Buffer | ||
|
||
logLevel := &slog.LevelVar{} | ||
logLevel.Set(slog.LevelWarn) | ||
h := New(&buf, &slog.HandlerOptions{ | ||
Level: logLevel, | ||
}) | ||
logger := slog.New(logtimeHandler{testTime, h}) | ||
logger.Log(context.Background(), slog.LevelWarn, "testing jerm custom slog handler") | ||
|
||
actual := buf.String() | ||
actual = actual[:len(actual)-1] | ||
expected := "2023-09-17T06:44:13Z WARN testing jerm custom slog handler" | ||
assert.Equal(expected, actual) | ||
} | ||
|
||
func TestOutputWithAttr(t *testing.T) { | ||
assert := assert.New(t) | ||
var buf bytes.Buffer | ||
|
||
h := New(&buf, nil) | ||
logger := slog.New(logtimeHandler{testTime, h}) | ||
attr := []slog.Attr{slog.String("t", "test"), slog.Bool("b", true)} | ||
logger.LogAttrs(context.Background(), slog.LevelInfo, "testing jerm custom slog handler", attr...) | ||
|
||
actual := buf.String() | ||
actual = actual[:len(actual)-1] | ||
expected := "2023-09-17T06:44:13Z INFO testing jerm custom slog handler t=test b=true" | ||
assert.Equal(expected, actual) | ||
} | ||
|
||
func TestOutputWithGroup(t *testing.T) { | ||
assert := assert.New(t) | ||
var buf bytes.Buffer | ||
|
||
h := New(&buf, nil) | ||
logger := slog.New(logtimeHandler{testTime, h}) | ||
attr := []slog.Attr{ | ||
slog.String("t", "test"), | ||
slog.Group("g", slog.Int("a", 1), slog.Int("b", 2)), | ||
slog.Bool("b", true), | ||
} | ||
logger.LogAttrs(context.Background(), slog.LevelInfo, "testing jerm custom slog handler", attr...) | ||
|
||
actual := buf.String() | ||
actual = actual[:len(actual)-1] | ||
expected := "2023-09-17T06:44:13Z INFO testing jerm custom slog handler t=test g.a=1 g.b=2 b=true" | ||
assert.Equal(expected, actual) | ||
} | ||
|
||
type logtimeHandler struct { | ||
t time.Time | ||
h slog.Handler | ||
} | ||
|
||
func (h logtimeHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
return h.h.Enabled(ctx, level) | ||
} | ||
|
||
func (h logtimeHandler) WithGroup(name string) slog.Handler { | ||
return logtimeHandler{h.t, h.h.WithGroup(name)} | ||
} | ||
|
||
func (h logtimeHandler) Handle(ctx context.Context, r slog.Record) error { | ||
r.Time = h.t | ||
return h.h.Handle(ctx, r) | ||
} | ||
|
||
func (h logtimeHandler) WithAttrs(as []slog.Attr) slog.Handler { | ||
return logtimeHandler{h.t, h.h.WithAttrs(as)} | ||
} |
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
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