-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_object_config.go
66 lines (51 loc) · 1.63 KB
/
log_object_config.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
package object_config
import (
"fmt"
"reflect"
"github.com/evgeniums/go-backend-helpers/pkg/logger"
"github.com/evgeniums/go-backend-helpers/pkg/utils"
)
func Info(log logger.Logger, obj Object, configPath ...string) {
Log(log, utils.OptionalArg("", configPath...), obj)
}
func Debug(log logger.Logger, obj Object, configPath ...string) {
Log(log, utils.OptionalArg("", configPath...), obj, logger.DebugLevel)
}
func logObject(log logger.Logger, configPath string, obj reflect.Value, logLevel ...logger.Level) {
level := utils.OptionalArg(logger.InfoLevel, logLevel...)
typ := obj.Type()
if typ.Kind() == reflect.Ptr {
typ = obj.Elem().Type()
}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
var value reflect.Value
if obj.Type().Kind() == reflect.Ptr {
value = obj.Elem().Field(i)
} else {
value = obj.Field(i)
}
if field.Type.Kind() == reflect.Struct && field.Anonymous {
logObject(log, configPath, value.Addr(), logLevel...)
} else if field.Type.Kind() != reflect.Map {
keyPath := Key(configPath, field.Name)
formatValue := func() string {
val := ""
_, ok := field.Tag.Lookup("mask")
if ok {
val = "********"
} else {
val = fmt.Sprintf("%v", value)
}
return val
}
LogParameter(log, keyPath, formatValue(), level)
}
}
}
func Log(log logger.Logger, configPath string, obj Object, logLevel ...logger.Level) {
logObject(log, configPath, reflect.ValueOf(obj.Config()), logLevel...)
}
func LogParameter(log logger.Logger, key string, value string, logLevel logger.Level) {
log.Log(logLevel, "Configuration", logger.Fields{"key": key, "value": value})
}