-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
executable file
·67 lines (55 loc) · 2.24 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
package config
import (
"encoding/json"
"fmt"
"github.com/PrunedNeuron/Fluoride/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// LoggerConfiguration is the logger config model
type LoggerConfiguration struct {
Level string `json:"level" yaml:"level" mapstructure:"level"`
Encoding string `json:"encoding" yaml:"encoding" mapstructure:"encoding"`
Color bool `json:"color" yaml:"color" mapstructure:"color"`
DevMode bool `json:"dev_mode" yaml:"dev_mode" mapstructure:"dev_mode"`
DisableCaller bool `json:"disable_caller" yaml:"disable_caller" mapstructure:"disable_caller"`
DisableStacktrace bool `json:"disable_stacktrace" yaml:"disable_stacktrace" mapstructure:"disable_stacktrace"`
}
// ConfigureLogger initializes and returns the configured logger
func ConfigureLogger() (*zap.Logger, error) {
loggerConfiguration := zap.NewProductionConfig()
var logLevel zapcore.Level
conf := GetConfig().Logger
if err := logLevel.Set(conf.Level); err != nil {
logger.Errorw("Could not determine logger.level", "error", err)
}
loggerConfiguration.Level.SetLevel(logLevel)
loggerConfiguration.Encoding = conf.Encoding
loggerConfiguration.Development = conf.DevMode
loggerConfiguration.DisableCaller = conf.DisableCaller
loggerConfiguration.DisableStacktrace = conf.DisableStacktrace
if conf.Color {
loggerConfiguration.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
// Use sane timestamp when logging to console
if loggerConfiguration.Encoding == "console" {
loggerConfiguration.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
}
// JSON Fields
loggerConfiguration.EncoderConfig.MessageKey = "msg"
loggerConfiguration.EncoderConfig.LevelKey = "level"
loggerConfiguration.EncoderConfig.CallerKey = "caller"
return loggerConfiguration.Build()
}
func (config *LoggerConfiguration) String() string {
json, err := json.Marshal(config)
if err != nil {
zap.S().Errorf("Failed to marshal struct into json, error: %s\n", err)
return errors.ErrMarshal.Error()
}
return fmt.Sprintf(string(json))
}
// IsEmpty checks whether the struct is empty / uninitialized / nil
func (config *LoggerConfiguration) IsEmpty() bool {
return config.Encoding == "" || config.Level == ""
}