Skip to content

Commit

Permalink
Fix stdlog/slog [#3612]
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Jun 28, 2024
1 parent 2ba68c9 commit eb762d0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/accumulated/run/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package run
import (
"encoding/json"
"io"
"log"
"log/slog"
"os"
"strings"
Expand Down Expand Up @@ -71,6 +72,9 @@ func (l *Logging) start(inst *Instance) error {
inst.logger = slog.New(h)
slog.SetDefault(inst.logger)

// TODO explain me
log.SetOutput(&logging.StdlogWriter{Handler: h})

return nil
}

Expand Down
34 changes: 33 additions & 1 deletion internal/logging/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"context"
"fmt"
"io"
stdlog "log"
"log/slog"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -130,6 +132,9 @@ func (h *logHandler) WithGroup(name string) slog.Handler {
}

func (h *logHandler) Enabled(ctx context.Context, level slog.Level) bool {
if ctx.Value(isStdlog) != nil {
return true
}
if level < h.lowestLevel {
return false
}
Expand All @@ -141,7 +146,8 @@ func (h *logHandler) Enabled(ctx context.Context, level slog.Level) bool {

func (h *logHandler) Handle(ctx context.Context, record slog.Record) error {
record.AddAttrs(Attrs(ctx)...)
if record.Level < h.levelFor(h.defaultLevel, record.Attrs) {
if ctx.Value(isStdlog) == nil &&
record.Level < h.levelFor(h.defaultLevel, record.Attrs) {
return nil
}

Expand Down Expand Up @@ -194,3 +200,29 @@ func (h *logHandler) levelFor(level slog.Level, fn func(func(slog.Attr) bool)) s
})
return level
}

var isStdlog _contextKey

type StdlogWriter struct {
Handler slog.Handler
}

func (w *StdlogWriter) Write(buf []byte) (int, error) {
var pc uintptr
if stdlog.Flags()&(stdlog.Lshortfile|stdlog.Llongfile) != 0 {
// skip [runtime.Callers, w.Write, Logger.Output, log.Print]
var pcs [1]uintptr
runtime.Callers(4, pcs[:])
pc = pcs[0]
}

// Remove final newline.
origLen := len(buf) // Report that the entire buf was written.
if len(buf) > 0 && buf[len(buf)-1] == '\n' {
buf = buf[:len(buf)-1]
}

ctx := context.WithValue(context.Background(), isStdlog, true)
r := slog.NewRecord(time.Now(), slog.LevelInfo, string(buf), pc)
return origLen, w.Handler.Handle(ctx, r)
}

0 comments on commit eb762d0

Please sign in to comment.