-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
76 lines (59 loc) · 1.8 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package servicemesh
import (
"context"
"fmt"
"io"
"log/slog"
"os"
)
// newLogger is a factory function that generates a slog instance for a service.
func (m *mesh) newLogger(service Service) *slog.Logger {
name := service.Name()
opts := &slog.HandlerOptions{
Level: m.logLevel,
}
if m.logOutput == nil {
m.logOutput = os.Stdout
}
if m.logHandler == nil {
m.logHandler = slog.NewTextHandler(m.logOutput, opts) // or NewJSONHandler for JSON output
}
logger := slog.New(m.logHandler)
if service != m {
logger = logger.With(slog.String("service", name))
}
return logger
}
// SetLogHandler sets the slog log handler interface for the service mesh and
// all existing services, as well as any services added in the future.
func (m *mesh) SetLogHandler(handler slog.Handler) {
m.logHandler = handler
m.logger = m.newLogger(m)
m.updateServiceLoggers()
}
// SetLogLevel sets the slog logger log level for the service mesh and
// all existing services, as well as any services added in the future.
func (m *mesh) SetLogLevel(level slog.Level) { // Change level type as appropriate
m.logLevel = level
m.logger.Log(context.Background(), slog.LevelInfo, fmt.Sprintf("setting log level to %d", level))
m.logger = m.newLogger(m)
m.updateServiceLoggers()
}
// SetLogDestination sets the slog logger destination for the service mesh and
// all existing services, as well as any services added in the future.
func (m *mesh) SetLogDestination(dst io.Writer) {
m.logOutput = dst
newLogger := m.newLogger(m)
m.logger = newLogger
m.updateServiceLoggers()
}
func (m *mesh) updateServiceLoggers() {
// set the log level for each service that has a logger
for _, service := range m.Services() {
candidate, ok := service.(HasLogger)
if !ok {
continue
}
candidate.SetLogger(m.newLogger(candidate))
}
}