github.com/starshine-sys/bcr@v0.21.0/logging.go (about) 1 package bcr 2 3 import ( 4 "log" 5 6 "go.uber.org/zap" 7 ) 8 9 // LogFunc is a function used for logging 10 type LogFunc func(template string, args ...interface{}) 11 12 // Logger is a basic logger 13 type Logger struct { 14 Debug LogFunc 15 Info LogFunc 16 Error LogFunc 17 } 18 19 // NewNoopLogger returns a Logger that does nothing when its functions are called 20 func NewNoopLogger() *Logger { 21 return &Logger{ 22 Debug: func(t string, args ...interface{}) { 23 return 24 }, 25 Info: func(t string, args ...interface{}) { 26 return 27 }, 28 Error: func(t string, args ...interface{}) { 29 return 30 }, 31 } 32 } 33 34 // wrapStdLog returns a function that calls log.Printf with the given prefix 35 func wrapStdLog(prefix string) LogFunc { 36 return func(template string, args ...interface{}) { 37 log.Printf(prefix+": "+template, args...) 38 } 39 } 40 41 // NewStdlibLogger returns a Logger that wraps the standard library's "log" package 42 func NewStdlibLogger(debug bool) *Logger { 43 if debug { 44 return &Logger{ 45 Debug: wrapStdLog("DEBUG"), 46 Info: wrapStdLog("INFO"), 47 Error: wrapStdLog("ERROR"), 48 } 49 } 50 51 return &Logger{ 52 Debug: func(t string, args ...interface{}) { 53 return 54 }, 55 Info: wrapStdLog("INFO"), 56 Error: wrapStdLog("ERROR"), 57 } 58 } 59 60 // NewZapLogger returns a Logger that wraps a SugaredLogger from go.uber.org/zap 61 func NewZapLogger(s *zap.SugaredLogger) *Logger { 62 return &Logger{ 63 Debug: s.Debugf, 64 Info: s.Infof, 65 Error: s.Errorf, 66 } 67 }