Bumps the go-modules-updates group with 11 updates in the /backend directory: | Package | From | To | | --- | --- | --- | | [github.com/PuerkitoBio/goquery](https://github.com/PuerkitoBio/goquery) | `1.10.2` | `1.11.0` | | [github.com/alecthomas/chroma/v2](https://github.com/alecthomas/chroma) | `2.15.0` | `2.20.0` | | [github.com/go-chi/chi/v5](https://github.com/go-chi/chi) | `5.2.1` | `5.2.3` | | [github.com/go-chi/cors](https://github.com/go-chi/cors) | `1.2.1` | `1.2.2` | | [github.com/go-pkgz/jrpc](https://github.com/go-pkgz/jrpc) | `0.3.1` | `0.4.0` | | [github.com/go-pkgz/lgr](https://github.com/go-pkgz/lgr) | `0.12.0` | `0.12.1` | | [github.com/go-pkgz/rest](https://github.com/go-pkgz/rest) | `1.20.3` | `1.20.4` | | [github.com/golang-jwt/jwt/v5](https://github.com/golang-jwt/jwt) | `5.2.2` | `5.3.0` | | [github.com/stretchr/testify](https://github.com/stretchr/testify) | `1.10.0` | `1.11.1` | | [go.etcd.io/bbolt](https://github.com/etcd-io/bbolt) | `1.4.0` | `1.4.3` | | [golang.org/x/image](https://github.com/golang/image) | `0.26.0` | `0.33.0` | Updates `github.com/PuerkitoBio/goquery` from 1.10.2 to 1.11.0 - [Release notes](https://github.com/PuerkitoBio/goquery/releases) - [Commits](https://github.com/PuerkitoBio/goquery/compare/v1.10.2...v1.11.0) Updates `github.com/alecthomas/chroma/v2` from 2.15.0 to 2.20.0 - [Release notes](https://github.com/alecthomas/chroma/releases) - [Commits](https://github.com/alecthomas/chroma/compare/v2.15.0...v2.20.0) Updates `github.com/go-chi/chi/v5` from 5.2.1 to 5.2.3 - [Release notes](https://github.com/go-chi/chi/releases) - [Changelog](https://github.com/go-chi/chi/blob/master/CHANGELOG.md) - [Commits](https://github.com/go-chi/chi/compare/v5.2.1...v5.2.3) Updates `github.com/go-chi/cors` from 1.2.1 to 1.2.2 - [Release notes](https://github.com/go-chi/cors/releases) - [Commits](https://github.com/go-chi/cors/compare/v1.2.1...v1.2.2) Updates `github.com/go-pkgz/jrpc` from 0.3.1 to 0.4.0 - [Release notes](https://github.com/go-pkgz/jrpc/releases) - [Commits](https://github.com/go-pkgz/jrpc/compare/v0.3.1...v0.4.0) Updates `github.com/go-pkgz/lgr` from 0.12.0 to 0.12.1 - [Release notes](https://github.com/go-pkgz/lgr/releases) - [Commits](https://github.com/go-pkgz/lgr/compare/v0.12.0...v0.12.1) Updates `github.com/go-pkgz/rest` from 1.20.3 to 1.20.4 - [Release notes](https://github.com/go-pkgz/rest/releases) - [Commits](https://github.com/go-pkgz/rest/compare/v1.20.3...v1.20.4) Updates `github.com/golang-jwt/jwt/v5` from 5.2.2 to 5.3.0 - [Release notes](https://github.com/golang-jwt/jwt/releases) - [Commits](https://github.com/golang-jwt/jwt/compare/v5.2.2...v5.3.0) Updates `github.com/stretchr/testify` from 1.10.0 to 1.11.1 - [Release notes](https://github.com/stretchr/testify/releases) - [Commits](https://github.com/stretchr/testify/compare/v1.10.0...v1.11.1) Updates `go.etcd.io/bbolt` from 1.4.0 to 1.4.3 - [Release notes](https://github.com/etcd-io/bbolt/releases) - [Commits](https://github.com/etcd-io/bbolt/compare/v1.4.0...v1.4.3) Updates `golang.org/x/image` from 0.26.0 to 0.33.0 - [Commits](https://github.com/golang/image/compare/v0.26.0...v0.33.0) --- updated-dependencies: - dependency-name: github.com/PuerkitoBio/goquery dependency-version: 1.11.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/alecthomas/chroma/v2 dependency-version: 2.20.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-chi/chi/v5 dependency-version: 5.2.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-chi/cors dependency-version: 1.2.2 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/jrpc dependency-version: 0.4.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/lgr dependency-version: 0.12.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/rest dependency-version: 1.20.4 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/golang-jwt/jwt/v5 dependency-version: 5.3.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/stretchr/testify dependency-version: 1.11.1 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: go.etcd.io/bbolt dependency-version: 1.4.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: golang.org/x/image dependency-version: 0.33.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates ... Signed-off-by: dependabot[bot] <support@github.com>
229 lines
5.6 KiB
Go
229 lines
5.6 KiB
Go
package lgr
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// ToSlogHandler converts lgr.L to slog.Handler
|
|
func ToSlogHandler(l L) slog.Handler {
|
|
return &lgrSlogHandler{lgr: l}
|
|
}
|
|
|
|
// FromSlogHandler creates lgr.L wrapper around slog.Handler
|
|
func FromSlogHandler(h slog.Handler) L {
|
|
return &slogLgrAdapter{handler: h}
|
|
}
|
|
|
|
// SetupWithSlog sets up the global logger with a slog logger
|
|
func SetupWithSlog(logger *slog.Logger) {
|
|
options := []Option{SlogHandler(logger.Handler())}
|
|
|
|
// check if the slog handler is enabled for debug level
|
|
// if so, enable debug mode in lgr to prevent filtering
|
|
if logger.Handler().Enabled(context.Background(), slog.LevelDebug) {
|
|
options = append(options, Debug)
|
|
}
|
|
|
|
Setup(options...)
|
|
}
|
|
|
|
// lgrSlogHandler implements slog.Handler using lgr.L
|
|
type lgrSlogHandler struct {
|
|
lgr L
|
|
attrs []slog.Attr
|
|
groups []string
|
|
}
|
|
|
|
// Enabled implements slog.Handler
|
|
func (h *lgrSlogHandler) Enabled(_ context.Context, level slog.Level) bool {
|
|
switch {
|
|
case level < slog.LevelInfo: // debug, Trace
|
|
// check if underlying lgr logger is configured to show debug
|
|
// since we can't directly query lgr's debug status, we assume enabled
|
|
return true
|
|
default:
|
|
return true
|
|
}
|
|
}
|
|
|
|
// Handle implements slog.Handler
|
|
func (h *lgrSlogHandler) Handle(_ context.Context, record slog.Record) error {
|
|
level := levelToString(record.Level)
|
|
|
|
// build message with attributes
|
|
msg := record.Message
|
|
|
|
// add time if record has it, otherwise current time is used by lgr
|
|
var timeStr string
|
|
if !record.Time.IsZero() {
|
|
timeStr = record.Time.Format("2006/01/02 15:04:05.000 ")
|
|
}
|
|
|
|
// format attributes as key=value pairs
|
|
var attrs strings.Builder
|
|
if len(h.attrs) > 0 || record.NumAttrs() > 0 {
|
|
attrs.WriteString(" ")
|
|
}
|
|
|
|
// add pre-defined attributes
|
|
for _, attr := range h.attrs {
|
|
attrs.WriteString(formatAttr(attr, h.groups))
|
|
}
|
|
|
|
// add record attributes
|
|
record.Attrs(func(attr slog.Attr) bool {
|
|
attrs.WriteString(formatAttr(attr, h.groups))
|
|
return true
|
|
})
|
|
|
|
// combine everything into final message
|
|
logMsg := fmt.Sprintf("%s%s %s%s", timeStr, level, msg, attrs.String())
|
|
h.lgr.Logf(logMsg)
|
|
return nil
|
|
}
|
|
|
|
// WithAttrs implements slog.Handler
|
|
func (h *lgrSlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
newHandler := &lgrSlogHandler{
|
|
lgr: h.lgr,
|
|
attrs: append(h.attrs, attrs...),
|
|
groups: h.groups,
|
|
}
|
|
return newHandler
|
|
}
|
|
|
|
// WithGroup implements slog.Handler
|
|
func (h *lgrSlogHandler) WithGroup(name string) slog.Handler {
|
|
newHandler := &lgrSlogHandler{
|
|
lgr: h.lgr,
|
|
attrs: h.attrs,
|
|
groups: append(h.groups, name),
|
|
}
|
|
return newHandler
|
|
}
|
|
|
|
// slogLgrAdapter implements lgr.L using slog.Handler
|
|
type slogLgrAdapter struct {
|
|
handler slog.Handler
|
|
}
|
|
|
|
// Logf implements lgr.L interface
|
|
func (a *slogLgrAdapter) Logf(format string, args ...interface{}) {
|
|
// parse log level from the beginning of the message
|
|
msg := fmt.Sprintf(format, args...)
|
|
level, msg := extractLevel(msg)
|
|
|
|
// create a record with caller information
|
|
// skip level is critical:
|
|
// - 0 = this line
|
|
// - 1 = this function (Logf)
|
|
// - 2 = caller of Logf (user code)
|
|
//
|
|
// note: We use PC=0 to ensure slog.Record.PC() returns 0,
|
|
// which causes slog to skip obtaining the caller info itself
|
|
record := slog.NewRecord(time.Now(), stringToLevel(level), msg, 2)
|
|
|
|
// we need to manually add the source information ourselves, since
|
|
// slog.Handler might have AddSource=true but won't get the caller
|
|
// right due to how we're adapting lgr → slog
|
|
pc, file, line, ok := runtime.Caller(2) // skip to caller of Logf
|
|
if ok {
|
|
// only add source info if we can find it
|
|
funcName := runtime.FuncForPC(pc).Name()
|
|
record.AddAttrs(
|
|
slog.Group("source",
|
|
slog.String("function", funcName),
|
|
slog.String("file", file),
|
|
slog.Int("line", line),
|
|
),
|
|
)
|
|
}
|
|
|
|
// handle the record
|
|
if err := a.handler.Handle(context.Background(), record); err != nil {
|
|
// if handling fails, fallback to stderr
|
|
fmt.Fprintf(os.Stderr, "slog handler error: %v\n", err)
|
|
}
|
|
}
|
|
|
|
// Helper functions
|
|
|
|
// levelToString converts slog.Level to string representation used by lgr
|
|
func levelToString(level slog.Level) string {
|
|
switch {
|
|
case level < slog.LevelInfo:
|
|
if level <= slog.LevelDebug-4 {
|
|
return "TRACE"
|
|
}
|
|
return "DEBUG"
|
|
case level < slog.LevelWarn:
|
|
return "INFO"
|
|
case level < slog.LevelError:
|
|
return "WARN"
|
|
default:
|
|
return "ERROR"
|
|
}
|
|
}
|
|
|
|
// stringToLevel converts lgr level string to slog.Level
|
|
func stringToLevel(level string) slog.Level {
|
|
switch level {
|
|
case "TRACE":
|
|
return slog.LevelDebug - 4
|
|
case "DEBUG":
|
|
return slog.LevelDebug
|
|
case "INFO":
|
|
return slog.LevelInfo
|
|
case "WARN":
|
|
return slog.LevelWarn
|
|
case "ERROR", "PANIC", "FATAL":
|
|
return slog.LevelError
|
|
default:
|
|
return slog.LevelInfo
|
|
}
|
|
}
|
|
|
|
// extractLevel parses lgr-style log message to extract level prefix
|
|
func extractLevel(msg string) (level, message string) {
|
|
for _, lvl := range levels {
|
|
prefix := lvl + " "
|
|
bracketPrefix := "[" + lvl + "] "
|
|
|
|
if strings.HasPrefix(msg, prefix) {
|
|
return lvl, strings.TrimPrefix(msg, prefix)
|
|
}
|
|
if strings.HasPrefix(msg, bracketPrefix) {
|
|
return lvl, strings.TrimPrefix(msg, bracketPrefix)
|
|
}
|
|
}
|
|
|
|
return "INFO", msg
|
|
}
|
|
|
|
// formatAttr converts slog.Attr to string representation
|
|
func formatAttr(attr slog.Attr, groups []string) string {
|
|
if attr.Equal(slog.Attr{}) {
|
|
return ""
|
|
}
|
|
|
|
key := attr.Key
|
|
if len(groups) > 0 {
|
|
key = strings.Join(groups, ".") + "." + key
|
|
}
|
|
|
|
val := attr.Value.String()
|
|
|
|
// handle string values specially by quoting them
|
|
if attr.Value.Kind() == slog.KindString {
|
|
val = fmt.Sprintf("%q", attr.Value.String())
|
|
}
|
|
|
|
return fmt.Sprintf("%s=%s ", key, val)
|
|
}
|