-
Notifications
You must be signed in to change notification settings - Fork 303
/
logger.go
61 lines (52 loc) · 1.8 KB
/
logger.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
package store
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/logger"
"github.com/tilt-dev/tilt/pkg/model"
)
func NewLogActionLogger(ctx context.Context, dispatch func(action Action)) logger.Logger {
l := logger.Get(ctx)
return logger.NewFuncLogger(l.SupportsColor(), l.Level(), func(level logger.Level, fields logger.Fields, b []byte) error {
dispatch(NewGlobalLogAction(level, b))
return nil
})
}
// Read labels and annotations of the given API object to determine where to log,
// panicking if there's no info available.
func MustObjectLogHandler(ctx context.Context, st RStore, obj runtime.Object) context.Context {
ctx, err := WithObjectLogHandler(ctx, st, obj)
if err != nil {
panic(err)
}
return ctx
}
// Read labels and annotations of the given API object to determine where to log.
func WithObjectLogHandler(ctx context.Context, st RStore, obj runtime.Object) (context.Context, error) {
meta, err := meta.Accessor(obj)
if err != nil {
return nil, fmt.Errorf("object missing metadata: %T", obj)
}
// It's ok if the manifest or span id don't exist, they will just
// get dumped in the global log.
mn := meta.GetAnnotations()[v1alpha1.AnnotationManifest]
spanID := meta.GetAnnotations()[v1alpha1.AnnotationSpanID]
w := apiLogWriter{
store: st,
manifestName: model.ManifestName(mn),
spanID: model.LogSpanID(spanID),
}
return logger.CtxWithLogHandler(ctx, w), nil
}
type apiLogWriter struct {
store RStore
manifestName model.ManifestName
spanID model.LogSpanID
}
func (w apiLogWriter) Write(level logger.Level, fields logger.Fields, p []byte) error {
w.store.Dispatch(NewLogAction(w.manifestName, w.spanID, level, fields, p))
return nil
}