slogadapter.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2025 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package slogutil
  7. import (
  8. "context"
  9. "fmt"
  10. "log/slog"
  11. "runtime"
  12. "strings"
  13. "time"
  14. )
  15. // Log levels:
  16. // - DEBUG: programmers only (not user troubleshooting)
  17. // - INFO: most stuff, files syncing properly
  18. // - WARN: errors that can be ignored or will be retried (e.g., sync failures)
  19. // - ERROR: errors that need handling, shown in the GUI
  20. func RegisterPackage(descr string) {
  21. registerPackage(descr, 2)
  22. }
  23. func NewAdapter(descr string) *adapter {
  24. registerPackage(descr, 2)
  25. return &adapter{slogDef}
  26. }
  27. func registerPackage(descr string, frames int) {
  28. var pcs [1]uintptr
  29. runtime.Callers(1+frames, pcs[:])
  30. pc := pcs[0]
  31. fr := runtime.CallersFrames([]uintptr{pc})
  32. if fram, _ := fr.Next(); fram.Function != "" {
  33. pkgName, _ := funcNameToPkg(fram.Function)
  34. globalLevels.SetDescr(pkgName, descr)
  35. }
  36. }
  37. type adapter struct {
  38. l *slog.Logger
  39. }
  40. func (a adapter) Debugln(vals ...interface{}) {
  41. a.log(strings.TrimSpace(fmt.Sprintln(vals...)), slog.LevelDebug)
  42. }
  43. func (a adapter) Debugf(format string, vals ...interface{}) {
  44. a.log(fmt.Sprintf(format, vals...), slog.LevelDebug)
  45. }
  46. func (a adapter) log(msg string, level slog.Level) {
  47. h := a.l.Handler()
  48. if !h.Enabled(context.Background(), level) {
  49. return
  50. }
  51. var pcs [1]uintptr
  52. // skip [runtime.Callers, this function, this function's caller]
  53. runtime.Callers(3, pcs[:])
  54. pc := pcs[0]
  55. r := slog.NewRecord(time.Now(), level, msg, pc)
  56. _ = h.Handle(context.Background(), r)
  57. }
  58. func (a adapter) ShouldDebug(facility string) bool {
  59. return globalLevels.Get(facility) <= slog.LevelDebug
  60. }