-
Notifications
You must be signed in to change notification settings - Fork 8
/
tf.go
72 lines (60 loc) · 1.75 KB
/
tf.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
package log
import (
"context"
"log/slog"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
type TFHandler struct {
attrs []slog.Attr
groups []string
}
const subsystem = "imagetest"
// Enabled implements slog.Handler.
func (h *TFHandler) Enabled(_ context.Context, _ slog.Level) bool {
// Rely on the handler to filter this out, tflog doesn't provide a public API
// for determining the providers log level :|
return true
}
// Handle implements slog.Handler.
func (h *TFHandler) Handle(ctx context.Context, record slog.Record) error {
ctx = tflog.NewSubsystem(
ctx,
subsystem,
tflog.WithAdditionalLocationOffset(3),
tflog.WithLevelFromEnv("TF_LOG_PROVIDER", subsystem))
attrs := make(map[string]any)
for _, attr := range h.attrs {
attrs[attr.Key] = attr.Value.Any()
}
// record level attrs take precedence over handler opt attrs
record.Attrs(func(a slog.Attr) bool {
attrs[a.Key] = a.Value.Any
attrs[a.Key] = a.Value.Any()
return true
})
switch record.Level {
case slog.LevelDebug:
tflog.SubsystemDebug(ctx, subsystem, record.Message, attrs)
case slog.LevelInfo:
tflog.SubsystemInfo(ctx, subsystem, record.Message, attrs)
case slog.LevelWarn:
tflog.SubsystemWarn(ctx, subsystem, record.Message, attrs)
case slog.LevelError:
tflog.SubsystemError(ctx, subsystem, record.Message, attrs)
default:
// fallback to Info level
tflog.SubsystemInfo(ctx, subsystem, record.Message, attrs)
}
return nil
}
// WithAttrs implements slog.Handler.
func (h *TFHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &TFHandler{attrs: append(h.attrs, attrs...)}
}
// WithGroup implements slog.Handler.
func (h *TFHandler) WithGroup(name string) slog.Handler {
return &TFHandler{
attrs: h.attrs,
groups: append(h.groups, name),
}
}