Debian Codegraph @

cmd/nebula-service/logs_windows.go

raw
1 package main
2
3 import (
4 "context"
5 "log/slog"
6 "strings"
7 "sync"
8
9 "github.com/slackhq/nebula/logging"
10 )
11
12 // newPlatformLogger returns a *slog.Logger that routes every log record
13 // through the Windows service logger so records end up in the Windows
14 // Event Log. All the heavy lifting (level management, format swap,
15 // timestamp toggle, WithAttrs/WithGroup) comes from logging.NewHandler;
16 // this file only contributes:
17 //
18 // - an io.Writer that forwards each formatted line to the service
19 // logger at the current record's Event Log severity, and
20 // - a thin severityTag that embeds *logging.Handler and overrides
21 // only Handle / WithAttrs / WithGroup, so Event Viewer's severity
22 // column and severity-based filters keep working the way they did
23 // before the slog migration.
24 //
25 // Format (text vs json) is carried by the embedded *logging.Handler, so
26 // logging.format: json in config still produces JSON lines in Event
27 // Viewer, same as the pre-slog logrus setup.
28 func newPlatformLogger() *slog.Logger {
29 w := &eventLogWriter{}
30 return slog.New(&severityTag{Handler: logging.NewHandler(w), w: w})
31 }
32
33 // eventLogWriter forwards slog-formatted lines to the Windows service
34 // logger at the severity most recently stashed by severityTag.Handle.
35 // The mutex serializes the stash + inner.Handle + Write cycle per record
36 // across all concurrent goroutines; slog's builtin text/json handlers
37 // each hold their own mutex around Write, but that only protects the
38 // Write call itself, not our stash-then-handle sequence.
39 type eventLogWriter struct {
40 mu sync.Mutex
41 level slog.Level
42 }
43
44 func (w *eventLogWriter) Write(p []byte) (int, error) {
45 line := strings.TrimRight(string(p), "\n")
46 switch {
47 case w.level >= slog.LevelError:
48 return len(p), logger.Error(line)
49 case w.level >= slog.LevelWarn:
50 return len(p), logger.Warning(line)
51 default:
52 return len(p), logger.Info(line)
53 }
54 }
55
56 // severityTag embeds *logging.Handler to pick up everything it does for
57 // free (Enabled, SetLevel, GetLevel, SetFormat, GetFormat,
58 // SetDisableTimestamp) and overrides only Handle / WithAttrs / WithGroup
59 // so each record's slog.Level is stashed on the writer before formatting
60 // and so derived handlers stay wrapped as severityTag rather than
61 // downgrading to bare *logging.Handler.
62 type severityTag struct {
63 *logging.Handler
64 w *eventLogWriter
65 }
66
67 func (s *severityTag) Handle(ctx context.Context, r slog.Record) error {
68 s.w.mu.Lock()
69 defer s.w.mu.Unlock()
70 s.w.level = r.Level
71 return s.Handler.Handle(ctx, r)
72 }
73
74 func (s *severityTag) WithAttrs(attrs []slog.Attr) slog.Handler {
75 if len(attrs) == 0 {
76 return s
77 }
78 return &severityTag{Handler: s.Handler.WithAttrs(attrs).(*logging.Handler), w: s.w}
79 }
80
81 func (s *severityTag) WithGroup(name string) slog.Handler {
82 if name == "" {
83 return s
84 }
85 return &severityTag{Handler: s.Handler.WithGroup(name).(*logging.Handler), w: s.w}
86 }