/
logger.go
419 lines (346 loc) · 13 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* Copyright 2017-2018 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package logger
import (
"fmt"
"runtime"
"strings"
"sync"
"github.com/AISphere/ffdl-commons/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// Constants for logging
const (
// ==============================
// ======= Log field keys =======
// These keys are to be used for structured logging, and will
// be visible to the Elastic Search repository for query and the like.
LogkeyCaller = "caller_info"
LogkeyTrainingID = "training_id"
LogkeyGpuType = "gpu_type"
LogkeyGpuUsage = "gpu_usage"
LogkeyFramework = "framework_name"
LogkeyFrameworkVersion = "framework_version"
LogkeyErrorCode = "error_code"
LogkeyErrorType = "error_type"
LogkeyModelID = "model_id"
LogkeyModelName = "model_name"
LogkeyDBName = "db_name"
LogkeyUserID = "user_id"
LogkeyModule = "module"
LogkeyIsMetrics = "is_metrics"
LogkeyIsFollow = "is_follow"
LogkeyIsSummary = "is_summary"
LogkeyObjectstorePath = "os_path"
LogkeyTrainingDataURI = "training_data_uri"
LogkeyModelDataURI = "model_data_uri"
LogkeyModelFilename = "model_filename"
LogkeyDeployerService = "deployer-service"
LogkeyLcmService = "lifecycle-manager-service"
LogkeyRestAPIService = "rest-api"
LogkeyStorageService = "storage-service"
LogkeyTrainerService = "trainer-service"
LogkeyVolumeManagerService = "volume-manager-service"
LogkeyJobMonitor = "jobmonitor"
// ==============================
// ======= Log categories =======
// These keys are to be used to enable specific keyed log categories, and are meant to
// be used as the second argument to `LocLoggerCategorized(logr *log.Entry, isEnabledKey string)`.
// For example, if I'm trying to debug code associated with training logs, the dev can
// set DLAAS_LOG_GETTRAININGLOGSTREAM to true in the environment when the LCM is launched.
// (There may also be a way to set it dynamically, I'm not sure). This capability is in addition
// to the log levels, such as logrus.DebugLevel, logrus.WarnLevel, etc., which are still applicable.
// DLAAS_LOG_GETTRAININGLOGSTREAM=true
LogCategoryGetTrainingLogStream = "log_GetTrainingLogStream"
LogCategoryGetTrainingLogStreamDefaultValue = false
// DLAAS_LOG_GETTRAININGLOGSTREAMFROMOBJSTORE=true
LogCategoryGetTrainingLogStreamFromObjStore = "log_getTrainingLogStreamFromObjStore"
LogCategoryGetTrainingLogStreamFromObjStoreDefaultValue = false
// DLAAS_LOG_REPOSITORY=true
LogCategoryRepository = "log_repository"
LogCategoryRepositoryDefaultValue = false
// DLAAS_LOG_SERVELOGHANDLER=true
LogCategoryServeLogHandler = "log_serveLogHandler"
LogCategoryServeLogHandlerDefaultValue = false
)
// Ensure that it is initialized only once
var loggerInitOnce sync.Once
// FileInfoFindGood looks at the stack, and tries to find the first entry that
// is not infrastructure related. i.e. that is essentially application code.
func FileInfoFindGood() string {
// Inspect runtime call stack
pc := make([]uintptr, 30)
stackDepth := runtime.Callers(0, pc)
// Try and skip functions on the stack that are clearly infrastructure related. Typically the stack will
// look something like:
// stack[0]: /usr/local/go/src/runtime/extern.go:219
// stack[1]: /home/sboag/git/src/github.com/AISphere/ffdl-commons/logger/logger.go:72
// stack[2]: /home/sboag/git/src/github.com/AISphere/ffdl-commons/logger/logger.go:120
// stack[3]: /home/sboag/git/src/github.com/AISphere/ffdl-commons/logger/logger.go:136
// stack[4]: /home/sboag/git/src/github.com/AISphere/ffdl-commons/service/lcm/service_impl.go:656
// stack[5]: /usr/local/go/src/runtime/asm_amd64.s:2087
// We're only interested in #4 here.
// Enable this if you want to view the stack
//fmt.Print("==============\n")
//for i := 0; i < stackDepth; i++ {
// f := runtime.FuncForPC(pc[i])
// file, line := f.FileLine(pc[i])
// fmt.Printf("stack[%d]: %s:%d\n", i, file, line)
//}
var file string
var line int
var f *runtime.Func
for i := 0; i < stackDepth && i < len(pc); i++ {
f = runtime.FuncForPC(pc[i])
file, line = f.FileLine(pc[i])
// There might ought to be some sort of registry for these hard-coded patterns.
if strings.Contains(file, ".pb.") {
continue
}
if strings.Contains(file, "runtime/extern.go") {
continue
}
if strings.Contains(file, "-commons/logger/logger.go") {
continue
}
if strings.Contains(file, "/logging_impl.go") {
continue
}
break
}
// Truncate abs file path
if slash := strings.LastIndex(file, "/"); slash >= 0 {
file2 := file[slash:]
clipped := file[:slash]
// But, better if it's qualified one level, given some generic filenames in DLaaS.
// Could go up to DLaaS root, I suppose, but would be pretty verbose.
if slash := strings.LastIndex(clipped, "/"); slash >= 0 {
file2 = clipped[slash+1:]+file2
}
file = file2
}
// Truncate package name
funcName := f.Name()
if slash := strings.LastIndex(funcName, "."); slash >= 0 {
funcName = funcName[slash+1:]
}
return fmt.Sprintf("%s:%d %s -", file, line, funcName)
}
// NewDlaaSLogData construct log data object.
func NewDlaaSLogData(serviceName string) log.Fields {
f := FileInfoFindGood()
data := log.Fields{LogkeyCaller: f}
data[LogkeyModule] = serviceName
return data
}
// LogServiceBasic Construct new basic logger for service.
func LogServiceBasic(serviceName string) *log.Entry {
data := NewDlaaSLogData(serviceName)
return &log.Entry{Logger: log.StandardLogger(), Data: data}
}
// LogServiceBasicWithFields Construct new basic logger for service, with key/value pairs.
func LogServiceBasicWithFields(serviceName string, fields log.Fields) *log.Entry {
data := NewDlaaSLogData(serviceName)
for k, v := range fields {
data[k] = v
}
return &log.Entry{Logger: log.StandardLogger(), Data: data}
}
// Config initializes the logging by setting the env var LOGLEVEL=<level>.
// Valid level values are: panic, fatal, error, warn, info, debug
func Config() {
loggerInitOnce.Do(func() {
level := viper.GetString(config.LogLevelKey)
if level != "" {
val, err := log.ParseLevel(level)
if err != nil {
panic(fmt.Sprintf("Error setting up logger: %s", err.Error()))
}
log.SetLevel(val)
}
loggingType := viper.GetString(config.LoggingType)
switch loggingType {
case config.LoggingTypeJson:
log.SetFormatter(&log.JSONFormatter{})
case config.LoggingTypeText:
log.SetFormatter(&log.TextFormatter{})
default: // any other env will use local settings (assuming outside SL)
env := viper.GetString(config.EnvKey)
switch env {
case config.DevelopmentEnv:
log.SetFormatter(&log.JSONFormatter{})
case config.StagingEnv:
log.SetFormatter(&log.JSONFormatter{})
case config.ProductionEnv:
log.SetFormatter(&log.JSONFormatter{})
default: // any other env will use local settings (assuming outside SL)
log.SetFormatter(&log.TextFormatter{})
}
}
viper.SetDefault(LogCategoryGetTrainingLogStream, LogCategoryGetTrainingLogStreamDefaultValue)
viper.SetDefault(LogCategoryGetTrainingLogStreamFromObjStore,
LogCategoryGetTrainingLogStreamFromObjStoreDefaultValue)
viper.SetDefault(LogCategoryRepository, LogCategoryRepositoryDefaultValue)
viper.SetDefault(LogCategoryServeLogHandler, LogCategoryServeLogHandlerDefaultValue)
})
// otherwise use logrus' default value
}
// LocLoggingEntry wraps another logger, and add code location information about where the the log entry occurs.
// Because logging can be expensive, especially logging that needs to figure out
// it's location, this class also has a way to disable the logging for debug/informational
// entries.
type LocLoggingEntry struct {
Logger *log.Entry
// If true, enable functions at the level of debug, info, and print.
// Warn, Error, Fatal, and Panic are always enabled.
Enabled bool
}
// LocLogger simply creates a LocLoggingEntry that wraps another logger.
func LocLogger(logr *log.Entry) *LocLoggingEntry {
logger := new(LocLoggingEntry)
logger.Logger = logr
logger.Enabled = true
return logger
}
// LocLoggerCategorized creates a LocLoggingEntry that wraps another logger, and accepts
// a string that is used with viper to say whether the informational/debug logging
// is enabled for this logger.
func LocLoggerCategorized(logr *log.Entry, isEnabledKey string) *LocLoggingEntry {
logger := new(LocLoggingEntry)
logger.Logger = logr
logger.Enabled = viper.GetBool(isEnabledKey)
return logger
}
// MakeNew makes a new LocLoggingEntry from an existing LocLoggingEntry,
// but using a new inner logger.
func (entry *LocLoggingEntry) MakeNew(logr *log.Entry) *LocLoggingEntry {
logger := new(LocLoggingEntry)
logger.Logger = logr
logger.Enabled = entry.Enabled
return logger
}
// withLoc returns a new logger that has recorded the code location where it occurs.
func (entry *LocLoggingEntry) withLoc() *LocLoggingEntry {
f := FileInfoFindGood()
data := log.Fields{LogkeyCaller: f}
newLogger := entry.Logger.WithFields(data)
return entry.MakeNew(newLogger)
}
// Add an error as single field (using the key defined in ErrorKey) to the Entry.
func (entry *LocLoggingEntry) WithError(err error) *LocLoggingEntry {
return entry.MakeNew(entry.Logger.WithError(err))
}
// Add a single field to the Entry.
func (entry *LocLoggingEntry) WithField(key string, value interface{}) *LocLoggingEntry {
return entry.MakeNew(entry.Logger.WithField(key, value))
}
// Add a map of fields to the Entry.
func (entry *LocLoggingEntry) WithFields(fields log.Fields) *LocLoggingEntry {
return entry.MakeNew(entry.Logger.WithFields(fields))
}
func (entry *LocLoggingEntry) Debug(args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Debug(args...)
}
}
func (entry *LocLoggingEntry) Print(args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Print(args...)
}
}
func (entry *LocLoggingEntry) Info(args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Info(args...)
}
}
func (entry *LocLoggingEntry) Warn(args ...interface{}) {
entry.withLoc().Logger.Warn(args...)
}
func (entry *LocLoggingEntry) Warning(args ...interface{}) {
entry.withLoc().Logger.Warning(args...)
}
func (entry *LocLoggingEntry) Error(args ...interface{}) {
entry.withLoc().Logger.Error(args...)
}
func (entry *LocLoggingEntry) Fatal(args ...interface{}) {
entry.withLoc().Logger.Fatal(args...)
}
func (entry *LocLoggingEntry) Panic(args ...interface{}) {
entry.withLoc().Logger.Panic(args...)
}
// Entry Printf family functions
func (entry *LocLoggingEntry) Debugf(format string, args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Debugf(format, args...)
}
}
func (entry *LocLoggingEntry) Infof(format string, args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Infof(format, args...)
}
}
func (entry *LocLoggingEntry) Printf(format string, args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Printf(format, args...)
}
}
func (entry *LocLoggingEntry) Warnf(format string, args ...interface{}) {
entry.withLoc().Logger.Warnf(format, args...)
}
func (entry *LocLoggingEntry) Warningf(format string, args ...interface{}) {
entry.withLoc().Logger.Warningf(format, args...)
}
func (entry *LocLoggingEntry) Errorf(format string, args ...interface{}) {
entry.withLoc().Logger.Errorf(format, args...)
}
func (entry *LocLoggingEntry) Fatalf(format string, args ...interface{}) {
entry.withLoc().Logger.Fatalf(format, args...)
}
func (entry *LocLoggingEntry) Panicf(format string, args ...interface{}) {
entry.withLoc().Logger.Panicf(format, args...)
}
// Entry Println family functions
func (entry *LocLoggingEntry) Debugln(args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Debugln(args...)
}
}
func (entry *LocLoggingEntry) Infoln(args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Infoln(args...)
}
}
func (entry *LocLoggingEntry) Println(args ...interface{}) {
if entry.Enabled == true {
entry.withLoc().Logger.Println(args...)
}
}
func (entry *LocLoggingEntry) Warnln(args ...interface{}) {
entry.withLoc().Logger.Warnln(args...)
}
func (entry *LocLoggingEntry) Warningln(args ...interface{}) {
entry.withLoc().Logger.Warnln(args...)
}
func (entry *LocLoggingEntry) Errorln(args ...interface{}) {
entry.withLoc().Logger.Errorln(args...)
}
func (entry *LocLoggingEntry) Fatalln(args ...interface{}) {
entry.withLoc().Logger.Fatalln(args...)
}
func (entry *LocLoggingEntry) Panicln(args ...interface{}) {
entry.withLoc().Logger.Panicln(args...)
}