Debian Codegraph @

event/adapter/zap/zap.go

raw symbols SCIP
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build !disable_events
6
7 // zap provides an implementation of zapcore.Core for events.
8 // To use globally:
9 //
10 // zap.ReplaceGlobals(zap.New(NewCore(exporter)))
11 //
12 // If you call elogging.SetExporter, then you can pass nil
13 // for the exporter above and it will use the global one.
14 package zap
15
16 import (
17 "context"
18 "fmt"
19 "math"
20 "reflect"
21 "time"
22
23 "go.uber.org/zap"
24 "go.uber.org/zap/zapcore"
25 "golang.org/x/exp/event"
26 "golang.org/x/exp/event/severity"
27 )
28
29 type core struct {
30 ev *event.Event // cloned but never delivered
31 labels []event.Label
32 }
33
34 var _ zapcore.Core = (*core)(nil)
35
36 func NewCore(ctx context.Context) zapcore.Core {
37 return &core{ev: event.New(ctx, event.LogKind)}
38 }
39
40 func (c *core) Enabled(level zapcore.Level) bool {
41 return true
42 }
43
44 func (c *core) With(fields []zapcore.Field) zapcore.Core {
45 c2 := *c
46 if len(fields) > 0 {
47 c2.labels = make([]event.Label, len(c.labels), len(c.labels)+len(fields))
48 copy(c2.labels, c.labels)
49 for _, f := range fields {
50 c2.labels = append(c2.labels, newLabel(f))
51 }
52 }
53 return &c2
54 }
55
56 func (c *core) Write(e zapcore.Entry, fs []zapcore.Field) error {
57 if c.ev == nil {
58 return nil
59 }
60 ev := c.ev.Clone()
61 if ev == nil {
62 return nil
63 }
64 ev.At = e.Time
65 // TODO: Add labels more efficiently: compare cap(ev.Labels) to the total number to add,
66 // and allocate a []Label once.
67 ev.Labels = append(ev.Labels, c.labels...)
68 ev.Labels = append(ev.Labels, convertLevel(e.Level).Label())
69 ev.Labels = append(ev.Labels, event.String("name", e.LoggerName))
70 if e.Stack != "" {
71 ev.Labels = append(ev.Labels, event.String("stack", e.Stack))
72 }
73 if e.Caller.Defined {
74 ev.Labels = append(ev.Labels, event.String("caller", e.Caller.String()))
75 }
76 for _, f := range fs {
77 ev.Labels = append(ev.Labels, newLabel(f))
78 }
79 ev.Labels = append(ev.Labels, event.String("msg", e.Message))
80 ev.Deliver()
81 return nil
82 }
83
84 func (c *core) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
85 return ce.AddCore(e, c)
86 }
87
88 func (c *core) Sync() error { return nil }
89
90 func newLabel(f zap.Field) event.Label {
91 switch f.Type {
92 case zapcore.ArrayMarshalerType, zapcore.ObjectMarshalerType, zapcore.BinaryType, zapcore.ByteStringType,
93 zapcore.Complex128Type, zapcore.Complex64Type, zapcore.TimeFullType, zapcore.ReflectType,
94 zapcore.ErrorType:
95 return event.Value(f.Key, f.Interface)
96 case zapcore.DurationType:
97 return event.Duration(f.Key, time.Duration(f.Integer))
98 case zapcore.Float64Type:
99 return event.Float64(f.Key, math.Float64frombits(uint64(f.Integer)))
100 case zapcore.Float32Type:
101 return event.Float64(f.Key, float64(math.Float32frombits(uint32(f.Integer))))
102 case zapcore.BoolType:
103 b := false
104 if f.Integer != 0 {
105 b = true
106 }
107 return event.Bool(f.Key, b)
108 case zapcore.Int64Type:
109 return event.Int64(f.Key, f.Integer)
110 case zapcore.Int32Type:
111 return event.Int64(f.Key, f.Integer)
112
113 //, zapcore.Int16Type, zapcore.Int8Type,
114 // zapcore.Uint64Type, zapcore.Uint32Type, zapcore.Uint16Type, zapcore.Uint8Type, zapcore.UintptrType:
115 // return (f.Key,uint64(f.Integer))
116 case zapcore.StringType:
117 return event.String(f.Key, f.String)
118 case zapcore.TimeType:
119 t := time.Unix(0, f.Integer)
120 if f.Interface != nil {
121 t = t.In(f.Interface.(*time.Location))
122 }
123 return event.Value(f.Key, t)
124 case zapcore.StringerType:
125 return event.String(f.Key, stringerToString(f.Interface))
126 case zapcore.NamespaceType:
127 // TODO: ???
128 return event.Label{}
129 case zapcore.SkipType:
130 // TODO: avoid creating a label at all in this case.
131 return event.Label{}
132 default:
133 panic(fmt.Sprintf("unknown field type: %v", f))
134 }
135 }
136
137 // Adapter from encodeStringer in go.uber.org/zap/zapcore/field.go.
138 func stringerToString(stringer interface{}) (s string) {
139 // Try to capture panics (from nil references or otherwise) when calling
140 // the String() method, similar to https://golang.org/src/fmt/print.go#L540
141 defer func() {
142 if err := recover(); err != nil {
143 // If it's a nil pointer, just say "<nil>". The likeliest causes are a
144 // Stringer that fails to guard against nil or a nil pointer for a
145 // value receiver, and in either case, "<nil>" is a nice result.
146 if v := reflect.ValueOf(stringer); v.Kind() == reflect.Ptr && v.IsNil() {
147 s = "<nil>"
148 return
149 }
150 s = fmt.Sprintf("PANIC=%v", err)
151 }
152 }()
153
154 return stringer.(fmt.Stringer).String()
155 }
156
157 func convertLevel(level zapcore.Level) severity.Level {
158 switch level {
159 case zapcore.DebugLevel:
160 return severity.Debug
161 case zapcore.InfoLevel:
162 return severity.Info
163 case zapcore.WarnLevel:
164 return severity.Warning
165 case zapcore.ErrorLevel:
166 return severity.Error
167 case zapcore.DPanicLevel:
168 return severity.Fatal - 1
169 case zapcore.PanicLevel:
170 return severity.Fatal + 1
171 case zapcore.FatalLevel:
172 return severity.Fatal
173 default:
174 return severity.Trace
175 }
176 }