-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
195 lines (170 loc) · 4.19 KB
/
logger.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package go_logger
import (
gologgerstatus "github.com/ralvarezdev/go-logger/status"
gostringsadd "github.com/ralvarezdev/go-strings/add"
gostringsaddformat "github.com/ralvarezdev/go-strings/add/format"
gostringsconvert "github.com/ralvarezdev/go-strings/convert"
gostringsseparator "github.com/ralvarezdev/go-strings/separator"
"log"
"strings"
)
var (
// HeaderSeparator is the header separator
HeaderSeparator = gostringsseparator.NewRepeatedContent(gostringsseparator.Space)
// StatusSeparator is the status separator
StatusSeparator = gostringsseparator.NewRepeatedContent(gostringsseparator.Space)
// DescriptionSeparator is the description separator
DescriptionSeparator = gostringsseparator.NewMultiline(
gostringsseparator.Space,
gostringsseparator.NewLine,
1,
)
// MessageSeparator is the message separator
MessageSeparator = gostringsseparator.Space
// AddCharactersFn is the add characters function
AddCharactersFn = gostringsadd.Brackets
)
type (
// Message struct
Message struct {
header string
subheader string
description *[]string
status gologgerstatus.Status
}
// Logger is an interface for logging messages
Logger interface {
Log(message *Message)
Info(header, subheader string, details *[]string)
Error(header, subheader string, errors *[]error)
Debug(header, subheader string, details *[]string)
Critical(header, subheader string, details *[]string)
Warning(header, subheader string, details *[]string)
}
// DefaultLogger is a logger that logs messages
DefaultLogger struct{}
)
// NewMessage creates a new message
func NewMessage(
header, subheader string,
description *[]string,
status gologgerstatus.Status,
) *Message {
return &Message{
header,
subheader,
description,
status,
}
}
// Status returns the status of a message
func (m *Message) Status() gologgerstatus.Status {
return m.status
}
// String gets the string representation of a message
func (m *Message) String() string {
var formattedMessage []string
// Add header
if m.header != "" {
formattedMessage = append(
formattedMessage,
AddCharactersFn(
HeaderSeparator,
m.header,
),
)
}
// Format status
formattedMessage = append(
formattedMessage,
m.status.Format(
StatusSeparator,
AddCharactersFn,
),
)
// Add subheader
if m.subheader != "" {
formattedMessage = append(formattedMessage, m.subheader)
}
// Add formatted description
if m.description != nil && len(*m.description) > 0 {
formattedMessage = append(
formattedMessage, gostringsaddformat.StringArray(
DescriptionSeparator,
m.description,
AddCharactersFn,
),
)
}
return strings.Join(formattedMessage, string(MessageSeparator))
}
// NewDefaultLogger creates a new logger
func NewDefaultLogger() *DefaultLogger {
return &DefaultLogger{}
}
// Log logs a message
func (d *DefaultLogger) Log(message *Message) {
log.Println(message.String())
}
// BuildAndLog builds a message and logs it
func (d *DefaultLogger) BuildAndLog(
header, subheader string,
details *[]string,
status gologgerstatus.Status,
) {
// Create a new message and log it
message := NewMessage(
header,
subheader,
details,
status,
)
d.Log(message)
}
// Info logs an info message
func (d *DefaultLogger) Info(header, subheader string, details *[]string) {
d.BuildAndLog(
header,
subheader,
details,
gologgerstatus.Info,
)
}
// Error logs an error message
func (d *DefaultLogger) Error(header, subheader string, errors *[]error) {
// Map errors to a string array
mappedErrors := gostringsconvert.ErrorArrayToStringArray(errors)
d.BuildAndLog(
header,
subheader,
mappedErrors,
gologgerstatus.Error,
)
}
// Debug logs a debug message
func (d *DefaultLogger) Debug(header, subheader string, details *[]string) {
d.BuildAndLog(
header,
subheader,
details,
gologgerstatus.Debug,
)
}
// Critical logs a critical message
func (d *DefaultLogger) Critical(header, subheader string, details *[]string) {
d.BuildAndLog(
header,
subheader,
details,
gologgerstatus.Critical,
)
}
// Warning logs a warning message
func (d *DefaultLogger) Warning(header, subheader string, details *[]string) {
d.BuildAndLog(
header,
subheader,
details,
gologgerstatus.Warning,
)
}