github.com/grailbio/base@v0.0.11/common/log/log.go (about) 1 package log 2 3 import ( 4 "context" 5 "fmt" 6 7 "go.uber.org/zap/zapcore" 8 9 "go.uber.org/zap" 10 ) 11 12 var logger = NewLogger(Config{Level: DebugLevel}) 13 14 // SetLoggerConfig sets the logging config. 15 func SetLoggerConfig(config Config) { 16 logger = NewLogger(config) 17 } 18 19 // SetLoggerLevel sets the logging level. 20 func SetLoggerLevel(level zapcore.Level) { 21 SetLoggerConfig(Config{Level: level}) 22 } 23 24 // Instantiate a new logger and assign any key-value pair to addedInfo field in logger to log additional 25 // information specific to service 26 func GetNewLoggerWithDefaultFields(addedInfo ...interface{}) *Logger { 27 return NewLoggerWithDefaultFields(Config{Level: DebugLevel}, addedInfo) 28 } 29 30 // Debug logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 31 // If ctx is nil, all fields from contextFields will be omitted. 32 // If ctx does not contain a key in contextFields, that field will be omitted. 33 func Debug(ctx context.Context, msg string, keysAndValues ...interface{}) { 34 Debugv(ctx, 1, msg, keysAndValues...) 35 } 36 37 // Debugf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. 38 // If ctx is nil, all fields from contextFields will be omitted. 39 // If ctx does not contain a key in contextFields, that field will be omitted. 40 func Debugf(ctx context.Context, fs string, args ...interface{}) { 41 Debugv(ctx, 1, fmt.Sprintf(fs, args...)) 42 } 43 44 // Debugv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 45 // Caller is skipped by skip. 46 // If ctx is nil, all fields from contextFields will be omitted. 47 // If ctx does not contain a key in contextFields, that field will be omitted. 48 func Debugv(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) { 49 logger.Debugv(ctx, skip+1, msg, keysAndValues...) 50 } 51 52 // DebugNoCtx logs a message and variadic key-value pairs. 53 func DebugNoCtx(msg string, keysAndValues ...interface{}) { 54 // context.Background() is a singleton and gets initialized once 55 Debugv(context.Background(), 1, msg, keysAndValues...) 56 } 57 58 // DebugfNoCtx uses fmt.Sprintf to log a templated message. 59 func DebugfNoCtx(fs string, args ...interface{}) { 60 Debugv(context.Background(), 1, fmt.Sprintf(fs, args...)) 61 } 62 63 // Info logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 64 // If ctx is nil, all fields from contextFields will be omitted. 65 // If ctx does not contain a key in contextFields, that field will be omitted. 66 func Info(ctx context.Context, msg string, keysAndValues ...interface{}) { 67 Infov(ctx, 1, msg, keysAndValues...) 68 } 69 70 // Infof uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. 71 // If ctx is nil, all fields from contextFields will be omitted. 72 // If ctx does not contain a key in contextFields, that field will be omitted. 73 func Infof(ctx context.Context, fs string, args ...interface{}) { 74 Infov(ctx, 1, fmt.Sprintf(fs, args...)) 75 } 76 77 // Infov logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 78 // Caller is skipped by skip. 79 // If ctx is nil, all fields from contextFields will be omitted. 80 // If ctx does not contain a key in contextFields, that field will be omitted. 81 func Infov(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) { 82 logger.Infov(ctx, skip+1, msg, keysAndValues...) 83 } 84 85 // InfoNoCtx logs a message and variadic key-value pairs. 86 func InfoNoCtx(msg string, keysAndValues ...interface{}) { 87 // context.Background() is a singleton and gets initialized once 88 Infov(context.Background(), 1, msg, keysAndValues...) 89 } 90 91 // InfofNoCtx uses fmt.Sprintf to log a templated message. 92 func InfofNoCtx(fs string, args ...interface{}) { 93 Infov(context.Background(), 1, fmt.Sprintf(fs, args...)) 94 } 95 96 // Warn logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 97 // If ctx is nil, all fields from contextFields will be omitted. 98 // If ctx does not contain a key in contextFields, that field will be omitted. 99 func Warn(ctx context.Context, msg string, keysAndValues ...interface{}) { 100 Warnv(ctx, 1, msg, keysAndValues...) 101 } 102 103 // Warnf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. 104 // If ctx is nil, all fields from contextFields will be omitted. 105 // If ctx does not contain a key in contextFields, that field will be omitted. 106 func Warnf(ctx context.Context, fs string, args ...interface{}) { 107 Warnv(ctx, 1, fmt.Sprintf(fs, args...)) 108 } 109 110 // Warnv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 111 // Caller is skipped by skip. 112 // If ctx is nil, all fields from contextFields will be omitted. 113 // If ctx does not contain a key in contextFields, that field will be omitted. 114 func Warnv(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) { 115 logger.Warnv(ctx, skip+1, msg, keysAndValues...) 116 } 117 118 // WarnNoCtx logs a message and variadic key-value pairs. 119 func WarnNoCtx(msg string, keysAndValues ...interface{}) { 120 // context.Background() is a singleton and gets initialized once 121 Warnv(context.Background(), 1, msg, keysAndValues...) 122 } 123 124 // WarnfNoCtx uses fmt.Sprintf to log a templated message. 125 func WarnfNoCtx(fs string, args ...interface{}) { 126 Warnv(context.Background(), 1, fmt.Sprintf(fs, args...)) 127 } 128 129 // Fatal logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 130 // If ctx is nil, all fields from contextFields will be omitted. 131 // If ctx does not contain a key in contextFields, that field will be omitted. 132 func Fatal(ctx context.Context, msg string, keysAndValues ...interface{}) { 133 Fatalv(ctx, 1, msg, keysAndValues...) 134 } 135 136 // Fatalf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. 137 // If ctx is nil, all fields from contextFields will be omitted. 138 // If ctx does not contain a key in contextFields, that field will be omitted. 139 func Fatalf(ctx context.Context, fs string, args ...interface{}) { 140 Fatalv(ctx, 1, fmt.Sprintf(fs, args...)) 141 } 142 143 // Fatalv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 144 // Caller is skipped by skip. 145 // If ctx is nil, all fields from contextFields will be omitted. 146 // If ctx does not contain a key in contextFields, that field will be omitted. 147 func Fatalv(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) { 148 logger.Fatalv(ctx, skip+1, msg, keysAndValues...) 149 } 150 151 // FatalNoCtx logs a message and variadic key-value pairs. 152 func FatalNoCtx(msg string, keysAndValues ...interface{}) { 153 // context.Background() is a singleton and gets initialized once 154 Fatalv(context.Background(), 1, msg, keysAndValues...) 155 } 156 157 // FatalfNoCtx uses fmt.Sprintf to log a templated message. 158 func FatalfNoCtx(fs string, args ...interface{}) { 159 Fatalv(context.Background(), 1, fmt.Sprintf(fs, args...)) 160 } 161 162 // Error logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 163 // If ctx is nil, all fields from contextFields will be omitted. 164 // If ctx does not contain a key in contextFields, that field will be omitted. 165 func Error(ctx context.Context, msg string, keysAndValues ...interface{}) { 166 Errorv(ctx, 1, msg, keysAndValues...) 167 } 168 169 // Errorf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. 170 // If ctx is nil, all fields from contextFields will be omitted. 171 // If ctx does not contain a key in contextFields, that field will be omitted. 172 func Errorf(ctx context.Context, fs string, args ...interface{}) { 173 Errorv(ctx, 1, fmt.Sprintf(fs, args...)) 174 } 175 176 // Errorv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 177 // Caller is skipped by skip. 178 // If ctx is nil, all fields from contextFields will be omitted. 179 // If ctx does not contain a key in contextFields, that field will be omitted. 180 func Errorv(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) { 181 logger.Errorv(ctx, skip+1, msg, keysAndValues...) 182 } 183 184 // ErrorNoCtx logs a message and variadic key-value pairs. 185 func ErrorNoCtx(msg string, keysAndValues ...interface{}) { 186 // context.Background() is a singleton and gets initialized once 187 Errorv(context.Background(), 1, msg, keysAndValues...) 188 } 189 190 // ErrorfNoCtx uses fmt.Sprintf to log a templated message. 191 func ErrorfNoCtx(fs string, args ...interface{}) { 192 Errorv(context.Background(), 1, fmt.Sprintf(fs, args...)) 193 } 194 195 // Error logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 196 // If ctx is nil, all fields from contextFields will be omitted. 197 // If ctx does not contain a key in contextFields, that field will be omitted. 198 // Returns an error with the given message for convenience 199 func ErrorAndReturn(ctx context.Context, msg string, keysAndValues ...interface{}) error { 200 return ErrorvAndReturn(ctx, 1, msg, keysAndValues...) 201 } 202 203 // Errorf uses fmt.Errorf to construct an error and log its message 204 // If ctx is nil, all fields from contextFields will be omitted. 205 // If ctx does not contain a key in contextFields, that field will be omitted. 206 // Returns the error for convenicence 207 func ErrorfAndReturn(ctx context.Context, fs string, args ...interface{}) error { 208 err := fmt.Errorf(fs, args...) 209 Errorv(ctx, 1, err.Error()) 210 return err 211 } 212 213 // Errorv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. 214 // Caller is skipped by skip. 215 // If ctx is nil, all fields from contextFields will be omitted. 216 // If ctx does not contain a key in contextFields, that field will be omitted. 217 // Returns an error with the given message for convenience 218 func ErrorvAndReturn(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) error { 219 return logger.ErrorvAndReturn(ctx, skip+1, msg, keysAndValues...) 220 } 221 222 // ErrorNoCtx logs a message and variadic key-value pairs. 223 // Returns an error with the given message for convenience 224 func ErrorNoCtxAndReturn(msg string, keysAndValues ...interface{}) error { 225 // context.Background() is a singleton and gets initialized once 226 return ErrorvAndReturn(context.Background(), 1, msg, keysAndValues...) 227 } 228 229 func InjectTestLogger(testLogger *zap.SugaredLogger) { 230 logger = NewLoggerFromCore(testLogger) 231 }