github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/common/log.go (about) 1 // Copyright (c) 2017-2018 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package common 16 17 import ( 18 "go.uber.org/zap" 19 ) 20 21 // Logger is a general logger interface 22 type Logger interface { 23 // Log at debug level 24 Debug(args ...interface{}) 25 26 // Log at debug level with fmt.Printf-like formatting 27 Debugf(format string, args ...interface{}) 28 29 // Log at info level 30 Info(args ...interface{}) 31 32 // Log at info level with fmt.Printf-like formatting 33 Infof(format string, args ...interface{}) 34 35 // Log at warning level 36 Warn(args ...interface{}) 37 38 // Log at warning level with fmt.Printf-like formatting 39 Warnf(format string, args ...interface{}) 40 41 // Log at error level 42 Error(args ...interface{}) 43 44 // Log at error level with fmt.Printf-like formatting 45 Errorf(format string, args ...interface{}) 46 47 // Log at fatal level, then terminate process (irrecoverable) 48 Fatal(args ...interface{}) 49 50 // Log at fatal level with fmt.Printf-like formatting, then terminate process (irrecoverable) 51 Fatalf(format string, args ...interface{}) 52 53 // Log at panic level, then panic (recoverable) 54 Panic(args ...interface{}) 55 56 // Log at panic level with fmt.Printf-like formatting, then panic (recoverable) 57 Panicf(format string, args ...interface{}) 58 59 // Return a logger with the specified key-value pair set, to be logged in a subsequent normal logging call 60 With(args ...interface{}) Logger 61 } 62 63 // LoggerFactory defines the log factory ares needs. 64 type LoggerFactory interface { 65 // GetDefaultLogger returns the default logger. 66 GetDefaultLogger() Logger 67 // GetLogger returns logger given the logger name. 68 GetLogger(name string) Logger 69 } 70 71 // ZapLoggerFactory is the stdlog implementation of LoggerFactory 72 type ZapLoggerFactory struct { 73 logger *ZapLogger 74 } 75 76 // NewLoggerFactory creates a default zap LoggerFactory implementation. 77 func NewLoggerFactory() LoggerFactory { 78 return &ZapLoggerFactory{ 79 &ZapLogger{ 80 zap.NewExample().Sugar(), 81 }, 82 } 83 } 84 85 // GetDefaultLogger returns the default zap logger. 86 func (r *ZapLoggerFactory) GetDefaultLogger() Logger { 87 return r.logger 88 } 89 90 // GetLogger of ZapLoggerFactory ignores the given name and just return the default logger. 91 func (r *ZapLoggerFactory) GetLogger(name string) Logger { 92 return r.logger 93 } 94 95 // ZapLogger is wrapper of zap 96 type ZapLogger struct { 97 sugaredLogger *zap.SugaredLogger 98 } 99 100 // Debug is log at debug level 101 func (z *ZapLogger) Debug(args ...interface{}) { 102 z.sugaredLogger.Debug(args...) 103 } 104 105 // Debugf is log at debug level with fmt.Printf-like formatting 106 func (z *ZapLogger) Debugf(format string, args ...interface{}) { 107 z.sugaredLogger.Debugf(format, args...) 108 } 109 110 // Info is log at info level 111 func (z *ZapLogger) Info(args ...interface{}) { 112 z.sugaredLogger.Info(args...) 113 } 114 115 // Infof is log at info level with fmt.Printf-like formatting 116 func (z *ZapLogger) Infof(format string, args ...interface{}) { 117 z.sugaredLogger.Infof(format, args...) 118 } 119 120 // Warn is log at warning level 121 func (z *ZapLogger) Warn(args ...interface{}) { 122 z.sugaredLogger.Warn(args...) 123 } 124 125 // Warnf is log at warning level with fmt.Printf-like formatting 126 func (z *ZapLogger) Warnf(format string, args ...interface{}) { 127 z.sugaredLogger.Warnf(format, args...) 128 } 129 130 // Error is log at error level 131 func (z *ZapLogger) Error(args ...interface{}) { 132 z.sugaredLogger.Error(args...) 133 } 134 135 // Errorf is log at error level with fmt.Printf-like formatting 136 func (z *ZapLogger) Errorf(format string, args ...interface{}) { 137 z.sugaredLogger.Errorf(format, args...) 138 } 139 140 // Fatal is log at fatal level, then terminate process (irrecoverable) 141 func (z *ZapLogger) Fatal(args ...interface{}) { 142 z.sugaredLogger.Fatal(args...) 143 } 144 145 // Fatalf is log at fatal level with fmt.Printf-like formatting, then terminate process (irrecoverable) 146 func (z *ZapLogger) Fatalf(format string, args ...interface{}) { 147 z.sugaredLogger.Fatalf(format, args...) 148 } 149 150 // Panic is log at panic level, then panic (recoverable) 151 func (z *ZapLogger) Panic(args ...interface{}) { 152 z.sugaredLogger.Panic(args...) 153 } 154 155 // Panicf is log at panic level with fmt.Printf-like formatting, then panic (recoverable) 156 func (z *ZapLogger) Panicf(format string, args ...interface{}) { 157 z.sugaredLogger.Panicf(format, args...) 158 } 159 160 // With returns a logger with the specified key-value pair set, to be logged in a subsequent normal logging call 161 func (z *ZapLogger) With(args ...interface{}) Logger { 162 return &ZapLogger{ 163 z.sugaredLogger.With(args...), 164 } 165 }