github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/logger/implementation/glog/glog.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 glog 14 15 import ( 16 "github.com/facebookincubator/go-belt/tool/logger/adapter" 17 "github.com/facebookincubator/go-belt/tool/logger/types" 18 "github.com/golang/glog" 19 ) 20 21 // GLog is a types.Emitter implementation based on glog. 22 type GLog struct{} 23 24 var _ types.Emitter = (*GLog)(nil) 25 26 // NewEmitter returns a new instance of a types.Emitter based 27 // on the global instance of glog (the upstream glog does not have 28 // a non-global instance, unfortunately). 29 func NewEmitter(_ ...types.Option) GLog { 30 return GLog{} 31 } 32 33 // New returns a new instance a types.Logger based on glog. 34 func New(opts ...types.Option) types.Logger { 35 return adapter.LoggerFromEmitter(NewEmitter(opts...), opts...) 36 } 37 38 // Flush implements types.Emitter 39 func (l GLog) Flush() { 40 glog.Flush() 41 } 42 43 // Emit implements types.Emitter 44 func (l GLog) Emit(entry *types.Entry) { 45 file, line := entry.Caller.FileLine() 46 logging.printWithFileLine( 47 uint32(SeverityFromLevel(entry.Level)), 48 file, line, 49 entry.Properties.Has(EntryPropertyAlsoStderr(true)), 50 entry.Message, 51 ) 52 } 53 54 // Severity is the internal glog's logging level. 55 type Severity glog.Level 56 57 // SeverityFromLevel converts logger.Level to Severity. 58 func SeverityFromLevel(level types.Level) Severity { 59 switch level { 60 case types.LevelTrace, types.LevelDebug: 61 return 0 62 case types.LevelInfo: 63 return 0 64 case types.LevelWarning: 65 return 1 66 case types.LevelError: 67 return 2 68 case types.LevelPanic, types.LevelFatal: 69 return 3 70 default: 71 return 0 72 } 73 }