forked from tilt-dev/tilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
73 lines (62 loc) · 2.2 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
62
63
64
65
66
67
68
69
70
71
72
73
package store
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/logger"
"github.com/tilt-dev/tilt/pkg/model"
"github.com/tilt-dev/tilt/pkg/model/logstore"
)
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 Dispatcher, obj metav1.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 Dispatcher, obj metav1.Object) (context.Context, error) {
// It's ok if the manifest or span id don't exist, they will just
// get dumped in the global log.
mn := obj.GetAnnotations()[v1alpha1.AnnotationManifest]
spanID := obj.GetAnnotations()[v1alpha1.AnnotationSpanID]
typ, err := meta.TypeAccessor(obj)
if err != nil {
return nil, fmt.Errorf("object missing type data: %T", obj)
}
if spanID == "" {
spanID = fmt.Sprintf("%s-%s", typ.GetKind(), obj.GetName())
}
return WithManifestLogHandler(ctx, st, model.ManifestName(mn), model.LogSpanID(spanID)), nil
}
func WithManifestLogHandler(ctx context.Context, st Dispatcher, mn model.ManifestName, spanID logstore.SpanID) context.Context {
w := manifestLogWriter{
store: st,
manifestName: mn,
spanID: spanID,
}
return logger.CtxWithLogHandler(ctx, w)
}
type manifestLogWriter struct {
store Dispatcher
manifestName model.ManifestName
spanID model.LogSpanID
}
func (w manifestLogWriter) Write(level logger.Level, fields logger.Fields, p []byte) error {
w.store.Dispatch(NewLogAction(w.manifestName, w.spanID, level, fields, p))
return nil
}
type Dispatcher interface {
Dispatch(action Action)
}