-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
168 lines (134 loc) · 5.26 KB
/
log.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
package log
import (
"context"
"github.com/rizanw/go-log/logger"
)
// Config for Log configuration
type Config struct {
// AppName is your application name
// it will be printed as `app` in log
AppName string
// Environment is your application environment running on
// it will be printed as `env` in log
// `dev` | `development` | `local` will mark your app under development env
Environment string
// Level is minimum log level to be printed (default: DEBUG)
Level Level
// TimeFormat is for log time format (default: RFC3339)
TimeFormat string
// WithCaller toggle to print which line is calling the log (default: false)
WithCaller bool
// CallerSkip is offset number for which caller line you wants to be print (default: 0)
CallerSkip int
// WithStack is a toggle to print which stack trace error located (default: false)
WithStack bool
// StackLevel is minimum log level for zap stack trace (default: ERROR)
StackLevel *Level
// StackMarshaller, function to get and log the stack trace for zerolog (default: `zerolog/pkgerrors`)
StackMarshaller func(err error) interface{}
// MaskSensitiveData is keys of field to be masked
MaskSensitiveData []string
// SensitiveDataMasker, function to modify sensitive value into something (default: `*****`)
SensitiveDataMasker func(value string) string
// UseJSON is a toggle to format log as json (default: false)
UseJSON bool
// UseColor is a toggle to colorize your log console
// note: it only works using `zerolog` engine and under `development` environment
UseColor bool
// UseMultiWriters is a toggle to print log into log file and log console
// note: FilePath must be filled
UseMultiWriters bool
// FilePath a file path to write the log as a file
// note: if you fill the file path, your console log will be empty.
FilePath string
// Engine is logger to be used
Engine Engine
}
// SetConfig is function to customize log configuration
func SetConfig(config *Config) error {
var (
err error
newLogger logger.ILogger
configLogger logger.Config
engineLogger logger.Engine
)
if config != nil {
isDevelopment := false
if config.Environment == "development" || config.Environment == "local" || config.Environment == "dev" {
isDevelopment = true
}
errStackLevel := ErrorLevel
if config.StackLevel != nil {
errStackLevel = *config.StackLevel
}
maskSensitiveData := make(map[string]struct{})
for _, key := range config.MaskSensitiveData {
maskSensitiveData[key] = struct{}{}
}
configLogger = logger.Config{
AppName: config.AppName,
Environment: config.Environment,
IsDevelopment: isDevelopment,
TimeFormat: config.TimeFormat,
Level: config.Level,
WithCaller: config.WithCaller,
CallerSkip: config.CallerSkip,
WithStack: config.WithStack,
StackLevel: errStackLevel,
StackMarshaller: config.StackMarshaller,
UseJSON: config.UseJSON,
UseColor: config.UseColor,
SensitiveFields: maskSensitiveData,
SensitiveFieldMasker: config.SensitiveDataMasker,
UseMultiWriters: config.UseMultiWriters,
File: config.FilePath,
}
engineLogger = config.Engine
}
newLogger, err = NewLogger(configLogger, engineLogger)
if err != nil {
return err
}
rlogger = newLogger
return nil
}
// Debug prints log on debug level
func Debug(ctx context.Context, err error, metadata KV, message string) {
rlogger.Debug(buildFields(ctx, metadata), err, message)
}
// Info prints log on info level
func Info(ctx context.Context, err error, metadata KV, message string) {
rlogger.Info(buildFields(ctx, metadata), err, message)
}
// Warn prints log on warn level
func Warn(ctx context.Context, err error, metadata KV, message string) {
rlogger.Warn(buildFields(ctx, metadata), err, message)
}
// Error prints log on error level
func Error(ctx context.Context, err error, metadata KV, message string) {
rlogger.Error(buildFields(ctx, metadata), err, message)
}
// Fatal prints log on fatal level
func Fatal(ctx context.Context, err error, metadata KV, message string) {
rlogger.Fatal(buildFields(ctx, metadata), err, message)
}
// Debugf prints log on debug level like fmt.Printf
func Debugf(ctx context.Context, err error, metadata KV, formatedMsg string, args ...interface{}) {
rlogger.Debugf(buildFields(ctx, metadata), err, formatedMsg, args...)
}
// Infof prints log on info level like fmt.Printf
func Infof(ctx context.Context, err error, metadata KV, formatedMsg string, args ...interface{}) {
rlogger.Infof(buildFields(ctx, metadata), err, formatedMsg, args...)
}
// Warnf prints log on warn level like fmt.Printf
func Warnf(ctx context.Context, err error, metadata KV, formatedMsg string, args ...interface{}) {
rlogger.Warnf(buildFields(ctx, metadata), err, formatedMsg, args...)
}
// Errorf prints log on error level like fmt.printf
func Errorf(ctx context.Context, err error, metadata KV, formatedMsg string, args ...interface{}) {
rlogger.Errorf(buildFields(ctx, metadata), err, formatedMsg, args...)
}
// Fatalf prints log on fatal level like fmt.printf
func Fatalf(ctx context.Context, err error, metadata KV, formatedMsg string, args ...interface{}) {
rlogger.Fatalf(buildFields(ctx, metadata), err, formatedMsg, args...)
}