-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
63 lines (53 loc) · 1.29 KB
/
profile.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
package logger
import (
"encoding/json"
"fmt"
)
// Profile holds global logger options
type Profile struct {
LogLevelPatterns string
WithCorrelation bool
WithLoggerName bool
}
// GetCurrentProfile gets the current logger profile
func GetCurrentProfile() Profile {
return Profile{
LogLevelPatterns: GetLogLevelPattern(),
WithCorrelation: IsEnabledCorrelation(),
WithLoggerName: IsEnabledLoggerName(),
}
}
// UnmarshalProfile deserializes into a Profile object
func UnmarshalProfile(data []byte) (Profile, error) {
profile := Profile{}
err := json.Unmarshal(data, &profile)
if err != nil {
return Profile{}, err
}
return profile, nil
}
// Marshal serializes the Profile object
func (profile *Profile) Marshal() ([]byte, error) {
data, err := json.Marshal(profile)
if err != nil {
return nil, err
}
return data, nil
}
// Apply sets the global logger options
func (profile *Profile) Apply() error {
err := SetLogLevel(profile.LogLevelPatterns)
if err != nil {
return err
}
ToggleCorrelation(profile.WithCorrelation)
ToggleLoggerName(profile.WithLoggerName)
return nil
}
func (profile *Profile) String() string {
return fmt.Sprintf("[pattern=%s, with correlation=%t, with logger name=%t]",
profile.LogLevelPatterns,
profile.WithCorrelation,
profile.WithLoggerName,
)
}