github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/logger/implementation/logrus/restore_caller_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 logrus 14 15 import ( 16 "runtime" 17 18 "github.com/sirupsen/logrus" 19 ) 20 21 type logrusCtxKeyCallerT struct{} 22 23 var LogrusCtxKeyCaller = logrusCtxKeyCallerT{} 24 25 // RestoreCallerHook is a logrus.Hook which sets the Caller 26 // from a Context. 27 type RestoreCallerHook struct{} 28 29 var _ logrus.Hook = (*RestoreCallerHook)(nil) 30 31 func newRestoreCallerHook() RestoreCallerHook { 32 return RestoreCallerHook{} 33 } 34 35 func (RestoreCallerHook) Levels() []logrus.Level { 36 return logrus.AllLevels 37 } 38 func (RestoreCallerHook) Fire(entry *logrus.Entry) error { 39 if entry.Context == nil { 40 // It looks like the Hook was used not through the logrus adapter, 41 // but directly by calling logrus logger itself. In this case 42 // we should not do anything. 43 // 44 // This can happen if one creates a logrus adapter providing a logrus Logger 45 // which is also used somewhere outside of this logrus adapter. This is supposed 46 // to be forbidden (see the description of function `New`), but better to 47 // add a bit of defensive code and avoid panics: 48 return nil 49 } 50 51 // restoring the Caller: 52 caller := entry.Context.Value(LogrusCtxKeyCaller) 53 if caller == nil { 54 // no Caller was set 55 return nil 56 } 57 entry.Caller, _ = caller.(*runtime.Frame) 58 59 // working around the check in standard formatters: 60 origLogger := entry.Logger 61 entry.Logger = &logrus.Logger{ 62 Out: origLogger.Out, 63 Hooks: origLogger.Hooks, 64 Formatter: origLogger.Formatter, 65 ReportCaller: true, 66 Level: origLogger.Level, 67 ExitFunc: origLogger.ExitFunc, 68 BufferPool: origLogger.BufferPool, 69 } 70 71 return nil 72 }