mirror of
https://github.com/syncthing/syncthing.git
synced 2025-12-23 22:18:14 -05:00
feat: switch logging framework (#10220)
This updates our logging framework from legacy freetext strings using the `log` package to structured log entries using `log/slog`. I have updated all INFO or higher level entries, but not yet DEBUG (😓)... So, at a high level: There is a slight change in log levels, effectively adding a new warning level: - DEBUG is still debug (ideally not for users but developers, though this is something we need to work on) - INFO is still info, though I've added more data here, effectively making Syncthing more verbose by default (more on this below) - WARNING is a new log level that is different from the _old_ WARNING (more below) - ERROR is what was WARNING before -- problems that must be dealt with, and also bubbled as a popup in the GUI. A new feature is that the logging level can be set per package to something other than just debug or info, and hence I feel that we can add a bit more things into INFO while moving some (in fact, most) current INFO level warnings into WARNING. For example, I think it's justified to get a log of synced files in INFO and sync failures in WARNING. These are things that have historically been tricky to debug properly, and having more information by default will be useful to many, while still making it possible get close to told level of inscrutability by setting the log level to WARNING. I'd like to get to a stage where DEBUG is never necessary to just figure out what's going on, as opposed to trying to narrow down a likely bug. Code wise: - Our logging object, generally known as `l` in each package, is now a new adapter object that provides the old API on top of the newer one. (This should go away once all old log entries are migrated.) This is only for `l.Debugln` and `l.Debugf`. - There is a new level tracker that keeps the log level for each package. - There is a nested setup of handlers, since the structure mandated by `log/slog` is slightly convoluted (imho). We do this because we need to do formatting at a "medium" level internally so we can buffer log lines in text format but with separate timestamp and log level for the API/GUI to consume. - The `debug` API call becomes a `loglevels` API call, which can set the log level to `DEBUG`, `INFO`, `WARNING` or `ERROR` per package. The GUI is updated to handle this. - Our custom `sync` package provided some debugging of mutexes quite strongly integrated into the old logging framework, only turned on when `STTRACE` was set to certain values at startup, etc. It's been a long time since this has been useful; I removed it. - The `STTRACE` env var remains and can be used the same way as before, while additionally permitting specific log levels to be specified, `STTRACE=model:WARN,scanner:DEBUG`. - There is a new command line option `--log-level=INFO` to set the default log level. - The command line options `--log-flags` and `--verbose` go away, but are currently retained as hidden & ignored options since we set them by default in some of our startup examples and Syncthing would otherwise fail to start. Sample format messages: ``` 2009-02-13 23:31:30 INF A basic info line (attr1="val with spaces" attr2=2 attr3="val\"quote" a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with grouped values (attr1=val1 foo.attr2=2 foo.bar.attr3=3 a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with grouped values via logger (foo.attr1=val1 foo.attr2=2 a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with nested grouped values via logger (bar.foo.attr1=val1 bar.foo.attr2=2 a=a log.pkg=slogutil) 2009-02-13 23:31:30 WRN A warning entry (a=a log.pkg=slogutil) 2009-02-13 23:31:30 ERR An error (a=a log.pkg=slogutil) ``` --------- Co-authored-by: Ross Smith II <ross@smithii.com>
This commit is contained in:
@@ -9,9 +9,9 @@ package olddb
|
||||
import (
|
||||
"encoding/binary"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/syncthing/syncthing/internal/db/olddb/backend"
|
||||
"github.com/syncthing/syncthing/lib/sync"
|
||||
)
|
||||
|
||||
// A smallIndex is an in memory bidirectional []byte to uint32 map. It gives
|
||||
@@ -32,7 +32,6 @@ func newSmallIndex(db backend.Backend, prefix []byte) *smallIndex {
|
||||
prefix: prefix,
|
||||
id2val: make(map[uint32]string),
|
||||
val2id: make(map[string]uint32),
|
||||
mut: sync.NewMutex(),
|
||||
}
|
||||
idx.load()
|
||||
return idx
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"iter"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -78,7 +79,7 @@ func (s *DB) getFolderDB(folder string, create bool) (*folderDB, error) {
|
||||
}
|
||||
}
|
||||
|
||||
l.Debugf("Folder %s in database %s", folder, dbName)
|
||||
slog.Debug("Folder database opened", "folder", folder, "db", dbName)
|
||||
path := dbName
|
||||
if !filepath.IsAbs(path) {
|
||||
path = filepath.Join(s.pathBase, dbName)
|
||||
|
||||
@@ -7,12 +7,14 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/internal/db"
|
||||
"github.com/syncthing/syncthing/internal/slogutil"
|
||||
)
|
||||
|
||||
const maxDBConns = 16
|
||||
@@ -128,7 +130,7 @@ func OpenTemp() (*DB, error) {
|
||||
return nil, wrap(err)
|
||||
}
|
||||
path := filepath.Join(dir, "db")
|
||||
l.Debugln("Test DB in", path)
|
||||
slog.Debug("Test DB", slogutil.FilePath(path))
|
||||
return Open(path)
|
||||
}
|
||||
|
||||
|
||||
@@ -9,10 +9,12 @@ package sqlite
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/syncthing/syncthing/internal/db"
|
||||
"github.com/syncthing/syncthing/internal/slogutil"
|
||||
"github.com/thejerf/suture/v4"
|
||||
)
|
||||
|
||||
@@ -56,7 +58,7 @@ func (s *Service) Serve(ctx context.Context) error {
|
||||
if wait < 0 {
|
||||
wait = time.Minute
|
||||
}
|
||||
l.Debugln("Next periodic run in", wait)
|
||||
slog.DebugContext(ctx, "Next periodic run due", "after", wait)
|
||||
|
||||
timer := time.NewTimer(wait)
|
||||
for {
|
||||
@@ -71,17 +73,17 @@ func (s *Service) Serve(ctx context.Context) error {
|
||||
}
|
||||
|
||||
timer.Reset(s.maintenanceInterval)
|
||||
l.Debugln("Next periodic run in", s.maintenanceInterval)
|
||||
slog.DebugContext(ctx, "Next periodic run due", "after", s.maintenanceInterval)
|
||||
_ = s.internalMeta.PutTime(lastMaintKey, time.Now())
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) periodic(ctx context.Context) error {
|
||||
t0 := time.Now()
|
||||
l.Debugln("Periodic start")
|
||||
slog.DebugContext(ctx, "Periodic start")
|
||||
|
||||
t1 := time.Now()
|
||||
defer func() { l.Debugln("Periodic done in", time.Since(t1), "+", t1.Sub(t0)) }()
|
||||
defer func() { slog.DebugContext(ctx, "Periodic done in", "t1", time.Since(t1), "t0t1", t1.Sub(t0)) }()
|
||||
|
||||
s.sdb.updateLock.Lock()
|
||||
err := tidy(ctx, s.sdb.sql)
|
||||
@@ -94,7 +96,7 @@ func (s *Service) periodic(ctx context.Context) error {
|
||||
fdb.updateLock.Lock()
|
||||
defer fdb.updateLock.Unlock()
|
||||
|
||||
if err := garbageCollectOldDeletedLocked(fdb); err != nil {
|
||||
if err := garbageCollectOldDeletedLocked(ctx, fdb); err != nil {
|
||||
return wrap(err)
|
||||
}
|
||||
if err := garbageCollectBlocklistsAndBlocksLocked(ctx, fdb); err != nil {
|
||||
@@ -118,15 +120,16 @@ func tidy(ctx context.Context, db *sqlx.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func garbageCollectOldDeletedLocked(fdb *folderDB) error {
|
||||
func garbageCollectOldDeletedLocked(ctx context.Context, fdb *folderDB) error {
|
||||
l := slog.With("fdb", fdb.baseDB)
|
||||
if fdb.deleteRetention <= 0 {
|
||||
l.Debugln(fdb.baseName, "delete retention is infinite, skipping cleanup")
|
||||
slog.DebugContext(ctx, "Delete retention is infinite, skipping cleanup")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove deleted files that are marked as not needed (we have processed
|
||||
// them) and they were deleted more than MaxDeletedFileAge ago.
|
||||
l.Debugln(fdb.baseName, "forgetting deleted files older than", fdb.deleteRetention)
|
||||
l.DebugContext(ctx, "Forgetting deleted files", "retention", fdb.deleteRetention)
|
||||
res, err := fdb.stmt(`
|
||||
DELETE FROM files
|
||||
WHERE deleted AND modified < ? AND local_flags & {{.FlagLocalNeeded}} == 0
|
||||
@@ -135,7 +138,7 @@ func garbageCollectOldDeletedLocked(fdb *folderDB) error {
|
||||
return wrap(err)
|
||||
}
|
||||
if aff, err := res.RowsAffected(); err == nil {
|
||||
l.Debugln(fdb.baseName, "removed old deleted file records:", aff)
|
||||
l.DebugContext(ctx, "Removed old deleted file records", "affected", aff)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -176,9 +179,14 @@ func garbageCollectBlocklistsAndBlocksLocked(ctx context.Context, fdb *folderDB)
|
||||
SELECT 1 FROM files WHERE files.blocklist_hash = blocklists.blocklist_hash
|
||||
)`); err != nil {
|
||||
return wrap(err, "delete blocklists")
|
||||
} else if shouldDebug() {
|
||||
rows, err := res.RowsAffected()
|
||||
l.Debugln(fdb.baseName, "blocklist GC:", rows, err)
|
||||
} else {
|
||||
slog.DebugContext(ctx, "Blocklist GC", "fdb", fdb.baseName, "result", slogutil.Expensive(func() any {
|
||||
rows, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return slogutil.Error(err)
|
||||
}
|
||||
return slog.Int64("rows", rows)
|
||||
}))
|
||||
}
|
||||
|
||||
if res, err := tx.ExecContext(ctx, `
|
||||
@@ -187,9 +195,14 @@ func garbageCollectBlocklistsAndBlocksLocked(ctx context.Context, fdb *folderDB)
|
||||
SELECT 1 FROM blocklists WHERE blocklists.blocklist_hash = blocks.blocklist_hash
|
||||
)`); err != nil {
|
||||
return wrap(err, "delete blocks")
|
||||
} else if shouldDebug() {
|
||||
rows, err := res.RowsAffected()
|
||||
l.Debugln(fdb.baseName, "blocks GC:", rows, err)
|
||||
} else {
|
||||
slog.DebugContext(ctx, "Blocks GC", "fdb", fdb.baseName, "result", slogutil.Expensive(func() any {
|
||||
rows, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return slogutil.Error(err)
|
||||
}
|
||||
return slog.Int64("rows", rows)
|
||||
}))
|
||||
}
|
||||
|
||||
return wrap(tx.Commit())
|
||||
|
||||
@@ -6,10 +6,6 @@
|
||||
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"github.com/syncthing/syncthing/lib/logger"
|
||||
)
|
||||
import "github.com/syncthing/syncthing/internal/slogutil"
|
||||
|
||||
var l = logger.DefaultLogger.NewFacility("sqlite", "SQLite database")
|
||||
|
||||
func shouldDebug() bool { return l.ShouldDebug("sqlite") }
|
||||
func init() { slogutil.RegisterPackage("SQLite database") }
|
||||
|
||||
@@ -10,11 +10,13 @@ import (
|
||||
"cmp"
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/syncthing/syncthing/internal/gen/dbproto"
|
||||
"github.com/syncthing/syncthing/internal/itererr"
|
||||
"github.com/syncthing/syncthing/internal/slogutil"
|
||||
"github.com/syncthing/syncthing/lib/osutil"
|
||||
"github.com/syncthing/syncthing/lib/protocol"
|
||||
"github.com/syncthing/syncthing/lib/sliceutil"
|
||||
@@ -486,12 +488,12 @@ func (s *folderDB) periodicCheckpointLocked(fs []protocol.FileInfo) {
|
||||
if s.updatePoints > updatePointsThreshold {
|
||||
conn, err := s.sql.Conn(context.Background())
|
||||
if err != nil {
|
||||
l.Debugln(s.baseName, "conn:", err)
|
||||
slog.Debug("Connection error", slog.String("db", s.baseName), slogutil.Error(err))
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
if _, err := conn.ExecContext(context.Background(), `PRAGMA journal_size_limit = 8388608`); err != nil {
|
||||
l.Debugln(s.baseName, "PRAGMA journal_size_limit:", err)
|
||||
slog.Debug("PRAGMA journal_size_limit error", slog.String("db", s.baseName), slogutil.Error(err))
|
||||
}
|
||||
|
||||
// Every 50th checkpoint becomes a truncate, in an effort to bring
|
||||
@@ -505,11 +507,11 @@ func (s *folderDB) periodicCheckpointLocked(fs []protocol.FileInfo) {
|
||||
|
||||
var res, modified, moved int
|
||||
if row.Err() != nil {
|
||||
l.Debugln(s.baseName, cmd+":", err)
|
||||
slog.Debug("Command error", slog.String("db", s.baseName), slog.String("cmd", cmd), slogutil.Error(err))
|
||||
} else if err := row.Scan(&res, &modified, &moved); err != nil {
|
||||
l.Debugln(s.baseName, cmd+" (scan):", err)
|
||||
slog.Debug("Command scan error", slog.String("db", s.baseName), slog.String("cmd", cmd), slogutil.Error(err))
|
||||
} else {
|
||||
l.Debugln(s.baseName, cmd, s.checkpointsCount, "at", s.updatePoints, "returned", res, modified, moved)
|
||||
slog.Debug("Checkpoint result", "db", s.baseName, "checkpointscount", s.checkpointsCount, "updatepoints", s.updatePoints, "res", res, "modified", modified, "moved", moved)
|
||||
}
|
||||
|
||||
// Reset the truncate counter when a truncate succeeded. If it
|
||||
|
||||
25
internal/slogutil/expensive.go
Normal file
25
internal/slogutil/expensive.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// Expensive wraps a log value that is expensive to compute and should only
|
||||
// be called if the log line is actually emitted.
|
||||
func Expensive(fn func() any) expensive {
|
||||
return expensive{fn}
|
||||
}
|
||||
|
||||
type expensive struct {
|
||||
fn func() any
|
||||
}
|
||||
|
||||
func (e expensive) LogValue() slog.Value {
|
||||
return slog.AnyValue(e.fn())
|
||||
}
|
||||
187
internal/slogutil/formatting.go
Normal file
187
internal/slogutil/formatting.go
Normal file
@@ -0,0 +1,187 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"path"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type formattingHandler struct {
|
||||
attrs []slog.Attr
|
||||
groups []string
|
||||
out io.Writer
|
||||
recs []*lineRecorder
|
||||
timeOverride time.Time
|
||||
}
|
||||
|
||||
var _ slog.Handler = (*formattingHandler)(nil)
|
||||
|
||||
func (h *formattingHandler) Enabled(context.Context, slog.Level) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *formattingHandler) Handle(_ context.Context, rec slog.Record) error {
|
||||
fr := runtime.CallersFrames([]uintptr{rec.PC})
|
||||
var logAttrs []any
|
||||
if fram, _ := fr.Next(); fram.Function != "" {
|
||||
pkgName, typeName := funcNameToPkg(fram.Function)
|
||||
lvl := globalLevels.Get(pkgName)
|
||||
if lvl > rec.Level {
|
||||
// Logging not enabled at the record's level
|
||||
return nil
|
||||
}
|
||||
logAttrs = append(logAttrs, slog.String("pkg", pkgName))
|
||||
if lvl <= slog.LevelDebug {
|
||||
// We are debugging, add additional source line data
|
||||
if typeName != "" {
|
||||
logAttrs = append(logAttrs, slog.String("type", typeName))
|
||||
}
|
||||
logAttrs = append(logAttrs, slog.Group("src", slog.String("file", path.Base(fram.File)), slog.Int("line", fram.Line)))
|
||||
}
|
||||
}
|
||||
|
||||
var prefix string
|
||||
if len(h.groups) > 0 {
|
||||
prefix = strings.Join(h.groups, ".") + "."
|
||||
}
|
||||
|
||||
// Build the message string.
|
||||
var sb strings.Builder
|
||||
sb.WriteString(rec.Message)
|
||||
|
||||
// Collect all the attributes, adding the handler prefix.
|
||||
attrs := make([]slog.Attr, 0, rec.NumAttrs()+len(h.attrs)+1)
|
||||
rec.Attrs(func(attr slog.Attr) bool {
|
||||
attr.Key = prefix + attr.Key
|
||||
attrs = append(attrs, attr)
|
||||
return true
|
||||
})
|
||||
attrs = append(attrs, h.attrs...)
|
||||
attrs = append(attrs, slog.Group("log", logAttrs...))
|
||||
|
||||
// Expand and format attributes
|
||||
var attrCount int
|
||||
for _, attr := range attrs {
|
||||
for _, attr := range expandAttrs("", attr) {
|
||||
appendAttr(&sb, "", attr, &attrCount)
|
||||
}
|
||||
}
|
||||
if attrCount > 0 {
|
||||
sb.WriteRune(')')
|
||||
}
|
||||
|
||||
line := Line{
|
||||
When: cmp.Or(h.timeOverride, rec.Time),
|
||||
Message: sb.String(),
|
||||
Level: rec.Level,
|
||||
}
|
||||
|
||||
// If there is a recorder, record the line.
|
||||
for _, rec := range h.recs {
|
||||
rec.record(line)
|
||||
}
|
||||
|
||||
// If there's an output, print the line.
|
||||
if h.out != nil {
|
||||
_, _ = line.WriteTo(h.out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func expandAttrs(prefix string, a slog.Attr) []slog.Attr {
|
||||
if prefix != "" {
|
||||
a.Key = prefix + "." + a.Key
|
||||
}
|
||||
val := a.Value.Resolve()
|
||||
if val.Kind() != slog.KindGroup {
|
||||
return []slog.Attr{a}
|
||||
}
|
||||
var attrs []slog.Attr
|
||||
for _, attr := range val.Group() {
|
||||
attrs = append(attrs, expandAttrs(a.Key, attr)...)
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
func appendAttr(sb *strings.Builder, prefix string, a slog.Attr, attrCount *int) {
|
||||
if a.Key == "" {
|
||||
return
|
||||
}
|
||||
sb.WriteRune(' ')
|
||||
if *attrCount == 0 {
|
||||
sb.WriteRune('(')
|
||||
}
|
||||
sb.WriteString(prefix)
|
||||
sb.WriteString(a.Key)
|
||||
sb.WriteRune('=')
|
||||
v := a.Value.Resolve().String()
|
||||
if strings.ContainsAny(v, ` "`) {
|
||||
v = strconv.Quote(v)
|
||||
}
|
||||
sb.WriteString(v)
|
||||
*attrCount++
|
||||
}
|
||||
|
||||
func (h *formattingHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
if len(h.groups) > 0 {
|
||||
prefix := strings.Join(h.groups, ".") + "."
|
||||
for i := range attrs {
|
||||
attrs[i].Key = prefix + attrs[i].Key
|
||||
}
|
||||
}
|
||||
return &formattingHandler{
|
||||
attrs: append(h.attrs, attrs...),
|
||||
groups: h.groups,
|
||||
recs: h.recs,
|
||||
out: h.out,
|
||||
timeOverride: h.timeOverride,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *formattingHandler) WithGroup(name string) slog.Handler {
|
||||
if name == "" {
|
||||
return h
|
||||
}
|
||||
return &formattingHandler{
|
||||
attrs: h.attrs,
|
||||
groups: append([]string{name}, h.groups...),
|
||||
recs: h.recs,
|
||||
out: h.out,
|
||||
timeOverride: h.timeOverride,
|
||||
}
|
||||
}
|
||||
|
||||
func funcNameToPkg(fn string) (string, string) {
|
||||
fn = strings.ToLower(fn)
|
||||
fn = strings.TrimPrefix(fn, "github.com/syncthing/syncthing/lib/")
|
||||
fn = strings.TrimPrefix(fn, "github.com/syncthing/syncthing/internal/")
|
||||
|
||||
pkgTypFn := strings.Split(fn, ".") // [package, type, method] or [package, function]
|
||||
if len(pkgTypFn) <= 2 {
|
||||
return pkgTypFn[0], ""
|
||||
}
|
||||
|
||||
pkg := pkgTypFn[0]
|
||||
// Remove parenthesis and asterisk from the type name
|
||||
typ := strings.TrimLeft(strings.TrimRight(pkgTypFn[1], ")"), "(*")
|
||||
// Skip certain type names that add no value
|
||||
typ = strings.TrimSuffix(typ, "service")
|
||||
switch typ {
|
||||
case pkg, "", "serveparams":
|
||||
return pkg, ""
|
||||
default:
|
||||
return pkg, typ
|
||||
}
|
||||
}
|
||||
51
internal/slogutil/formatting_test.go
Normal file
51
internal/slogutil/formatting_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFormattingHandler(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
h := &formattingHandler{
|
||||
out: buf,
|
||||
timeOverride: time.Unix(1234567890, 0).In(time.UTC),
|
||||
}
|
||||
|
||||
l := slog.New(h).With("a", "a")
|
||||
l.Info("A basic info line", "attr1", "val with spaces", "attr2", 2, "attr3", `val"quote`)
|
||||
l.Info("An info line with grouped values", "attr1", "val1", slog.Group("foo", "attr2", 2, slog.Group("bar", "attr3", "3")))
|
||||
|
||||
l2 := l.WithGroup("foo")
|
||||
l2.Info("An info line with grouped values via logger", "attr1", "val1", "attr2", 2)
|
||||
|
||||
l3 := l2.WithGroup("bar")
|
||||
l3.Info("An info line with nested grouped values via logger", "attr1", "val1", "attr2", 2)
|
||||
|
||||
l3.Debug("A debug entry")
|
||||
l3.Warn("A warning entry")
|
||||
l3.Error("An error")
|
||||
|
||||
exp := `
|
||||
2009-02-13 23:31:30 INF A basic info line (attr1="val with spaces" attr2=2 attr3="val\"quote" a=a log.pkg=slogutil)
|
||||
2009-02-13 23:31:30 INF An info line with grouped values (attr1=val1 foo.attr2=2 foo.bar.attr3=3 a=a log.pkg=slogutil)
|
||||
2009-02-13 23:31:30 INF An info line with grouped values via logger (foo.attr1=val1 foo.attr2=2 a=a log.pkg=slogutil)
|
||||
2009-02-13 23:31:30 INF An info line with nested grouped values via logger (bar.foo.attr1=val1 bar.foo.attr2=2 a=a log.pkg=slogutil)
|
||||
2009-02-13 23:31:30 WRN A warning entry (a=a log.pkg=slogutil)
|
||||
2009-02-13 23:31:30 ERR An error (a=a log.pkg=slogutil)`
|
||||
|
||||
if strings.TrimSpace(buf.String()) != strings.TrimSpace(exp) {
|
||||
t.Log(buf.String())
|
||||
t.Log(exp)
|
||||
t.Error("mismatch")
|
||||
}
|
||||
}
|
||||
104
internal/slogutil/leveler.go
Normal file
104
internal/slogutil/leveler.go
Normal file
@@ -0,0 +1,104 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"maps"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A levelTracker keeps track of log level per package. This enables the
|
||||
// traditional STTRACE variable to set certain packages to debug level, but
|
||||
// also allows setting packages to other levels such as WARN to silence
|
||||
// INFO-level messages.
|
||||
//
|
||||
// The STTRACE environment variable is one way of controlling this, where
|
||||
// mentioning a package makes it DEBUG level:
|
||||
// STTRACE="model,protocol" # model and protocol are at DEBUG level
|
||||
// however you can also give specific levels after a colon:
|
||||
// STTRACE="model:WARNING,protocol:DEBUG"
|
||||
|
||||
func PackageDescrs() map[string]string {
|
||||
return globalLevels.Descrs()
|
||||
}
|
||||
|
||||
func PackageLevels() map[string]slog.Level {
|
||||
return globalLevels.Levels()
|
||||
}
|
||||
|
||||
func SetPackageLevel(pkg string, level slog.Level) {
|
||||
globalLevels.Set(pkg, level)
|
||||
}
|
||||
|
||||
func SetDefaultLevel(level slog.Level) {
|
||||
globalLevels.SetDefault(level)
|
||||
}
|
||||
|
||||
type levelTracker struct {
|
||||
mut sync.RWMutex
|
||||
defLevel slog.Level
|
||||
descrs map[string]string // package name to description
|
||||
levels map[string]slog.Level // package name to level
|
||||
}
|
||||
|
||||
func (t *levelTracker) Get(pkg string) slog.Level {
|
||||
t.mut.RLock()
|
||||
defer t.mut.RUnlock()
|
||||
if level, ok := t.levels[pkg]; ok {
|
||||
return level
|
||||
}
|
||||
return t.defLevel
|
||||
}
|
||||
|
||||
func (t *levelTracker) Set(pkg string, level slog.Level) {
|
||||
t.mut.Lock()
|
||||
changed := t.levels[pkg] != level
|
||||
t.levels[pkg] = level
|
||||
t.mut.Unlock()
|
||||
if changed {
|
||||
slog.Info("Changed package log level", "package", pkg, "level", level)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *levelTracker) SetDefault(level slog.Level) {
|
||||
t.mut.Lock()
|
||||
changed := t.defLevel != level
|
||||
t.defLevel = level
|
||||
t.mut.Unlock()
|
||||
if changed {
|
||||
slog.Info("Changed default log level", "level", level)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *levelTracker) SetDescr(pkg, descr string) {
|
||||
t.mut.Lock()
|
||||
t.descrs[pkg] = descr
|
||||
t.mut.Unlock()
|
||||
}
|
||||
|
||||
func (t *levelTracker) Descrs() map[string]string {
|
||||
t.mut.RLock()
|
||||
defer t.mut.RUnlock()
|
||||
m := make(map[string]string, len(t.descrs))
|
||||
maps.Copy(m, t.descrs)
|
||||
return m
|
||||
}
|
||||
|
||||
func (t *levelTracker) Levels() map[string]slog.Level {
|
||||
t.mut.RLock()
|
||||
defer t.mut.RUnlock()
|
||||
m := make(map[string]slog.Level, len(t.descrs))
|
||||
for pkg := range t.descrs {
|
||||
if level, ok := t.levels[pkg]; ok {
|
||||
m[pkg] = level
|
||||
} else {
|
||||
m[pkg] = t.defLevel
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
61
internal/slogutil/line.go
Normal file
61
internal/slogutil/line.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A Line is our internal representation of a formatted log line. This is
|
||||
// what we present in the API and what we buffer internally.
|
||||
type Line struct {
|
||||
When time.Time `json:"when"`
|
||||
Message string `json:"message"`
|
||||
Level slog.Level `json:"level"`
|
||||
}
|
||||
|
||||
func (l *Line) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := fmt.Fprintf(w, "%s %s %s\n", l.timeStr(), l.levelStr(), l.Message)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func (l *Line) timeStr() string {
|
||||
return l.When.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func (l *Line) levelStr() string {
|
||||
str := func(base string, val slog.Level) string {
|
||||
if val == 0 {
|
||||
return base
|
||||
}
|
||||
return fmt.Sprintf("%s%+d", base, val)
|
||||
}
|
||||
|
||||
switch {
|
||||
case l.Level < slog.LevelInfo:
|
||||
return str("DBG", l.Level-slog.LevelDebug)
|
||||
case l.Level < slog.LevelWarn:
|
||||
return str("INF", l.Level-slog.LevelInfo)
|
||||
case l.Level < slog.LevelError:
|
||||
return str("WRN", l.Level-slog.LevelWarn)
|
||||
default:
|
||||
return str("ERR", l.Level-slog.LevelError)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Line) MarshalJSON() ([]byte, error) {
|
||||
// Custom marshal to get short level strings instead of default JSON serialisation
|
||||
return json.Marshal(map[string]any{
|
||||
"when": l.When,
|
||||
"message": l.Message,
|
||||
"level": l.levelStr(),
|
||||
})
|
||||
}
|
||||
59
internal/slogutil/recorder.go
Normal file
59
internal/slogutil/recorder.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxLogLines = 1000
|
||||
|
||||
type Recorder interface {
|
||||
Since(t time.Time) []Line
|
||||
Clear()
|
||||
}
|
||||
|
||||
func NewRecorder(level slog.Level) Recorder {
|
||||
return &lineRecorder{level: level}
|
||||
}
|
||||
|
||||
type lineRecorder struct {
|
||||
level slog.Level
|
||||
mut sync.Mutex
|
||||
lines []Line
|
||||
}
|
||||
|
||||
func (r *lineRecorder) record(line Line) {
|
||||
if line.Level < r.level {
|
||||
return
|
||||
}
|
||||
r.mut.Lock()
|
||||
r.lines = append(r.lines, line)
|
||||
if len(r.lines) > maxLogLines {
|
||||
r.lines = r.lines[len(r.lines)-maxLogLines:]
|
||||
}
|
||||
r.mut.Unlock()
|
||||
}
|
||||
|
||||
func (r *lineRecorder) Clear() {
|
||||
r.mut.Lock()
|
||||
r.lines = nil
|
||||
r.mut.Unlock()
|
||||
}
|
||||
|
||||
func (r *lineRecorder) Since(t time.Time) []Line {
|
||||
r.mut.Lock()
|
||||
defer r.mut.Unlock()
|
||||
for i := range r.lines {
|
||||
if r.lines[i].When.After(t) {
|
||||
return r.lines[i:]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
71
internal/slogutil/slogadapter.go
Normal file
71
internal/slogutil/slogadapter.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Log levels:
|
||||
// - DEBUG: programmers only (not user troubleshooting)
|
||||
// - INFO: most stuff, files syncing properly
|
||||
// - WARN: errors that can be ignored or will be retried (e.g., sync failures)
|
||||
// - ERROR: errors that need handling, shown in the GUI
|
||||
|
||||
func RegisterPackage(descr string) {
|
||||
registerPackage(descr, 2)
|
||||
}
|
||||
|
||||
func NewAdapter(descr string) *adapter {
|
||||
registerPackage(descr, 2)
|
||||
return &adapter{slogDef}
|
||||
}
|
||||
|
||||
func registerPackage(descr string, frames int) {
|
||||
var pcs [1]uintptr
|
||||
runtime.Callers(1+frames, pcs[:])
|
||||
pc := pcs[0]
|
||||
fr := runtime.CallersFrames([]uintptr{pc})
|
||||
if fram, _ := fr.Next(); fram.Function != "" {
|
||||
pkgName, _ := funcNameToPkg(fram.Function)
|
||||
globalLevels.SetDescr(pkgName, descr)
|
||||
}
|
||||
}
|
||||
|
||||
type adapter struct {
|
||||
l *slog.Logger
|
||||
}
|
||||
|
||||
func (a adapter) Debugln(vals ...interface{}) {
|
||||
a.log(strings.TrimSpace(fmt.Sprintln(vals...)), slog.LevelDebug)
|
||||
}
|
||||
|
||||
func (a adapter) Debugf(format string, vals ...interface{}) {
|
||||
a.log(fmt.Sprintf(format, vals...), slog.LevelDebug)
|
||||
}
|
||||
|
||||
func (a adapter) log(msg string, level slog.Level) {
|
||||
h := a.l.Handler()
|
||||
if !h.Enabled(context.Background(), level) {
|
||||
return
|
||||
}
|
||||
var pcs [1]uintptr
|
||||
// skip [runtime.Callers, this function, this function's caller]
|
||||
runtime.Callers(3, pcs[:])
|
||||
pc := pcs[0]
|
||||
r := slog.NewRecord(time.Now(), level, msg, pc)
|
||||
_ = h.Handle(context.Background(), r)
|
||||
}
|
||||
|
||||
func (a adapter) ShouldDebug(facility string) bool {
|
||||
return globalLevels.Get(facility) <= slog.LevelDebug
|
||||
}
|
||||
47
internal/slogutil/sloginit.go
Normal file
47
internal/slogutil/sloginit.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
GlobalRecorder = &lineRecorder{level: -1000}
|
||||
ErrorRecorder = &lineRecorder{level: slog.LevelError}
|
||||
globalLevels = &levelTracker{
|
||||
levels: make(map[string]slog.Level),
|
||||
descrs: make(map[string]string),
|
||||
}
|
||||
slogDef = slog.New(&formattingHandler{
|
||||
recs: []*lineRecorder{GlobalRecorder, ErrorRecorder},
|
||||
out: os.Stdout,
|
||||
})
|
||||
)
|
||||
|
||||
func init() {
|
||||
slog.SetDefault(slogDef)
|
||||
|
||||
// Handle legacy STTRACE var
|
||||
pkgs := strings.Split(os.Getenv("STTRACE"), ",")
|
||||
for _, pkg := range pkgs {
|
||||
pkg = strings.TrimSpace(pkg)
|
||||
if pkg == "" {
|
||||
continue
|
||||
}
|
||||
level := slog.LevelDebug
|
||||
if cutPkg, levelStr, ok := strings.Cut(pkg, ":"); ok {
|
||||
pkg = cutPkg
|
||||
if err := level.UnmarshalText([]byte(levelStr)); err != nil {
|
||||
slog.Warn("Bad log level requested in STTRACE", slog.String("pkg", pkg), slog.String("level", levelStr), Error(err))
|
||||
}
|
||||
}
|
||||
globalLevels.Set(pkg, level)
|
||||
}
|
||||
}
|
||||
40
internal/slogutil/slogvalues.go
Normal file
40
internal/slogutil/slogvalues.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2025 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package slogutil
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"maps"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func Address(v any) slog.Attr {
|
||||
return slog.Any("address", v)
|
||||
}
|
||||
|
||||
func Error(err any) slog.Attr {
|
||||
if err == nil {
|
||||
return slog.Attr{}
|
||||
}
|
||||
return slog.Any("error", err)
|
||||
}
|
||||
|
||||
func FilePath(path string) slog.Attr {
|
||||
return slog.String("path", path)
|
||||
}
|
||||
|
||||
func URI(v any) slog.Attr {
|
||||
return slog.Any("uri", v)
|
||||
}
|
||||
|
||||
func Map[T any](m map[string]T) []any {
|
||||
var attrs []any
|
||||
for _, key := range slices.Sorted(maps.Keys(m)) {
|
||||
attrs = append(attrs, slog.Any(key, m[key]))
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
Reference in New Issue
Block a user