-
Notifications
You must be signed in to change notification settings - Fork 47
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
6 changed files
with
175 additions
and
93 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 was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
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,113 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package log | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
type slogHandler struct { | ||
Logger | ||
|
||
attrs []any // *slog.Attr or *slogGroup | ||
} | ||
|
||
type slogGroup struct { | ||
name string | ||
attrs []any // *slog.Attr or *slogGroup | ||
} | ||
|
||
func isSlogGroup(v any) bool { _, ok := v.(*slogGroup); return ok } | ||
|
||
func (h *slogHandler) Enabled(_ context.Context, level slog.Level) bool { | ||
switch level { | ||
case slog.LevelDebug: | ||
return h.Logger.Level <= DebugLevel | ||
case slog.LevelInfo: | ||
return h.Logger.Level <= InfoLevel | ||
case slog.LevelWarn: | ||
return h.Logger.Level <= WarnLevel | ||
case slog.LevelError: | ||
return h.Logger.Level <= ErrorLevel | ||
} | ||
return false | ||
} | ||
|
||
func (h *slogHandler) Handle(_ context.Context, r slog.Record) error { | ||
var e *Entry | ||
switch r.Level { | ||
case slog.LevelDebug: | ||
e = h.Logger.Debug() | ||
case slog.LevelInfo: | ||
e = h.Logger.Info() | ||
case slog.LevelWarn: | ||
e = h.Logger.Warn() | ||
case slog.LevelError: | ||
e = h.Logger.Error() | ||
default: | ||
e = h.Logger.Log() | ||
} | ||
e = h.output(e, h.attrs) | ||
r.Attrs(func(attr slog.Attr) bool { | ||
e = e.Any(attr.Key, attr.Value) | ||
return true | ||
}) | ||
e.Msg(r.Message) | ||
return nil | ||
} | ||
|
||
func (h *slogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
if len(h.attrs) != 0 && isSlogGroup(h.attrs[len(h.attrs)-1]) { | ||
h.withAttrs(h.attrs[len(h.attrs)-1].(*slogGroup), attrs) | ||
} else { | ||
for _, attr := range attrs { | ||
h.attrs = append(h.attrs, &attr) | ||
} | ||
} | ||
return h | ||
} | ||
|
||
func (h *slogHandler) withAttrs(group *slogGroup, attrs []slog.Attr) { | ||
if len(group.attrs) != 0 && isSlogGroup(group.attrs[len(group.attrs)-1]) { | ||
h.withAttrs(group.attrs[len(group.attrs)-1].(*slogGroup), attrs) | ||
} else { | ||
for _, attr := range attrs { | ||
group.attrs = append(group.attrs, &attr) | ||
} | ||
} | ||
} | ||
|
||
func (h *slogHandler) WithGroup(name string) slog.Handler { | ||
if len(h.attrs) != 0 && isSlogGroup(h.attrs[len(h.attrs)-1]) { | ||
group := h.attrs[len(h.attrs)-1].(*slogGroup) | ||
group.attrs = append(group.attrs, &slogGroup{name: name}) | ||
} else { | ||
h.attrs = append(h.attrs, &slogGroup{name: name}) | ||
} | ||
return h | ||
} | ||
|
||
func (h *slogHandler) output(e *Entry, attrs []any) *Entry { | ||
for _, v := range attrs { | ||
if group, ok := v.(*slogGroup); ok && group.name != "" { | ||
e.Dict(group.name, h.output(NewContext(nil), group.attrs).Value()) | ||
} else if attr, ok := v.(*slog.Attr); ok { | ||
e = e.Any(attr.Key, attr.Value) | ||
} | ||
} | ||
return e | ||
} | ||
|
||
// Slog wraps the Logger to provide *slog.Logger | ||
func (l *Logger) Slog() *slog.Logger { | ||
logger := *l | ||
switch { | ||
case logger.Caller > 0: | ||
logger.Caller += 3 | ||
case logger.Caller < 0: | ||
logger.Caller -= 3 | ||
} | ||
return slog.New(&slogHandler{Logger: logger}) | ||
} |
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,46 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package log | ||
|
||
import ( | ||
"log/slog" | ||
"testing" | ||
) | ||
|
||
func TestSlogNormal(t *testing.T) { | ||
var logger *slog.Logger = (&Logger{ | ||
Level: InfoLevel, | ||
TimeField: "date", | ||
TimeFormat: "2006-01-02", | ||
Caller: 1, | ||
}).Slog() | ||
|
||
logger.Info("hello from slog Info") | ||
logger.Warn("hello from slog Warn") | ||
logger.Error("hello from slog Error") | ||
} | ||
|
||
func TestSlogAttrs(t *testing.T) { | ||
var logger *slog.Logger = (&Logger{ | ||
Level: InfoLevel, | ||
TimeField: "date", | ||
TimeFormat: "2006-01-02", | ||
Caller: 1, | ||
}).Slog() | ||
|
||
sublogger := logger.With("logger", "attr_logger").With("everything", 42) | ||
sublogger.Info("hello from attr slog") | ||
} | ||
|
||
func TestSlogGroup(t *testing.T) { | ||
var logger *slog.Logger = (&Logger{ | ||
Level: InfoLevel, | ||
TimeField: "date", | ||
TimeFormat: "2006-01-02", | ||
Caller: 1, | ||
}).Slog() | ||
|
||
sublogger := logger.WithGroup("g1").With("logger", "attr_logger").WithGroup("g2").With("foo", "bar") | ||
sublogger.Info("hello from group slog") | ||
} |