1package klog 2 3import ( 4 "fmt" 5 "math" 6 "os" 7 "sync" 8 9 "github.com/go-kit/log" 10 "github.com/go-kit/log/level" 11) 12 13var maxLevel Level = math.MaxInt32 14var logger = log.NewNopLogger() 15var mu = sync.Mutex{} 16 17// SetLogger redirects klog logging to the given logger. 18// It must be called prior any call to klog. 19func SetLogger(l log.Logger) { 20 mu.Lock() 21 logger = l 22 mu.Unlock() 23} 24 25// ClampLevel clamps the leveled logging at the specified value. 26// It must be called prior any call to klog. 27func ClampLevel(l Level) { 28 mu.Lock() 29 maxLevel = l 30 mu.Unlock() 31} 32 33type Level int32 34 35type Verbose bool 36 37func V(level Level) Verbose { return level <= maxLevel } 38 39func (v Verbose) Enabled() bool { return bool(v) } 40 41func (v Verbose) Info(args ...interface{}) { 42 if v { 43 level.Debug(logger).Log("func", "Verbose.Info", "msg", fmt.Sprint(args...)) 44 } 45} 46 47func (v Verbose) Infoln(args ...interface{}) { 48 if v { 49 level.Debug(logger).Log("func", "Verbose.Infoln", "msg", fmt.Sprint(args...)) 50 } 51} 52 53func (v Verbose) Infof(format string, args ...interface{}) { 54 if v { 55 level.Debug(logger).Log("func", "Verbose.Infof", "msg", fmt.Sprintf(format, args...)) 56 } 57} 58 59func (v Verbose) InfoS(msg string, keysAndValues ...interface{}) { 60 if v { 61 level.Debug(logger).Log("func", "Verbose.InfoS", "msg", msg, keysAndValues) 62 } 63} 64 65func (v Verbose) InfoSDepth(_ int, msg string, keysAndValues ...interface{}) { 66 if v { 67 level.Debug(logger).Log("func", "Verbose.InfoSDepth", "msg", msg, keysAndValues) 68 } 69} 70 71func Info(args ...interface{}) { 72 level.Debug(logger).Log("func", "Info", "msg", fmt.Sprint(args...)) 73} 74 75func InfoDepth(_ int, args ...interface{}) { 76 level.Debug(logger).Log("func", "InfoDepth", "msg", fmt.Sprint(args...)) 77} 78 79func Infoln(args ...interface{}) { 80 level.Debug(logger).Log("func", "Infoln", "msg", fmt.Sprint(args...)) 81} 82 83func Infof(format string, args ...interface{}) { 84 level.Debug(logger).Log("func", "Infof", "msg", fmt.Sprintf(format, args...)) 85} 86 87func InfoS(msg string, keysAndValues ...interface{}) { 88 level.Debug(logger).Log("func", "InfoS", "msg", msg, keysAndValues) 89} 90 91func InfoSDepth(_ int, msg string, keysAndValues ...interface{}) { 92 level.Debug(logger).Log("func", "InfoS", "msg", msg, keysAndValues) 93} 94 95func Warning(args ...interface{}) { 96 level.Warn(logger).Log("func", "Warning", "msg", fmt.Sprint(args...)) 97} 98 99func WarningDepth(_ int, args ...interface{}) { 100 level.Warn(logger).Log("func", "WarningDepth", "msg", fmt.Sprint(args...)) 101} 102 103func Warningln(args ...interface{}) { 104 level.Warn(logger).Log("func", "Warningln", "msg", fmt.Sprint(args...)) 105} 106 107func Warningf(format string, args ...interface{}) { 108 level.Warn(logger).Log("func", "Warningf", "msg", fmt.Sprintf(format, args...)) 109} 110 111func Error(args ...interface{}) { 112 level.Error(logger).Log("func", "Error", "msg", fmt.Sprint(args...)) 113} 114 115func ErrorDepth(_ int, args ...interface{}) { 116 level.Error(logger).Log("func", "ErrorDepth", "msg", fmt.Sprint(args...)) 117} 118 119func Errorln(args ...interface{}) { 120 level.Error(logger).Log("func", "Errorln", "msg", fmt.Sprint(args...)) 121} 122 123func Errorf(format string, args ...interface{}) { 124 level.Error(logger).Log("func", "Errorf", "msg", fmt.Sprintf(format, args...)) 125} 126 127func Fatal(args ...interface{}) { 128 level.Error(logger).Log("func", "Fatal", "msg", fmt.Sprint(args...)) 129 os.Exit(255) 130} 131 132func FatalDepth(_ int, args ...interface{}) { 133 level.Error(logger).Log("func", "FatalDepth", "msg", fmt.Sprint(args...)) 134 os.Exit(255) 135} 136 137func Fatalln(args ...interface{}) { 138 level.Error(logger).Log("func", "Fatalln", "msg", fmt.Sprint(args...)) 139 os.Exit(255) 140} 141 142func Fatalf(format string, args ...interface{}) { 143 level.Error(logger).Log("func", "Fatalf", "msg", fmt.Sprintf(format, args...)) 144 os.Exit(255) 145} 146 147func Exit(args ...interface{}) { 148 level.Error(logger).Log("func", "Exit", "msg", fmt.Sprint(args...)) 149 os.Exit(1) 150} 151 152func ExitDepth(_ int, args ...interface{}) { 153 level.Error(logger).Log("func", "ExitDepth", "msg", fmt.Sprint(args...)) 154 os.Exit(1) 155} 156 157func Exitln(args ...interface{}) { 158 level.Error(logger).Log("func", "Exitln", "msg", fmt.Sprint(args...)) 159 os.Exit(1) 160} 161 162func Exitf(format string, args ...interface{}) { 163 level.Error(logger).Log("func", "Exitf", "msg", fmt.Sprintf(format, args...)) 164 os.Exit(1) 165} 166