Skip to content

Commit 3038a94

Browse files
committed
refactor: substituted format structs by global variables
1 parent e24abf2 commit 3038a94

File tree

4 files changed

+141
-222
lines changed

4 files changed

+141
-222
lines changed

logger.go

Lines changed: 36 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
package go_logger
22

33
import (
4+
gologgerseparator "github.com/ralvarezdev/go-logger/separator"
45
gologgerstatus "github.com/ralvarezdev/go-logger/status"
56
gologgerstrings "github.com/ralvarezdev/go-logger/strings"
67
"log"
78
"strings"
89
)
910

11+
var (
12+
// StatusSeparator is the status separator
13+
StatusSeparator = gologgerseparator.NewRepeatedContent(gologgerseparator.Space)
14+
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(
24+
gologgerseparator.Space,
25+
gologgerseparator.NewLine,
26+
1,
27+
)
28+
29+
// NameSeparator is the name separator
30+
NameSeparator = gologgerseparator.NewRepeatedContent(gologgerseparator.Space)
31+
32+
// MessageSeparator is the message separator
33+
MessageSeparator = gologgerseparator.Space
34+
)
35+
1036
type (
1137
// Log interface
1238
Log interface {
@@ -18,24 +44,12 @@ type (
1844
title string
1945
details []string
2046
status gologgerstatus.Status
21-
format *LogMessageFormat
22-
}
23-
24-
LogMessageFormat struct {
25-
StatusSeparator *gologgerstrings.ContentSeparator
26-
DetailsSeparator *gologgerstrings.MultilineSeparator
2747
}
2848

2949
// LogError struct
3050
LogError struct {
3151
title string
3252
errors []error
33-
format *LogErrorFormat
34-
}
35-
36-
LogErrorFormat struct {
37-
StatusSeparator *gologgerstrings.ContentSeparator
38-
ErrorsSeparator *gologgerstrings.MultilineSeparator
3953
}
4054

4155
// Logger is an interface for logging messages
@@ -44,127 +58,30 @@ type (
4458
LogError(logError *LogError)
4559
}
4660

47-
// LoggerFormat struct
48-
LoggerFormat struct {
49-
NameSeparator *gologgerstrings.ContentSeparator
50-
MessageSeparator gologgerstrings.Separator
51-
}
52-
5361
// DefaultLogger is a logger that logs messages
5462
DefaultLogger struct {
5563
name string
5664
formattedName string
57-
format *LoggerFormat
58-
}
59-
)
60-
61-
// NewLogMessageFormat creates a new log message format
62-
func NewLogMessageFormat(
63-
statusSeparator *gologgerstrings.ContentSeparator,
64-
detailsSeparator *gologgerstrings.MultilineSeparator,
65-
) *LogMessageFormat {
66-
return &LogMessageFormat{
67-
StatusSeparator: statusSeparator,
68-
DetailsSeparator: detailsSeparator,
69-
}
70-
}
71-
72-
// CopyLogMessageFormat creates a copy of a log message format
73-
func CopyLogMessageFormat(format *LogMessageFormat) *LogMessageFormat {
74-
return &LogMessageFormat{
75-
StatusSeparator: format.StatusSeparator,
76-
DetailsSeparator: format.DetailsSeparator,
77-
}
78-
}
79-
80-
// DefaultLogMessageFormat is the default log message format
81-
var DefaultLogMessageFormat = NewLogMessageFormat(
82-
gologgerstrings.NewRepeatedContentSeparator(gologgerstrings.SpaceSeparator),
83-
gologgerstrings.NewMultilineSeparator(
84-
gologgerstrings.SpaceSeparator,
85-
gologgerstrings.NewLineSeparator,
86-
1,
87-
),
88-
)
89-
90-
// NewLogErrorFormat creates a new log error format
91-
func NewLogErrorFormat(
92-
statusSeparator *gologgerstrings.ContentSeparator,
93-
errorsSeparator *gologgerstrings.MultilineSeparator,
94-
) *LogErrorFormat {
95-
return &LogErrorFormat{
96-
StatusSeparator: statusSeparator,
97-
ErrorsSeparator: errorsSeparator,
98-
}
99-
}
100-
101-
// CopyLogErrorFormat creates a copy of a log error format
102-
func CopyLogErrorFormat(format *LogErrorFormat) *LogErrorFormat {
103-
return &LogErrorFormat{
104-
StatusSeparator: format.StatusSeparator,
105-
ErrorsSeparator: format.ErrorsSeparator,
106-
}
107-
}
108-
109-
// DefaultLogErrorFormat is the default log error format
110-
var DefaultLogErrorFormat = NewLogErrorFormat(
111-
gologgerstrings.NewRepeatedContentSeparator(gologgerstrings.SpaceSeparator),
112-
gologgerstrings.NewMultilineSeparator(
113-
gologgerstrings.SpaceSeparator,
114-
gologgerstrings.NewLineSeparator,
115-
1,
116-
),
117-
)
118-
119-
// NewLoggerFormat creates a new logger format
120-
func NewLoggerFormat(
121-
nameSeparator *gologgerstrings.ContentSeparator,
122-
messageSeparator gologgerstrings.Separator,
123-
) *LoggerFormat {
124-
return &LoggerFormat{
125-
NameSeparator: nameSeparator,
126-
MessageSeparator: messageSeparator,
127-
}
128-
}
129-
130-
// CopyLoggerFormat creates a copy of a logger format
131-
func CopyLoggerFormat(format *LoggerFormat) *LoggerFormat {
132-
return &LoggerFormat{
133-
NameSeparator: format.NameSeparator,
134-
MessageSeparator: format.MessageSeparator,
13565
}
136-
}
137-
138-
// DefaultLoggerFormat is the default logger format
139-
var DefaultLoggerFormat = NewLoggerFormat(
140-
gologgerstrings.NewRepeatedContentSeparator(gologgerstrings.SpaceSeparator),
141-
gologgerstrings.SpaceSeparator,
14266
)
14367

14468
// NewLogMessage creates a new log message
14569
func NewLogMessage(
14670
title string,
14771
status gologgerstatus.Status,
148-
format *LogMessageFormat,
14972
details ...string,
15073
) *LogMessage {
151-
// Check if the format is nil
152-
if format == nil {
153-
format = DefaultLogMessageFormat
154-
}
155-
15674
return &LogMessage{
15775
title: title,
15876
status: status,
15977
details: details,
160-
format: CopyLogMessageFormat(format),
16178
}
16279
}
16380

16481
// FormatDetails gets the formatted details
16582
func (l *LogMessage) FormatDetails() string {
16683
return gologgerstrings.FormatStringArray(
167-
l.format.DetailsSeparator,
84+
DetailsSeparator,
16885
&l.details,
16986
)
17087
}
@@ -174,10 +91,10 @@ func (l *LogMessage) String() string {
17491
var formattedLog []string
17592

17693
// Format status
177-
if l.status != gologgerstatus.StatusNone {
94+
if l.status != gologgerstatus.None {
17895
formattedLog = append(
17996
formattedLog,
180-
gologgerstrings.FormatStatus(l.status, l.format.StatusSeparator),
97+
gologgerstrings.FormatStatus(StatusSeparator, l.status),
18198
)
18299
}
183100

@@ -197,24 +114,17 @@ func (l *LogMessage) String() string {
197114
// NewLogError creates a new log error
198115
func NewLogError(
199116
title string,
200-
format *LogErrorFormat,
201117
errors ...error,
202118
) *LogError {
203-
// Check if the format is nil
204-
if format == nil {
205-
format = DefaultLogErrorFormat
206-
}
207-
208119
return &LogError{
209120
title: title,
210121
errors: errors,
211-
format: CopyLogErrorFormat(format),
212122
}
213123
}
214124

215125
// FormatErrors gets the formatted errors
216126
func (l *LogError) FormatErrors() string {
217-
return gologgerstrings.FormatErrorArray(l.format.ErrorsSeparator, &l.errors)
127+
return gologgerstrings.FormatErrorArray(ErrorsSeparator, &l.errors)
218128
}
219129

220130
// String gets the string representation of a log error
@@ -225,8 +135,8 @@ func (l *LogError) String() string {
225135
formattedLog = append(
226136
formattedLog,
227137
gologgerstrings.FormatStatus(
228-
gologgerstatus.StatusFailed,
229-
l.format.StatusSeparator,
138+
StatusSeparator,
139+
gologgerstatus.Failed,
230140
),
231141
)
232142

@@ -244,16 +154,10 @@ func (l *LogError) String() string {
244154
}
245155

246156
// NewDefaultLogger creates a new logger
247-
func NewDefaultLogger(name string, format *LoggerFormat) *DefaultLogger {
248-
// Check if the format is nil
249-
if format == nil {
250-
format = DefaultLoggerFormat
251-
}
252-
157+
func NewDefaultLogger(name string) *DefaultLogger {
253158
return &DefaultLogger{
254159
name: name,
255-
formattedName: gologgerstrings.AddBrackets(name, format.NameSeparator),
256-
format: CopyLoggerFormat(format),
160+
formattedName: gologgerstrings.AddBrackets(NameSeparator, name),
257161
}
258162
}
259163

@@ -266,7 +170,7 @@ func (d *DefaultLogger) FormatLogMessage(logMessage *LogMessage) string {
266170

267171
return strings.Join(
268172
[]string{d.formattedName, logMessage.String()},
269-
string(d.format.MessageSeparator),
173+
string(MessageSeparator),
270174
)
271175
}
272176

@@ -286,7 +190,7 @@ func (d *DefaultLogger) FormatLogError(logError *LogError) string {
286190
[]string{
287191
d.formattedName,
288192
logError.String(),
289-
}, string(d.format.MessageSeparator),
193+
}, string(MessageSeparator),
290194
)
291195
}
292196

separator/separator.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package separator
2+
3+
import (
4+
"strings"
5+
)
6+
7+
type (
8+
// Separator is the separator for a string
9+
Separator string
10+
11+
// Content is the separator for the content
12+
Content struct {
13+
Left Separator
14+
Right Separator
15+
}
16+
17+
// Multiline is the separator for the multiline content
18+
Multiline struct {
19+
SingleLine Separator
20+
Line Separator
21+
TabSize int
22+
}
23+
)
24+
25+
// Separator constants
26+
const (
27+
Space Separator = " "
28+
Comma Separator = ","
29+
NewLine Separator = "\n"
30+
Tab Separator = "\t"
31+
)
32+
33+
// NewContent creates a new content separator
34+
func NewContent(left, right Separator) *Content {
35+
return &Content{
36+
Left: left,
37+
Right: right,
38+
}
39+
}
40+
41+
// NewRepeatedContent creates a new content separator with the same separator
42+
func NewRepeatedContent(separator Separator) *Content {
43+
return NewContent(separator, separator)
44+
}
45+
46+
// NewMultiline creates a new multiline separator
47+
func NewMultiline(
48+
singleLine, line Separator,
49+
tabSize int,
50+
) *Multiline {
51+
return &Multiline{
52+
SingleLine: singleLine,
53+
Line: line,
54+
TabSize: tabSize,
55+
}
56+
}
57+
58+
// Tab returns a tab separator
59+
func (m *Multiline) Tab() Separator {
60+
return Separator(strings.Repeat(string(Tab), m.TabSize))
61+
}

status/status.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@ type (
66
)
77

88
const (
9-
StatusSuccess Status = iota
10-
StatusFailed
11-
StatusError
12-
StatusWarning
13-
StatusInfo
14-
StatusDebug
15-
StatusTrace
16-
StatusNone
17-
StatusUnknown
9+
Success Status = iota
10+
Failed
11+
Error
12+
Warning
13+
Info
14+
Debug
15+
Trace
16+
None
17+
Unknown
1818
)
1919

2020
// String returns the string representation of the status
2121
func (s Status) String() string {
2222
switch s {
23-
case StatusSuccess:
23+
case Success:
2424
return "SUCCESS"
25-
case StatusFailed:
25+
case Failed:
2626
return "FAILED"
27-
case StatusError:
27+
case Error:
2828
return "ERROR"
29-
case StatusWarning:
29+
case Warning:
3030
return "WARNING"
31-
case StatusInfo:
31+
case Info:
3232
return "INFO"
33-
case StatusDebug:
33+
case Debug:
3434
return "DEBUG"
35-
case StatusTrace:
35+
case Trace:
3636
return "TRACE"
37-
case StatusNone:
37+
case None:
3838
return ""
3939
default:
4040
return "UNKNOWN"

0 commit comments

Comments
 (0)