-
Notifications
You must be signed in to change notification settings - Fork 4
/
initializer.go
678 lines (591 loc) · 21.3 KB
/
initializer.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// Copyright (c) 2020 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
// Package rklogger contains couple of utility functions for initializing zap logger.
package rklogger
import (
"encoding/json"
"errors"
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path"
"reflect"
)
var (
// StdoutEncoderConfig is default zap logger encoder config whose output path is stdout.
StdoutEncoderConfig = NewZapStdoutEncoderConfig()
// StdoutLoggerConfig is default zap logger config whose output path is stdout.
StdoutLoggerConfig = &zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: true,
Encoding: "console",
DisableStacktrace: true,
EncoderConfig: *StdoutEncoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
// StdoutLogger is default zap logger whose output path is stdout.
StdoutLogger, _ = StdoutLoggerConfig.Build()
// NoopLogger is default zap noop logger.
NoopLogger = zap.NewNop()
// EventLoggerConfigBytes is default zap logger which is used by EventLogger.
EventLoggerConfigBytes = []byte(`{
"level": "info",
"encoding": "console",
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"initialFields": {},
"encoderConfig": {
"messageKey": "msg",
"levelKey": "",
"nameKey": "",
"timeKey": "",
"callerKey": "",
"stacktraceKey": "",
"callstackKey": "",
"errorKey": "",
"timeEncoder": "iso8601",
"fileKey": "",
"levelEncoder": "capital",
"durationEncoder": "second",
"callerEncoder": "full",
"nameEncoder": "full"
},
"maxsize": 1024,
"maxage": 7,
"maxbackups": 3,
"localtime": true,
"compress": true
}`)
// Default EventLogger and EventLoggerConfig.
EventLogger, EventLoggerConfig, _ = NewZapLoggerWithBytes(EventLoggerConfigBytes, JSON)
// LumberjackConfig is default lumberjack config.
LumberjackConfig = NewLumberjackConfigDefault()
)
// FileType is a config file type which support json and yaml currently.
type FileType int
const (
// JSON https://www.json.org/
JSON FileType = 0
// YAML https://yaml.org/
YAML FileType = 1
// EncodingConsole console encoding style of logging
EncodingConsole = "console"
// EncodingJson console encoding style of logging
EncodingJson = "json"
)
// Stringfy above config file types.
func (fileType FileType) String() string {
names := [...]string{"JSON", "YAML"}
// Please do not forget to change the boundary while adding a new config file types
if fileType < JSON || fileType > YAML {
return "UNKNOWN"
}
return names[fileType]
}
// NewZapLoggerWithOverride create new zap.Logger with override
func NewZapLoggerWithOverride(loggerEncoding string, loggerOutputPath ...string) (*zap.Logger, error) {
zapLoggerConfig := NewZapStdoutConfig()
lumberjackConfig := NewLumberjackConfigDefault()
if loggerEncoding == EncodingJson || len(loggerOutputPath) > 0 {
if loggerEncoding == EncodingJson {
zapLoggerConfig.Encoding = EncodingJson
}
if len(loggerOutputPath) > 0 {
zapLoggerConfig.OutputPaths = toAbsPath(loggerOutputPath...)
}
if lumberjackConfig == nil {
lumberjackConfig = NewLumberjackConfigDefault()
}
}
if logger, err := NewZapLoggerWithConf(zapLoggerConfig, lumberjackConfig, zap.AddCaller()); err != nil {
return nil, err
} else {
return logger, nil
}
}
// NewZapEventConfig creates new zap.Config for EventLogger
func NewZapEventConfig() *zap.Config {
_, config, _ := NewZapLoggerWithBytes(EventLoggerConfigBytes, JSON)
return config
}
// NewLumberjackConfigDefault creates new default lumberjack config
func NewLumberjackConfigDefault() *lumberjack.Logger {
return &lumberjack.Logger{
MaxSize: 1024,
MaxAge: 7,
MaxBackups: 3,
LocalTime: true,
Compress: true,
}
}
// NewZapStdoutEncoderConfig creates new stdout encoder config
func NewZapStdoutEncoderConfig() *zapcore.EncoderConfig {
return &zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
// NewZapStdoutConfig creates new stdout config
func NewZapStdoutConfig() *zap.Config {
return &zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: true,
Encoding: "console",
DisableStacktrace: true,
EncoderConfig: *NewZapStdoutEncoderConfig(),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
}
// NewZapLoggerWithBytes inits zap logger with byte array from content of config file
// lumberjack.Logger could be empty, if not provided,
// then, we will use default write sync
func NewZapLoggerWithBytes(raw []byte, fileType FileType, opts ...zap.Option) (*zap.Logger, *zap.Config, error) {
if raw == nil {
return nil, nil, errors.New("input byte array is nil")
}
if len(raw) == 0 {
return nil, nil, errors.New("byte array is empty")
}
// Initialize zap logger from config file
var logger *zap.Logger
var err error
zapConfig := &zap.Config{}
lumberConfig := &lumberjack.Logger{}
if fileType == JSON {
// parse zap json file
if err := json.Unmarshal(raw, zapConfig); err != nil {
return nil, nil, err
}
// parse lumberjack json file
if err := json.Unmarshal(raw, lumberConfig); err != nil {
return nil, nil, err
}
logger, err = NewZapLoggerWithConf(zapConfig, lumberConfig, opts...)
} else if fileType == YAML {
// parse zap yaml file
if err := yaml.Unmarshal(raw, zapConfig); err != nil {
return nil, nil, err
}
// parse lumberjack yaml file
if err := yaml.Unmarshal(raw, lumberConfig); err != nil {
return nil, nil, err
}
logger, err = NewZapLoggerWithConf(zapConfig, lumberConfig, opts...)
} else {
logger, err = nil, errors.New("invalid config file")
}
// make sure we return nil for logger and logger config
if err != nil {
return nil, nil, err
}
return logger, zapConfig, err
}
// NewZapLoggerWithConfPath init zap logger with config file path
// File path needs to be absolute path
// lumberjack.Logger could be empty, if not provided,
// then, we will use default write sync
func NewZapLoggerWithConfPath(filePath string, fileType FileType, opts ...zap.Option) (*zap.Logger, *zap.Config, error) {
if len(filePath) == 0 {
return nil, nil, errors.New("file path is empty")
}
// Initialize zap logger from config file
var logger *zap.Logger
var err error
var config *zap.Config
err = validateFilePath(filePath)
if err == nil {
bytes, readErr := ioutil.ReadFile(filePath)
if readErr != nil {
return logger, config, readErr
}
logger, config, err = NewZapLoggerWithBytes(bytes, fileType, opts...)
}
return logger, config, err
}
// NewZapLoggerWithConfAndSyncer
// For backward compatibility with NewZapLoggerWithConf
func NewZapLoggerWithConfAndSyncer(config *zap.Config, lumber *lumberjack.Logger, extraSyncers []zapcore.WriteSyncer, opts ...zap.Option) (*zap.Logger, error) {
// Validate parameters
if config == nil {
return nil, errors.New("zap config is nil")
}
if lumber == nil {
return config.Build(opts...)
}
sync := make([]zapcore.WriteSyncer, 0, 0)
if extraSyncers != nil {
sync = append(sync, extraSyncers...)
}
// Iterate output path and attach to lumberjack
// Remember, each logger will use same lumberjack logger configuration
for i := range config.OutputPaths {
if config.OutputPaths[i] != "stdout" && config.OutputPaths[i] != "stderr" {
lumberNew := &lumberjack.Logger{
Filename: config.OutputPaths[i],
MaxAge: lumber.MaxAge,
MaxBackups: lumber.MaxBackups,
MaxSize: lumber.MaxSize,
Compress: lumber.Compress,
LocalTime: lumber.LocalTime,
}
sync = append(sync, zapcore.AddSync(lumberNew))
} else {
stdout, close, err := zap.Open(config.OutputPaths[i])
// just close the syncer if err occurs
if err != nil {
close()
} else {
sync = append(sync, stdout)
}
}
}
core := zapcore.NewCore(
generateEncoder(config),
zap.CombineWriteSyncers(sync...),
config.Level)
// add initial fields
initialFields := make([]zap.Field, 0, 0)
for k, v := range config.InitialFields {
initialFields = append(initialFields, zap.Any(k, v))
}
// add error output sync
errSink := make([]zapcore.WriteSyncer, 0, 0)
if len(config.ErrorOutputPaths) > 0 {
for i := range config.ErrorOutputPaths {
if config.ErrorOutputPaths[i] != "stdout" && config.ErrorOutputPaths[i] != "stderr" {
lumberNew := &lumberjack.Logger{
Filename: config.ErrorOutputPaths[i],
MaxAge: lumber.MaxAge,
MaxBackups: lumber.MaxBackups,
MaxSize: lumber.MaxSize,
Compress: lumber.Compress,
LocalTime: lumber.LocalTime,
}
errSink = append(errSink, zapcore.AddSync(lumberNew))
} else {
stdout, close, err := zap.Open(config.ErrorOutputPaths[i])
// just close the syncer if err occurs
if err != nil {
close()
} else {
errSink = append(errSink, stdout)
}
}
}
opts = append(opts, zap.ErrorOutput(zap.CombineWriteSyncers(errSink...)))
}
return zap.New(core, opts...).With(initialFields...), nil
}
// NewZapLoggerWithConf inits zap logger with config
// lumberjack.Logger could be empty, if not provided,
// then, we will use default write sync
func NewZapLoggerWithConf(config *zap.Config, lumber *lumberjack.Logger, opts ...zap.Option) (*zap.Logger, error) {
return NewZapLoggerWithConfAndSyncer(config, lumber, nil, opts...)
}
// NewLumberjackLoggerWithBytes inits lumberjack logger as write sync with raw byte array of config file
func NewLumberjackLoggerWithBytes(raw []byte, fileType FileType) (*lumberjack.Logger, error) {
if raw == nil {
return nil, errors.New("input byte array is nil")
}
if len(raw) == 0 {
return nil, errors.New("byte array is empty")
}
logger := &lumberjack.Logger{}
// unmarshal as yaml
if fileType == YAML {
if err := yaml.Unmarshal(raw, logger); err != nil {
return nil, err
}
} else if fileType == JSON {
if err := json.Unmarshal(raw, logger); err != nil {
return nil, err
}
} else {
return nil, errors.New("unknown type")
}
return logger, nil
}
// NewLumberjackLoggerWithConfPath inits lumberjack logger as write sync with lumberjack config file path
// File path needs to be absolute path
func NewLumberjackLoggerWithConfPath(filePath string, fileType FileType) (*lumberjack.Logger, error) {
if len(filePath) == 0 {
return nil, errors.New("file path is empty")
}
var logger *lumberjack.Logger
var err error
err = validateFilePath(filePath)
if err == nil {
bytes, readErr := ioutil.ReadFile(filePath)
if readErr == nil {
logger, err = NewLumberjackLoggerWithBytes(bytes, fileType)
} else {
err = readErr
}
}
return logger, err
}
func validateFilePath(filePath string) error {
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
err = errors.New(fmt.Sprintf("file does not exists, filePath:%s", filePath))
} else {
err = errors.New(fmt.Sprintf("error thrown while reading file, filePath:%s", filePath))
}
}
return err
}
// Generate zap encoder from zap config
func generateEncoder(config *zap.Config) zapcore.Encoder {
if config.Encoding == "json" {
return zapcore.NewJSONEncoder(config.EncoderConfig)
}
// default is console encoding
return zapcore.NewConsoleEncoder(config.EncoderConfig)
}
// Parse relative path, convert it to current working directory
func toAbsoluteWorkingDir(filePath string) (string, error) {
if path.IsAbs(filePath) {
return filePath, nil
}
dir, err := os.Getwd()
if err != nil {
return "", err
}
// relative path, add current working directory
return path.Clean(path.Join(dir, filePath)), nil
}
// TransformToZapConfig transforms wrapped zap config into zap.Config
func TransformToZapConfig(wrap *ZapConfigWrap) *zap.Config {
if wrap == nil {
return nil
}
level := zap.NewAtomicLevel()
if err := level.UnmarshalText([]byte(wrap.Level)); err != nil {
level.SetLevel(zapcore.InfoLevel)
}
config := &zap.Config{
Level: level,
Development: wrap.Development,
DisableCaller: wrap.DisableCaller,
DisableStacktrace: wrap.DisableStacktrace,
Sampling: wrap.Sampling,
Encoding: wrap.Encoding,
EncoderConfig: wrap.EncoderConfig,
OutputPaths: wrap.OutputPaths,
ErrorOutputPaths: wrap.ErrorOutputPaths,
InitialFields: wrap.InitialFields,
}
return config
}
// TransformToZapConfigWrap unmarshals zap.config
func TransformToZapConfigWrap(config *zap.Config) *ZapConfigWrap {
return &ZapConfigWrap{
Level: config.Level.String(),
Development: config.Development,
DisableCaller: config.DisableCaller,
DisableStacktrace: config.DisableStacktrace,
Sampling: config.Sampling,
Encoding: config.Encoding,
EncoderConfig: config.EncoderConfig,
OutputPaths: config.OutputPaths,
ErrorOutputPaths: config.ErrorOutputPaths,
InitialFields: config.InitialFields,
}
}
// Marshal zapcore.NameEncoder
func marshalZapNameEncoder(encoder zapcore.NameEncoder) string {
switch encoder {
default:
return "full"
}
}
// Marshal zapcore.CallerEncoder
func marshalZapCallerEncoder(encoder zapcore.CallerEncoder) string {
switch reflect.ValueOf(encoder).Pointer() {
case reflect.ValueOf(zapcore.FullCallerEncoder).Pointer():
return "full"
default:
return "short"
}
}
// Marshal zapcore.DurationEncoder
func marshalZapDurationEncoder(encoder zapcore.DurationEncoder) string {
switch reflect.ValueOf(encoder).Pointer() {
case reflect.ValueOf(zapcore.StringDurationEncoder).Pointer():
return "string"
case reflect.ValueOf(zapcore.NanosDurationEncoder).Pointer():
return "nanos"
case reflect.ValueOf(zapcore.MillisDurationEncoder).Pointer():
return "ms"
default:
return "secs"
}
}
// Marshal zapcore.TimeEncoder
func marshalZapTimeEncoder(encoder zapcore.TimeEncoder) string {
switch reflect.ValueOf(encoder).Pointer() {
case reflect.ValueOf(zapcore.RFC3339NanoTimeEncoder).Pointer():
return "RFC3339Nano"
case reflect.ValueOf(zapcore.RFC3339TimeEncoder).Pointer():
return "RFC3339"
case reflect.ValueOf(zapcore.ISO8601TimeEncoder).Pointer():
return "ISO8601"
case reflect.ValueOf(zapcore.EpochMillisTimeEncoder).Pointer():
return "millis"
case reflect.ValueOf(zapcore.EpochNanosTimeEncoder).Pointer():
return "nanos"
default:
return "seconds"
}
}
// Marshal zapcore.LevelEncoder
func marshalZapLevelEncoder(encoder zapcore.LevelEncoder) string {
switch reflect.ValueOf(encoder).Pointer() {
case reflect.ValueOf(zapcore.CapitalLevelEncoder).Pointer():
return "capital"
case reflect.ValueOf(zapcore.CapitalColorLevelEncoder).Pointer():
return "capitalColor"
case reflect.ValueOf(zapcore.LowercaseColorLevelEncoder).Pointer():
return "color"
default:
return "lower"
}
}
// ZapConfigWrap wraps zap config which copied from zap.Config
// This is used while parsing zap yaml config to zap.Config with viper
// because Level would throw an error since it is not a type of string
type ZapConfigWrap struct {
// Level is the minimum enabled logging level. Note that this is a dynamic
// level, so calling Config.Level.SetLevel will atomically change the log
// level of all loggers descended from this config.
Level string `json:"level" yaml:"level"`
// Development puts the logger in development mode, which changes the
// behavior of DPanicLevel and takes stacktraces more liberally.
Development bool `json:"development" yaml:"development"`
// DisableCaller stops annotating logs with the calling function's file
// name and line number. By default, all logs are annotated.
DisableCaller bool `json:"disableCaller" yaml:"disableCaller"`
// DisableStacktrace completely disables automatic stacktrace capturing. By
// default, stacktraces are captured for WarnLevel and above logs in
// development and ErrorLevel and above in production.
DisableStacktrace bool `json:"disableStacktrace" yaml:"disableStacktrace"`
// Sampling sets a sampling policy. A nil SamplingConfig disables sampling.
Sampling *zap.SamplingConfig `json:"sampling" yaml:"sampling"`
// Encoding sets the logger's encoding. Valid values are "json" and
// "console", as well as any third-party encodings registered via
// RegisterEncoder.
Encoding string `json:"encoding" yaml:"encoding"`
// EncoderConfig sets options for the chosen encoder. See
// zapcore.EncoderConfig for details.
EncoderConfig zapcore.EncoderConfig `json:"encoderConfig" yaml:"encoderConfig"`
// OutputPaths is a list of URLs or file paths to write logging output to.
// See Open for details.
OutputPaths []string `json:"outputPaths" yaml:"outputPaths"`
// ErrorOutputPaths is a list of URLs to write internal logger errors to.
// The default is standard error.
//
// Note that this setting only affects internal errors; for sample code that
// sends error-level logs to a different location from info- and debug-level
// logs, see the package-level AdvancedConfiguration example.
ErrorOutputPaths []string `json:"errorOutputPaths" yaml:"errorOutputPaths"`
// InitialFields is a collection of fields to add to the root logger.
InitialFields map[string]interface{} `json:"initialFields" yaml:"initialFields"`
}
// MarshalJSON marshals ZapConfigWrap
func (wrap *ZapConfigWrap) MarshalJSON() ([]byte, error) {
encoderWrap := &ZapEncoderConfigWrap{
MessageKey: wrap.EncoderConfig.MessageKey,
LevelKey: wrap.EncoderConfig.LevelKey,
TimeKey: wrap.EncoderConfig.TimeKey,
NameKey: wrap.EncoderConfig.NameKey,
CallerKey: wrap.EncoderConfig.CallerKey,
FunctionKey: wrap.EncoderConfig.FunctionKey,
StacktraceKey: wrap.EncoderConfig.StacktraceKey,
LineEnding: wrap.EncoderConfig.LineEnding,
EncodeLevel: marshalZapLevelEncoder(wrap.EncoderConfig.EncodeLevel),
EncodeTime: marshalZapTimeEncoder(wrap.EncoderConfig.EncodeTime),
EncodeDuration: marshalZapDurationEncoder(wrap.EncoderConfig.EncodeDuration),
EncodeCaller: marshalZapCallerEncoder(wrap.EncoderConfig.EncodeCaller),
EncodeName: marshalZapNameEncoder(wrap.EncoderConfig.EncodeName),
ConsoleSeparator: wrap.EncoderConfig.ConsoleSeparator,
}
// Create an inner config since zap.EncoderConfig would throw an error while marshalling
type innerZapConfig struct {
Level string `json:"level" yaml:"level"`
Development bool `json:"development" yaml:"development"`
DisableCaller bool `json:"disableCaller" yaml:"disableCaller"`
DisableStacktrace bool `json:"disableStacktrace" yaml:"disableStacktrace"`
Sampling *zap.SamplingConfig `json:"sampling" yaml:"sampling"`
Encoding string `json:"encoding" yaml:"encoding"`
EncoderConfig *ZapEncoderConfigWrap `json:"encoderConfig" yaml:"encoderConfig"`
OutputPaths []string `json:"outputPaths" yaml:"outputPaths"`
ErrorOutputPaths []string `json:"errorOutputPaths" yaml:"errorOutputPaths"`
InitialFields map[string]interface{} `json:"initialFields" yaml:"initialFields"`
}
return json.Marshal(&innerZapConfig{
Level: wrap.Level,
Development: wrap.Development,
DisableCaller: wrap.DisableCaller,
DisableStacktrace: wrap.DisableStacktrace,
Sampling: wrap.Sampling,
Encoding: wrap.Encoding,
EncoderConfig: encoderWrap,
OutputPaths: wrap.OutputPaths,
ErrorOutputPaths: wrap.ErrorOutputPaths,
InitialFields: wrap.InitialFields,
})
}
// UnmarshalJSON unmarshal ZapConfigWrap
func (wrap *ZapConfigWrap) UnmarshalJSON([]byte) error {
return nil
}
// ZapEncoderConfigWrap wraps zap EncoderConfig which copied from zapcore.EncoderConfig
// This is used while parsing zap yaml config to zapcore.EncoderConfig with viper
// because Level would throw an error since it is not a type of string
type ZapEncoderConfigWrap struct {
MessageKey string `json:"messageKey" yaml:"messageKey"`
LevelKey string `json:"levelKey" yaml:"levelKey"`
TimeKey string `json:"timeKey" yaml:"timeKey"`
NameKey string `json:"nameKey" yaml:"nameKey"`
CallerKey string `json:"callerKey" yaml:"callerKey"`
FunctionKey string `json:"functionKey" yaml:"functionKey"`
StacktraceKey string `json:"stacktraceKey" yaml:"stacktraceKey"`
LineEnding string `json:"lineEnding" yaml:"lineEnding"`
EncodeLevel string `json:"levelEncoder" yaml:"levelEncoder"`
EncodeTime string `json:"timeEncoder" yaml:"timeEncoder"`
EncodeDuration string `json:"durationEncoder" yaml:"durationEncoder"`
EncodeCaller string `json:"callerEncoder" yaml:"callerEncoder"`
EncodeName string `json:"nameEncoder" yaml:"nameEncoder"`
ConsoleSeparator string `json:"consoleSeparator" yaml:"consoleSeparator"`
}
// Make incoming paths to absolute path with current working directory attached as prefix
func toAbsPath(p ...string) []string {
res := make([]string, 0)
for i := range p {
newPath := p[i]
if !path.IsAbs(p[i]) {
wd, _ := os.Getwd()
newPath = path.Join(wd, p[i])
}
res = append(res, newPath)
}
return res
}