Skip to content
This repository was archived by the owner on Oct 4, 2025. It is now read-only.

Commit 957d186

Browse files
committed
feat: Added Logger implementation
* Added Status, Separator, LogError, LogErrorFormat, LogMessage, LogMessageFormat, DefaultLogger structs, among others, including some interfaces and their methods implementations * Forked from 'github.com/pixel-plaza-dev/uru-databases-2-go-service-common'
1 parent db21028 commit 957d186

File tree

5 files changed

+404
-0
lines changed

5 files changed

+404
-0
lines changed

errors.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package go_logger
2+
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
NilLoggerError = errors.New("logger is nil")
9+
)

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/ralvarezdev/go-logger
2+
3+
go 1.23.4

logger.go

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
package go_logger
2+
3+
import (
4+
"github.com/ralvarezdev/go-logger/status"
5+
loggerstrings "github.com/ralvarezdev/go-logger/strings"
6+
"log"
7+
"strings"
8+
)
9+
10+
type (
11+
// Log interface
12+
Log interface {
13+
String() string
14+
}
15+
16+
// LogMessage struct
17+
LogMessage struct {
18+
title string
19+
details []string
20+
status status.Status
21+
format *LogMessageFormat
22+
}
23+
24+
LogMessageFormat struct {
25+
StatusSeparator loggerstrings.Separator
26+
DetailsOuterSeparator loggerstrings.Separator
27+
DetailsInnerSeparator loggerstrings.Separator
28+
}
29+
30+
// LogError struct
31+
LogError struct {
32+
title string
33+
errors []error
34+
format *LogErrorFormat
35+
}
36+
37+
LogErrorFormat struct {
38+
StatusSeparator loggerstrings.Separator
39+
ErrorsOuterSeparator loggerstrings.Separator
40+
ErrorsInnerSeparator loggerstrings.Separator
41+
}
42+
43+
// Logger is an interface for logging messages
44+
Logger interface {
45+
LogMessage(logMessage *LogMessage)
46+
LogError(logError *LogError)
47+
}
48+
49+
// LoggerFormat struct
50+
LoggerFormat struct {
51+
NameSeparator loggerstrings.Separator
52+
MessageSeparator loggerstrings.Separator
53+
}
54+
55+
// DefaultLogger is a logger that logs messages
56+
DefaultLogger struct {
57+
name string
58+
formattedName string
59+
format *LoggerFormat
60+
}
61+
)
62+
63+
// DefaultLogMessageFormat is the default log message format
64+
var DefaultLogMessageFormat = LogMessageFormat{
65+
StatusSeparator: loggerstrings.SpaceSeparator,
66+
DetailsOuterSeparator: loggerstrings.NewLineSeparator,
67+
DetailsInnerSeparator: loggerstrings.TabSeparator,
68+
}
69+
70+
// DefaultLogErrorFormat is the default log error format
71+
var DefaultLogErrorFormat = LogErrorFormat{
72+
StatusSeparator: loggerstrings.SpaceSeparator,
73+
ErrorsOuterSeparator: loggerstrings.NewLineSeparator,
74+
ErrorsInnerSeparator: loggerstrings.TabSeparator,
75+
}
76+
77+
// DefaultLoggerFormat is the default logger format
78+
var DefaultLoggerFormat = LoggerFormat{
79+
NameSeparator: loggerstrings.SpaceSeparator,
80+
MessageSeparator: loggerstrings.SpaceSeparator,
81+
}
82+
83+
// NewLogMessageFormat creates a new log message format
84+
func NewLogMessageFormat(
85+
statusSeparator, detailsOuterSeparator, detailsInnerSeparator loggerstrings.Separator,
86+
) *LogMessageFormat {
87+
return &LogMessageFormat{
88+
StatusSeparator: statusSeparator,
89+
DetailsOuterSeparator: detailsOuterSeparator,
90+
DetailsInnerSeparator: detailsInnerSeparator,
91+
}
92+
}
93+
94+
// CopyLogMessageFormat creates a copy of a log message format
95+
func CopyLogMessageFormat(format *LogMessageFormat) *LogMessageFormat {
96+
return &LogMessageFormat{
97+
StatusSeparator: format.StatusSeparator,
98+
DetailsOuterSeparator: format.DetailsOuterSeparator,
99+
DetailsInnerSeparator: format.DetailsInnerSeparator,
100+
}
101+
}
102+
103+
// NewLogErrorFormat creates a new log error format
104+
func NewLogErrorFormat(
105+
statusSeparator, errorsOuterSeparator, errorsInnerSeparator loggerstrings.Separator,
106+
) *LogErrorFormat {
107+
return &LogErrorFormat{
108+
StatusSeparator: statusSeparator,
109+
ErrorsOuterSeparator: errorsOuterSeparator,
110+
ErrorsInnerSeparator: errorsInnerSeparator,
111+
}
112+
}
113+
114+
// CopyLogErrorFormat creates a copy of a log error format
115+
func CopyLogErrorFormat(format *LogErrorFormat) *LogErrorFormat {
116+
return &LogErrorFormat{
117+
StatusSeparator: format.StatusSeparator,
118+
ErrorsOuterSeparator: format.ErrorsOuterSeparator,
119+
ErrorsInnerSeparator: format.ErrorsInnerSeparator,
120+
}
121+
}
122+
123+
// NewLoggerFormat creates a new logger format
124+
func NewLoggerFormat(
125+
nameSeparator, messageSeparator loggerstrings.Separator,
126+
) *LoggerFormat {
127+
return &LoggerFormat{
128+
NameSeparator: nameSeparator,
129+
MessageSeparator: messageSeparator,
130+
}
131+
}
132+
133+
// CopyLoggerFormat creates a copy of a logger format
134+
func CopyLoggerFormat(format *LoggerFormat) *LoggerFormat {
135+
return &LoggerFormat{
136+
NameSeparator: format.NameSeparator,
137+
MessageSeparator: format.MessageSeparator,
138+
}
139+
}
140+
141+
// NewLogMessage creates a new log message
142+
func NewLogMessage(title string, status status.Status, format *LogMessageFormat, details ...string) *LogMessage {
143+
// Check if the format is nil
144+
if format == nil {
145+
format = &DefaultLogMessageFormat
146+
}
147+
148+
return &LogMessage{title: title, status: status, details: details, format: CopyLogMessageFormat(format)}
149+
}
150+
151+
// FormatDetails gets the formatted details
152+
func (l *LogMessage) FormatDetails() string {
153+
return loggerstrings.FormatStringArray(l.format.DetailsOuterSeparator, l.format.DetailsInnerSeparator, &l.details)
154+
}
155+
156+
// String gets the string representation of a log message
157+
func (l *LogMessage) String() string {
158+
var formattedLog []string
159+
160+
// Format status
161+
if l.status != status.StatusNone {
162+
formattedLog = append(formattedLog, loggerstrings.FormatStatus(l.status, l.format.StatusSeparator))
163+
}
164+
165+
// Add title
166+
if l.title != "" {
167+
formattedLog = append(formattedLog, l.title)
168+
}
169+
170+
// Add formatted details
171+
if len(l.details) > 0 {
172+
formattedLog = append(formattedLog, l.FormatDetails())
173+
}
174+
175+
return strings.Join(formattedLog, " ")
176+
}
177+
178+
// NewLogError creates a new log error
179+
func NewLogError(title string, format *LogErrorFormat, errors ...error) *LogError {
180+
// Check if the format is nil
181+
if format == nil {
182+
format = &DefaultLogErrorFormat
183+
}
184+
185+
return &LogError{title: title, errors: errors, format: CopyLogErrorFormat(format)}
186+
}
187+
188+
// FormatErrors gets the formatted errors
189+
func (l *LogError) FormatErrors() string {
190+
return loggerstrings.FormatErrorArray(l.format.ErrorsOuterSeparator, l.format.ErrorsInnerSeparator, &l.errors)
191+
}
192+
193+
// String gets the string representation of a log error
194+
func (l *LogError) String() string {
195+
var formattedLog []string
196+
197+
// Format status
198+
formattedLog = append(formattedLog, loggerstrings.FormatStatus(status.StatusFailed, l.format.StatusSeparator))
199+
200+
// Add message
201+
if l.title != "" {
202+
formattedLog = append(formattedLog, l.title)
203+
}
204+
205+
// Add formatted errors
206+
if len(l.errors) > 0 {
207+
formattedLog = append(formattedLog, l.FormatErrors())
208+
}
209+
210+
return strings.Join(formattedLog, " ")
211+
}
212+
213+
// NewDefaultLogger creates a new logger
214+
func NewDefaultLogger(name string, format *LoggerFormat) *DefaultLogger {
215+
// Check if the format is nil
216+
if format == nil {
217+
format = &DefaultLoggerFormat
218+
}
219+
220+
return &DefaultLogger{
221+
name: name,
222+
formattedName: loggerstrings.AddBrackets(name, format.NameSeparator),
223+
format: CopyLoggerFormat(format),
224+
}
225+
}
226+
227+
// FormatLogMessage formats a log message
228+
func (d *DefaultLogger) FormatLogMessage(logMessage *LogMessage) string {
229+
// Check if the log message is nil
230+
if logMessage == nil {
231+
return d.formattedName
232+
}
233+
234+
return strings.Join([]string{d.formattedName, logMessage.String()}, string(d.format.MessageSeparator))
235+
}
236+
237+
// LogMessage logs a message
238+
func (d *DefaultLogger) LogMessage(logMessage *LogMessage) {
239+
log.Println(d.FormatLogMessage(logMessage))
240+
}
241+
242+
// FormatLogError formats a log error
243+
func (d *DefaultLogger) FormatLogError(logError *LogError) string {
244+
// Check if the log error is nil
245+
if logError == nil {
246+
return d.formattedName
247+
}
248+
249+
return strings.Join(
250+
[]string{
251+
d.formattedName,
252+
logError.String(),
253+
}, string(d.format.MessageSeparator),
254+
)
255+
}
256+
257+
// LogError logs an error
258+
func (d *DefaultLogger) LogError(logError *LogError) {
259+
log.Println(d.FormatLogError(logError))
260+
}

status/status.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package status
2+
3+
type (
4+
// Status is the status of the logger
5+
Status int
6+
)
7+
8+
const (
9+
StatusSuccess Status = iota
10+
StatusFailed
11+
StatusError
12+
StatusWarning
13+
StatusInfo
14+
StatusDebug
15+
StatusTrace
16+
StatusNone
17+
StatusUnknown
18+
)
19+
20+
// String returns the string representation of the status
21+
func (s Status) String() string {
22+
switch s {
23+
case StatusSuccess:
24+
return "Success"
25+
case StatusFailed:
26+
return "Failed"
27+
case StatusError:
28+
return "Error"
29+
case StatusWarning:
30+
return "Warning"
31+
case StatusInfo:
32+
return "Info"
33+
case StatusDebug:
34+
return "Debug"
35+
case StatusTrace:
36+
return "Trace"
37+
case StatusNone:
38+
return ""
39+
default:
40+
return "Unknown"
41+
}
42+
}

0 commit comments

Comments
 (0)