| 1 |
// Copyright (c) 2016 Uber Technologies, Inc. |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to deal |
| 5 |
// in the Software without restriction, including without limitation the rights |
| 6 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
// copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
// THE SOFTWARE. |
| 20 |
|
| 21 |
package zapcore |
| 22 |
|
| 23 |
import "go.uber.org/multierr" |
| 24 |
|
| 25 |
type hooked struct { |
| 26 |
Core |
| 27 |
funcs []func(Entry) error |
| 28 |
} |
| 29 |
|
| 30 |
var ( |
| 31 |
_ Core = (*hooked)(nil) |
| 32 |
_ leveledEnabler = (*hooked)(nil) |
| 33 |
) |
| 34 |
|
| 35 |
// RegisterHooks wraps a Core and runs a collection of user-defined callback |
| 36 |
// hooks each time a message is logged. Execution of the callbacks is blocking. |
| 37 |
// |
| 38 |
// This offers users an easy way to register simple callbacks (e.g., metrics |
| 39 |
// collection) without implementing the full Core interface. |
| 40 |
func RegisterHooks(core Core, hooks ...func(Entry) error) Core { |
| 41 |
funcs := append([]func(Entry) error{}, hooks...) |
| 42 |
return &hooked{ |
| 43 |
Core: core, |
| 44 |
funcs: funcs, |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func (h *hooked) Level() Level { |
| 49 |
return LevelOf(h.Core) |
| 50 |
} |
| 51 |
|
| 52 |
func (h *hooked) Check(ent Entry, ce *CheckedEntry) *CheckedEntry { |
| 53 |
// Let the wrapped Core decide whether to log this message or not. This |
| 54 |
// also gives the downstream a chance to register itself directly with the |
| 55 |
// CheckedEntry. |
| 56 |
if downstream := h.Core.Check(ent, ce); downstream != nil { |
| 57 |
return downstream.AddCore(ent, h) |
| 58 |
} |
| 59 |
return ce |
| 60 |
} |
| 61 |
|
| 62 |
func (h *hooked) With(fields []Field) Core { |
| 63 |
return &hooked{ |
| 64 |
Core: h.Core.With(fields), |
| 65 |
funcs: h.funcs, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func (h *hooked) Write(ent Entry, _ []Field) error { |
| 70 |
// Since our downstream had a chance to register itself directly with the |
| 71 |
// CheckedMessage, we don't need to call it here. |
| 72 |
var err error |
| 73 |
for i := range h.funcs { |
| 74 |
err = multierr.Append(err, h.funcs[i](ent)) |
| 75 |
} |
| 76 |
return err |
| 77 |
} |