|
14 | 14 | package log |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "strings" |
| 18 | + |
17 | 19 | "github.com/go-logr/logr" |
18 | 20 |
|
19 | 21 | "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" |
20 | 22 | acktypes "github.com/aws-controllers-k8s/runtime/pkg/types" |
21 | 23 | ) |
22 | 24 |
|
| 25 | +// ResourceLogger is a wrapper around a logr.Logger that writes log messages |
| 26 | +// about resources involved in a controller loop. It implements |
| 27 | +// `pkg/types.Logger` |
| 28 | +type ResourceLogger struct { |
| 29 | + log logr.Logger |
| 30 | + res acktypes.AWSResource |
| 31 | + blockDepth int |
| 32 | +} |
| 33 | + |
| 34 | +// WithValues adapts the internal logger with a set of additional values |
| 35 | +func (rl *ResourceLogger) WithValues( |
| 36 | + values ...interface{}, |
| 37 | +) { |
| 38 | + rl.log = rl.log.WithValues(values...) |
| 39 | +} |
| 40 | + |
| 41 | +// Debug writes a supplied log message about a resource that includes a set of |
| 42 | +// standard log values for the resource's kind, namespace, name, etc |
| 43 | +func (rl *ResourceLogger) Debug( |
| 44 | + msg string, |
| 45 | + additionalValues ...interface{}, |
| 46 | +) { |
| 47 | + AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg) |
| 48 | +} |
| 49 | + |
| 50 | +// Info writes a supplied log message about a resource that includes a |
| 51 | +// set of standard log values for the resource's kind, namespace, name, etc |
| 52 | +func (rl *ResourceLogger) Info( |
| 53 | + msg string, |
| 54 | + additionalValues ...interface{}, |
| 55 | +) { |
| 56 | + AdaptResource(rl.log, rl.res, additionalValues...).V(0).Info(msg) |
| 57 | +} |
| 58 | + |
| 59 | +// Enter logs an entry to a function or code block |
| 60 | +func (rl *ResourceLogger) Enter( |
| 61 | + name string, // name of the function or code block we're entering |
| 62 | + additionalValues ...interface{}, |
| 63 | +) { |
| 64 | + if rl.log.V(1).Enabled() { |
| 65 | + rl.blockDepth++ |
| 66 | + depth := strings.Repeat(">", rl.blockDepth) |
| 67 | + msg := depth + " " + name |
| 68 | + AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Exit logs an exit from a function or code block |
| 73 | +func (rl *ResourceLogger) Exit( |
| 74 | + name string, // name of the function or code block we're exiting |
| 75 | + err error, |
| 76 | + additionalValues ...interface{}, |
| 77 | +) { |
| 78 | + if rl.log.V(1).Enabled() { |
| 79 | + depth := strings.Repeat("<", rl.blockDepth) |
| 80 | + msg := depth + " " + name |
| 81 | + if err != nil { |
| 82 | + additionalValues = append(additionalValues, "error") |
| 83 | + additionalValues = append(additionalValues, err) |
| 84 | + } |
| 85 | + AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg) |
| 86 | + rl.blockDepth-- |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Trace logs an entry to a function or code block and returns a functor |
| 91 | +// that can be called to log the exit of the function or code block |
| 92 | +func (rl *ResourceLogger) Trace( |
| 93 | + name string, |
| 94 | + additionalValues ...interface{}, |
| 95 | +) acktypes.TraceExiter { |
| 96 | + rl.Enter(name, additionalValues...) |
| 97 | + f := func(err error, args ...interface{}) { |
| 98 | + rl.Exit(name, err, args...) |
| 99 | + } |
| 100 | + return f |
| 101 | +} |
| 102 | + |
| 103 | +// NewResourceLogger returns a resourceLogger that can write log messages about |
| 104 | +// a resource. |
| 105 | +func NewResourceLogger( |
| 106 | + log logr.Logger, |
| 107 | + res acktypes.AWSResource, |
| 108 | + additionalValues ...interface{}, |
| 109 | +) *ResourceLogger { |
| 110 | + return &ResourceLogger{ |
| 111 | + log: AdaptResource(log, res, additionalValues...), |
| 112 | + res: res, |
| 113 | + blockDepth: 0, |
| 114 | + } |
| 115 | +} |
| 116 | + |
23 | 117 | // AdaptResource returns a logger with log values set for the resource's kind, |
24 | 118 | // namespace, name, etc |
25 | 119 | func AdaptResource( |
|
0 commit comments