-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
160 lines (129 loc) · 3.16 KB
/
formatter.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
package log
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/fatih/color"
)
type Format int
const (
FormatText Format = iota
FormatTextColor
FormatJson
)
const (
ColorTextLogFormat = "%s | %14s | %16s | %s %s"
TextLogFormat = "%s | %5s | %7s | %s %s"
TextPropFormat = " | %s=%v"
)
var (
blue = color.New(color.FgBlue).SprintFunc()
magenta = color.New(color.FgMagenta).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
cyan = color.New(color.FgCyan).SprintFunc()
)
type _formatter struct{}
var formatter = _formatter{}
type formatterProps struct {
message *message
format Format
}
func (_formatter) Get(props *formatterProps) (string, error) {
switch props.format {
case FormatText:
return formatter.text(props)
case FormatTextColor:
return formatter.textColor(props)
case FormatJson:
return formatter.json(props)
default:
return "", fmt.Errorf("incorrect log format: %v", props.format)
}
}
func (_formatter) text(props *formatterProps) (string, error) {
logger := props.message.super
name := logger.local.name
if len(name) > 7 {
name = name[:7]
}
msgProps := messageProps(props.message.props)
return fmt.Sprintf(
TextLogFormat,
time.Now().Format(logger.global.dateFormat),
props.message.level,
name,
props.message.text,
msgProps,
), nil
}
func messageProps(props map[string]interface{}) string {
all := strings.Builder{}
for k, v := range props {
all.WriteString(fmt.Sprintf(TextPropFormat, k, v))
}
return all.String()
}
func (_formatter) textColor(props *formatterProps) (string, error) {
logger := props.message.super
name := logger.local.name
if len(name) > 7 {
name = name[:7]
}
msgProps := messagePropsColor(props.message.props)
return fmt.Sprintf(
ColorTextLogFormat,
time.Now().Format(logger.global.dateFormat),
levelToColor(props.message.level),
blue(name),
props.message.text,
msgProps,
), nil
}
func messagePropsColor(props map[string]interface{}) string {
all := strings.Builder{}
for k, v := range props {
all.WriteString(fmt.Sprintf(TextPropFormat, k, cyan(v)))
}
return all.String()
}
func levelToColor(level Level) string {
switch level {
case Fatal:
return red(level)
case Error:
return red(level)
case Warning:
return yellow(level)
case Info:
return green(level)
case Debug:
return magenta(level)
default:
return string(level)
}
}
type JsonStruct struct {
Time string `json:"time"`
Level string `json:"level"`
Module string `json:"module"`
Message string `json:"message"`
Props map[string]interface{} `json:"props"`
}
func (_formatter) json(props *formatterProps) (string, error) {
logger := props.message.super
j := JsonStruct{
Time: time.Now().Format(logger.global.dateFormat),
Level: string(props.message.level),
Module: logger.local.name,
Message: props.message.text,
Props: props.message.props,
}
b, err := json.Marshal(j)
if err != nil {
return "", fmt.Errorf("error while formatting log message: %v", err)
}
return string(b), nil
}