/
ctxdims.go
36 lines (31 loc) · 1.05 KB
/
ctxdims.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
package log
import "context"
// CtxDimensions can propagate log dimensions inside a context object
type CtxDimensions struct {
}
// From returns the dimensions currently set inside a context object
func (c *CtxDimensions) From(ctx context.Context) []interface{} {
existing := ctx.Value(c)
if existing == nil {
return []interface{}{}
}
return existing.([]interface{})
}
// Append returns a new context that appends vals as dimensions for logging
func (c *CtxDimensions) Append(ctx context.Context, vals ...interface{}) context.Context {
if len(vals) == 0 {
return ctx
}
if len(vals)%2 != 0 {
panic("Expected even number of values")
}
existing := c.From(ctx)
newArr := make([]interface{}, 0, len(existing)+len(vals))
newArr = append(newArr, existing...)
newArr = append(newArr, vals...)
return context.WithValue(ctx, c, newArr)
}
// With returns a new Context object that appends the dimensions inside ctx with the context logCtx
func (c *CtxDimensions) With(ctx context.Context, log Logger) *Context {
return NewContext(log).With(c.From(ctx)...)
}