-
Notifications
You must be signed in to change notification settings - Fork 21
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
18 changed files
with
515 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/go-playground/log/v8" | ||
stdlog "log" | ||
"log/slog" | ||
) | ||
|
||
func main() { | ||
|
||
// This example demonstrates how to redirect the std log and slog to this logger by using it as | ||
// an slog.Handler. | ||
log.RedirectGoStdLog(true) | ||
log.WithFields(log.G("grouped", log.F("key", "value"))).Debug("test") | ||
stdlog.Println("test stdlog") | ||
slog.Info("test slog", slog.Group("group", "key", "value")) | ||
} |
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,24 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/go-playground/log/v8" | ||
slogredirect "github.com/go-playground/log/v8/handlers/slog" | ||
stdlog "log" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
func main() { | ||
|
||
// This example demonstrates how to redirect the std log and slog to this logger and output back out to any | ||
// slog.Handler, as well as any other handler(s) registered with this logger. | ||
log.AddHandler(slogredirect.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ | ||
ReplaceAttr: slogredirect.ReplaceAttrFn, // for custom log level output | ||
})), log.AllLevels...) | ||
log.WithFields(log.G("grouped", log.F("key", "value"))).Debug("test") | ||
stdlog.Println("test stdlog") | ||
slog.Info("test slog", slog.Group("group", "key", "value")) | ||
} |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
github.com/go-playground/errors/v5 v5.2.3 h1:RPxaFHgJZjgk/OFkUcfytJgRQKRINLtueVxgOdnfPpg= | ||
github.com/go-playground/errors/v5 v5.2.3/go.mod h1:DincxRGwraWmq39TZDqtnOtHGOJ+AbNbO0OmBzX6MLw= | ||
github.com/go-playground/pkg/v5 v5.6.0 h1:97YpRFzIcS5NFP2Uzxj8cPYz4zTuwweyXADYcA1KfUQ= | ||
github.com/go-playground/pkg/v5 v5.6.0/go.mod h1:TvZ2nNtNh6VfoNteY9ApA2BXt1ZwJliFZ4hzPAwLS9Y= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM= | ||
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||
github.com/go-playground/errors/v5 v5.3.1 h1:J2qU+9Whg863g3SATXKSJyFA91Zz85pYD3+obr5Oodk= | ||
github.com/go-playground/errors/v5 v5.3.1/go.mod h1:LcLhmzQ/RuEntAs9r38NSV+xtbHffhMx/1yuuEroc7M= | ||
github.com/go-playground/pkg/v5 v5.21.2 h1:DgVr88oMI3pfMFkEN9E6hp9YGG8NHc+019LRJfnUOfU= | ||
github.com/go-playground/pkg/v5 v5.21.2/go.mod h1:UgHNntEQnMJSygw2O2RQ3LAB0tprx81K90c/pOKh7cU= | ||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= | ||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0= | ||
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= |
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,59 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package slog | ||
|
||
import ( | ||
"context" | ||
log "github.com/go-playground/log/v8" | ||
"log/slog" | ||
) | ||
|
||
// Handler implementation. | ||
type Handler struct { | ||
handler slog.Handler | ||
} | ||
|
||
// New handler wraps an slog.Handler for log output. | ||
// | ||
// Calling this function automatically calls the slog.RedirectGoStdLog function in order to intercept and forward | ||
// the Go standard library log output to this handler. | ||
func New(handler slog.Handler) *Handler { | ||
log.RedirectGoStdLog(true) | ||
return &Handler{handler: handler} | ||
} | ||
|
||
// Log handles the log entry | ||
func (h *Handler) Log(e log.Entry) { | ||
r := slog.NewRecord(e.Timestamp, slog.Level(e.Level), e.Message, 0) | ||
r.AddAttrs(h.convertFields(e.Fields)...) | ||
_ = h.handler.Handle(context.Background(), r) | ||
} | ||
|
||
func (h *Handler) convertFields(fields []log.Field) []slog.Attr { | ||
attrs := make([]slog.Attr, 0, len(fields)) | ||
for _, f := range fields { | ||
switch t := f.Value.(type) { | ||
case []log.Field: | ||
a := h.convertFields(t) | ||
arr := make([]any, 0, len(a)) | ||
for _, v := range a { | ||
arr = append(arr, v) | ||
} | ||
attrs = append(attrs, slog.Group(f.Key, arr...)) | ||
default: | ||
attrs = append(attrs, slog.Any(f.Key, f.Value)) | ||
} | ||
} | ||
return attrs | ||
} | ||
|
||
// ReplaceAttrFn can be used with slog.HandlerOptions to replace attributes. | ||
// This function replaces the "level" attribute to get the custom log levels of this package. | ||
var ReplaceAttrFn = func(groups []string, a slog.Attr) slog.Attr { | ||
if a.Key == slog.LevelKey { | ||
level := log.Level(a.Value.Any().(slog.Level)) | ||
a.Value = slog.StringValue(level.String()) | ||
} | ||
return a | ||
} |
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 slog | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/go-playground/log/v8" | ||
"log/slog" | ||
"strings" | ||
"testing" | ||
"testing/slogtest" | ||
) | ||
|
||
func TestSlogRedirect(t *testing.T) { | ||
var buff bytes.Buffer | ||
log.AddHandler(New(slog.NewJSONHandler(&buff, &slog.HandlerOptions{ | ||
ReplaceAttr: ReplaceAttrFn, // for custom log level output | ||
})), log.AllLevels...) | ||
h := slog.Default().Handler() | ||
|
||
results := func() []map[string]any { | ||
var ms []map[string]any | ||
for _, line := range bytes.Split(buff.Bytes(), []byte{'\n'}) { | ||
if len(line) == 0 { | ||
continue | ||
} | ||
var m map[string]any | ||
if err := json.Unmarshal(line, &m); err != nil { | ||
panic(err) // In a real test, use t.Fatal. | ||
} | ||
ms = append(ms, m) | ||
} | ||
return ms | ||
} | ||
err := slogtest.TestHandler(h, results) | ||
if err != nil { | ||
// if a single error and is time key errors, is ok this logger always sets that. | ||
// sad this its the only way to hook into these errors because none of concrete and | ||
// Joined errors has no way to reach into them when not. | ||
if strings.Count(err.Error(), "\n") != 0 || !strings.Contains(err.Error(), "unexpected key \"time\": a Handler should ignore a zero Record.Time") { | ||
t.Fatal(err) | ||
} | ||
} | ||
} |
Oops, something went wrong.