github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/logger/types/hook.go (about) 1 // Copyright 2022 Meta Platforms, Inc. and affiliates. 2 // 3 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 // 5 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 // 7 // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 // 9 // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 // 11 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 13 package types 14 15 import ( 16 "github.com/facebookincubator/go-belt" 17 "github.com/facebookincubator/go-belt/pkg/field" 18 ) 19 20 // Hook is a pre-processor for log entries. 21 // It may for example modify them or cancel them. 22 type Hook interface { 23 // ProcessLogEntry is called right before sending the Entry to a Emitter. 24 // 25 // If the returned value is false, then the sending and processing by other hooks 26 // are cancelled. 27 // 28 // It is allowed to modify the entry. 29 ProcessLogEntry(*Entry) bool 30 31 // Flush gracefully empties any buffers the hook may have. 32 Flush() 33 } 34 35 // Hooks is a collection of Hook-s. 36 type Hooks []Hook 37 38 var _ Hook = (Hooks)(nil) 39 40 // ProcessLogEntry implements Hook. 41 func (s Hooks) ProcessLogEntry(e *Entry) bool { 42 for _, hook := range s { 43 if !hook.ProcessLogEntry(e) { 44 return false 45 } 46 } 47 return true 48 } 49 50 // Flush implements Hook. 51 func (s Hooks) Flush() { 52 for _, hook := range s { 53 hook.Flush() 54 } 55 } 56 57 // PreHookResult is a result of computations returned by a PreHook. 58 type PreHookResult struct { 59 // Skip forces a Logger to do not log this Entry. 60 Skip bool 61 62 // ExtraFields adds additional fields to the Entry. 63 ExtraFields field.AbstractFields 64 65 // ExtraEntryProperties adds additional Entry.Properties. 66 ExtraEntryProperties EntryProperties 67 } 68 69 // PreHook is executed very early in a Logger, allowing to discard or change 70 // an Entry before any essential computations are done. 71 // 72 // For example it may be useful for samplers. 73 type PreHook interface { 74 // ProcessInput is executed when functions Log, Trace, Debug, Info, Warn, Error, Panic and Fatal are called. 75 // 76 // TraceIDs are provided by Logger/CompactLogger and the rest arguments are just passed-through. 77 ProcessInput(belt.TraceIDs, Level, ...any) PreHookResult 78 79 // ProcessInputf is executed when functions Logf, Tracef, Debugf, Infof, Warnf, Errorf, Panicf and Fatalf are called. 80 // 81 // TraceIDs are provided by Logger/CompactLogger and the rest arguments are just passed-through. 82 ProcessInputf(belt.TraceIDs, Level, string, ...any) PreHookResult 83 84 // ProcessInputf is executed when functions LogFields, TraceFields, DebugFields, InfoFields, WarnFields, ErrorFields, PanicFields and FatalFields are called. 85 // 86 // TraceIDs are provided by Logger/CompactLogger and the rest arguments are just passed-through. 87 ProcessInputFields(belt.TraceIDs, Level, string, field.AbstractFields) PreHookResult 88 } 89 90 // PreHooks is a collection of PreHook-s. 91 type PreHooks []PreHook 92 93 var _ PreHook = (PreHooks)(nil) 94 95 // ProcessInput implements PreHook. 96 func (s PreHooks) ProcessInput(traceIDs belt.TraceIDs, level Level, values ...any) PreHookResult { 97 var result PreHookResult 98 for _, hook := range s { 99 oneResult := hook.ProcessInput(traceIDs, level, values...) 100 result.Skip = oneResult.Skip 101 if result.Skip { 102 return result 103 } 104 result.ExtraEntryProperties = append(result.ExtraEntryProperties, oneResult.ExtraEntryProperties) 105 result.ExtraFields = field.Add(result.ExtraFields, oneResult.ExtraFields) 106 } 107 108 return result 109 } 110 111 // ProcessInputf implements PreHook. 112 func (s PreHooks) ProcessInputf(traceIDs belt.TraceIDs, level Level, format string, args ...any) PreHookResult { 113 var result PreHookResult 114 for _, hook := range s { 115 oneResult := hook.ProcessInputf(traceIDs, level, format, args...) 116 result.Skip = oneResult.Skip 117 if result.Skip { 118 return result 119 } 120 result.ExtraEntryProperties = append(result.ExtraEntryProperties, oneResult.ExtraEntryProperties) 121 result.ExtraFields = field.Add(result.ExtraFields, oneResult.ExtraFields) 122 } 123 124 return result 125 } 126 127 // ProcessInputFields implements PreHook. 128 func (s PreHooks) ProcessInputFields(traceIDs belt.TraceIDs, level Level, message string, addFields field.AbstractFields) PreHookResult { 129 var result PreHookResult 130 for _, hook := range s { 131 oneResult := hook.ProcessInputFields(traceIDs, level, message, addFields) 132 result.Skip = oneResult.Skip 133 if result.Skip { 134 return result 135 } 136 result.ExtraEntryProperties = append(result.ExtraEntryProperties, oneResult.ExtraEntryProperties) 137 result.ExtraFields = field.Add(result.ExtraFields, oneResult.ExtraFields) 138 } 139 140 return result 141 }