Skip to content

Commit e78655b

Browse files
committed
refactor: improved Logger interface and its implementation
* Improved Logger interface and its implementation * Removed some Logger status * Added addCharactersFn parameter to the AddCharacters-related functions
1 parent 9bc319b commit e78655b

File tree

3 files changed

+164
-154
lines changed

3 files changed

+164
-154
lines changed

logger.go

Lines changed: 122 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -9,189 +9,182 @@ import (
99
)
1010

1111
var (
12+
// HeaderSeparator is the header separator
13+
HeaderSeparator = gologgerseparator.NewRepeatedContent(gologgerseparator.Space)
14+
1215
// StatusSeparator is the status separator
1316
StatusSeparator = gologgerseparator.NewRepeatedContent(gologgerseparator.Space)
1417

15-
// ErrorsSeparator is the errors separator
16-
ErrorsSeparator = gologgerseparator.NewMultiline(
17-
gologgerseparator.Space,
18-
gologgerseparator.NewLine,
19-
1,
20-
)
21-
22-
// DetailsSeparator is the details separator
23-
DetailsSeparator = gologgerseparator.NewMultiline(
18+
// DescriptionSeparator is the description separator
19+
DescriptionSeparator = gologgerseparator.NewMultiline(
2420
gologgerseparator.Space,
2521
gologgerseparator.NewLine,
2622
1,
2723
)
2824

29-
// NameSeparator is the name separator
30-
NameSeparator = gologgerseparator.NewRepeatedContent(gologgerseparator.Space)
31-
3225
// MessageSeparator is the message separator
3326
MessageSeparator = gologgerseparator.Space
27+
28+
// AddCharactersFn is the add characters function
29+
AddCharactersFn = gologgerstrings.AddBrackets
3430
)
3531

3632
type (
37-
// Log interface
38-
Log interface {
39-
String() string
40-
}
41-
42-
// LogMessage struct
43-
LogMessage struct {
44-
subheader string
45-
details []string
46-
status gologgerstatus.Status
47-
}
48-
49-
// LogError struct
50-
LogError struct {
51-
subheader string
52-
errors []error
33+
// Message struct
34+
Message struct {
35+
header string
36+
subheader string
37+
description *[]string
38+
status gologgerstatus.Status
5339
}
5440

5541
// Logger is an interface for logging messages
5642
Logger interface {
57-
LogMessage(header string, logMessage *LogMessage)
58-
LogError(header string, logError *LogError)
43+
Log(message *Message)
44+
Info(header, subheader string, details ...string)
45+
Error(header, subheader string, errors ...error)
46+
Debug(header, subheader string, details ...string)
47+
Critical(header, subheader string, details ...string)
48+
Warning(header, subheader string, details ...string)
5949
}
6050

6151
// DefaultLogger is a logger that logs messages
6252
DefaultLogger struct{}
63-
64-
// DefaultSubLogger is a logger that logs messages with a name
65-
DefaultSubLogger struct {
66-
name string
67-
formattedName string
68-
logger Logger
69-
}
7053
)
7154

72-
// NewLogMessage creates a new log message
73-
func NewLogMessage(
74-
subheader string,
55+
// NewMessage creates a new message
56+
func NewMessage(
57+
header, subheader string,
7558
status gologgerstatus.Status,
76-
details ...string,
77-
) *LogMessage {
78-
return &LogMessage{
79-
subheader: subheader,
80-
status: status,
81-
details: details,
59+
description *[]string,
60+
) *Message {
61+
return &Message{
62+
header: header,
63+
subheader: subheader,
64+
status: status,
65+
description: description,
8266
}
8367
}
8468

85-
// FormatDetails gets the formatted details
86-
func (l *LogMessage) FormatDetails() string {
87-
return gologgerstrings.FormatStringArray(
88-
DetailsSeparator,
89-
&l.details,
90-
)
91-
}
92-
93-
// String gets the string representation of a log message
94-
func (l *LogMessage) String() string {
95-
var formattedLog []string
96-
97-
// Format status
98-
if l.status != gologgerstatus.None {
99-
formattedLog = append(
100-
formattedLog,
101-
gologgerstrings.FormatStatus(StatusSeparator, l.status),
69+
// String gets the string representation of a message
70+
func (m *Message) String() string {
71+
var formattedMessage []string
72+
73+
// Add header
74+
if m.header != "" {
75+
formattedMessage = append(
76+
formattedMessage,
77+
gologgerstrings.FormatString(
78+
HeaderSeparator,
79+
m.header,
80+
AddCharactersFn,
81+
),
10282
)
10383
}
10484

105-
// Add subheader
106-
if l.subheader != "" {
107-
formattedLog = append(formattedLog, l.subheader)
108-
}
109-
110-
// Add formatted details
111-
if len(l.details) > 0 {
112-
formattedLog = append(formattedLog, l.FormatDetails())
113-
}
114-
115-
return strings.Join(formattedLog, " ")
116-
}
117-
118-
// NewLogError creates a new log error
119-
func NewLogError(
120-
subheader string,
121-
errors ...error,
122-
) *LogError {
123-
return &LogError{
124-
subheader: subheader,
125-
errors: errors,
126-
}
127-
}
128-
129-
// FormatErrors gets the formatted errors
130-
func (l *LogError) FormatErrors() string {
131-
return gologgerstrings.FormatErrorArray(ErrorsSeparator, &l.errors)
132-
}
133-
134-
// String gets the string representation of a log error
135-
func (l *LogError) String() string {
136-
var formattedLog []string
137-
13885
// Format status
139-
formattedLog = append(
140-
formattedLog,
86+
formattedMessage = append(
87+
formattedMessage,
14188
gologgerstrings.FormatStatus(
14289
StatusSeparator,
143-
gologgerstatus.Failed,
90+
m.status,
91+
AddCharactersFn,
14492
),
14593
)
14694

14795
// Add subheader
148-
if l.subheader != "" {
149-
formattedLog = append(formattedLog, l.subheader)
96+
if m.subheader != "" {
97+
formattedMessage = append(formattedMessage, m.subheader)
15098
}
15199

152-
// Add formatted errors
153-
if len(l.errors) > 0 {
154-
formattedLog = append(formattedLog, l.FormatErrors())
100+
// Add formatted description
101+
if m.description != nil && len(*m.description) > 0 {
102+
formattedMessage = append(
103+
formattedMessage, gologgerstrings.FormatStringArray(
104+
DescriptionSeparator,
105+
m.description,
106+
AddCharactersFn,
107+
),
108+
)
155109
}
156110

157-
return strings.Join(formattedLog, " ")
111+
return strings.Join(formattedMessage, string(MessageSeparator))
158112
}
159113

160114
// NewDefaultLogger creates a new logger
161115
func NewDefaultLogger() *DefaultLogger {
162116
return &DefaultLogger{}
163117
}
164118

165-
// FormatLogMessage formats a log message
166-
func (d *DefaultLogger) FormatLogMessage(
167-
header string,
168-
logMessage *LogMessage,
169-
) string {
170-
return strings.Join(
171-
[]string{header, logMessage.String()},
172-
string(MessageSeparator),
119+
// Log logs a message
120+
func (d *DefaultLogger) Log(message *Message) {
121+
log.Println(message.String())
122+
}
123+
124+
// BuildAndLog builds a message and logs it
125+
func (d *DefaultLogger) BuildAndLog(
126+
header, subheader string,
127+
details *[]string,
128+
status gologgerstatus.Status,
129+
) {
130+
// Create a new message and log it
131+
message := NewMessage(
132+
header,
133+
subheader,
134+
status,
135+
details,
173136
)
137+
d.Log(message)
174138
}
175139

176-
// LogMessage logs a message
177-
func (d *DefaultLogger) LogMessage(header string, logMessage *LogMessage) {
178-
log.Println(d.FormatLogMessage(header, logMessage))
140+
// Info logs an info message
141+
func (d *DefaultLogger) Info(header, subheader string, details ...string) {
142+
d.BuildAndLog(
143+
header,
144+
subheader,
145+
&details,
146+
gologgerstatus.Info,
147+
)
179148
}
180149

181-
// FormatLogError formats a log error
182-
func (d *DefaultLogger) FormatLogError(
183-
header string,
184-
logError *LogError,
185-
) string {
186-
return strings.Join(
187-
[]string{
188-
header,
189-
logError.String(),
190-
}, string(MessageSeparator),
150+
// Error logs an error message
151+
func (d *DefaultLogger) Error(header, subheader string, errors ...error) {
152+
// Map errors to a string array
153+
mappedErrors := gologgerstrings.MapErrorArrayToStringArray(&errors)
154+
d.BuildAndLog(
155+
header,
156+
subheader,
157+
mappedErrors,
158+
gologgerstatus.Error,
191159
)
192160
}
193161

194-
// LogError logs an error
195-
func (d *DefaultLogger) LogError(header string, logError *LogError) {
196-
log.Println(d.FormatLogError(header, logError))
162+
// Debug logs a debug message
163+
func (d *DefaultLogger) Debug(header, subheader string, details ...string) {
164+
d.BuildAndLog(
165+
header,
166+
subheader,
167+
&details,
168+
gologgerstatus.Debug,
169+
)
170+
}
171+
172+
// Critical logs a critical message
173+
func (d *DefaultLogger) Critical(header, subheader string, details ...string) {
174+
d.BuildAndLog(
175+
header,
176+
subheader,
177+
&details,
178+
gologgerstatus.Critical,
179+
)
180+
}
181+
182+
// Warning logs a warning message
183+
func (d *DefaultLogger) Warning(header, subheader string, details ...string) {
184+
d.BuildAndLog(
185+
header,
186+
subheader,
187+
&details,
188+
gologgerstatus.Warning,
189+
)
197190
}

status/status.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,26 @@ type (
66
)
77

88
const (
9-
Success Status = iota
10-
Failed
11-
Error
12-
Warning
13-
Info
9+
Info Status = iota
1410
Debug
15-
Trace
16-
None
17-
Unknown
11+
Warning
12+
Error
13+
Critical
1814
)
1915

2016
// String returns the string representation of the status
2117
func (s Status) String() string {
2218
switch s {
23-
case Success:
24-
return "SUCCESS"
25-
case Failed:
26-
return "FAILED"
27-
case Error:
28-
return "ERROR"
29-
case Warning:
30-
return "WARNING"
3119
case Info:
3220
return "INFO"
3321
case Debug:
3422
return "DEBUG"
35-
case Trace:
36-
return "TRACE"
37-
case None:
38-
return ""
23+
case Warning:
24+
return "WARNING"
25+
case Error:
26+
return "ERROR"
27+
case Critical:
28+
return "CRITICAL"
3929
default:
4030
return "UNKNOWN"
4131
}

0 commit comments

Comments
 (0)