forked from sirupsen/logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.go
67 lines (53 loc) · 1.49 KB
/
fields.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 logrus
import (
"encoding/json"
"strings"
cases "golang.org/x/text/cases"
language "golang.org/x/text/language"
structs_to_map "github.com/fatih/structs"
)
// Common global fields that will be set in every log
var GlobalFields Fields = make(Fields)
// Converts snake_case or PascalCase to camelCase
func toCamelCase(input string) string {
// Split the input string into words using underscores as separators
words := strings.Split(input, "_")
if len(words) > 0 {
// Capitalize the first letter of each word (except the first one)
for i := 1; i < len(words); i++ {
caser := cases.Title(language.Und)
words[i] = caser.String(words[i])
}
// Join the words to create the camelCase string
joinedWords := strings.Join(words, "")
return strings.ToLower(joinedWords[0:1]) + joinedWords[1:]
} else {
return ""
}
}
// Call to create empty Fields object
func NewFields_Empty() Fields {
return Fields{}
}
// Call to create Fields from any struct object, using camelCase mapping
func NewFields(anyObject interface{}) Fields {
logFields := Fields{}
for key, val := range structs_to_map.Map(anyObject) {
logFields[toCamelCase(key)] = val
}
return logFields
}
func (x *Fields) AddFields(extraFields map[string]interface{}) {
for key, val := range extraFields {
(*x)[toCamelCase(key)] = val
}
}
func (x *Fields) Json(pretty bool) string {
var bytes []byte
if pretty {
bytes, _ = json.MarshalIndent(x, "", " ")
} else {
bytes, _ = json.Marshal(x)
}
return string(bytes)
}