forked from cyinnove/logify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
97 lines (73 loc) · 2.55 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
package logify
import (
"fmt"
"sync"
)
// Logger interface defines the methods that any logger implementation should have.
type Logger interface {
Info(msg string, args ...interface{})
Debug(msg string, args ...interface{})
Test(msg string, args ...interface{})
Warn(msg string, args ...interface{})
Error(msg string, args ...interface{})
Fatal(msg string, args ...interface{})
}
// LoggerOptions struct represents the default logger implementation.
type LoggerOptions struct {
Formatter Formatter
}
var (
once sync.Once
instance *LoggerOptions
)
// GetLogger returns the singleton instance of LoggerOptions.
// Msg returns an instance of the Logger.
// The Logger is a singleton object that provides logging functionality.
// It initializes the LoggerOptions with default values if it hasn't been initialized before.
// The LoggerOptions controls various logging options such as formatter, color enablement, and log level visibility.
func Msg() Logger {
once.Do(func() {
instance = &LoggerOptions{
Formatter: Formatter{},
}
})
return instance
}
func (l *LoggerOptions) colorize(holder, color string) {
l.Formatter.SetHolder(fmt.Sprintf("%s%s%s", color, holder, Colors[Reset]))
}
// Info logs an info message with the specified message and arguments.
func (l *LoggerOptions) Info(msg string, args ...interface{}) {
l.colorize(Info.String(), Colors[Blue])
l.Formatter.SetMessage(msg, args...)
l.Formatter.Log()
}
// Debug logs a debug message with the specified message and arguments.
func (l *LoggerOptions) Debug(msg string, args ...interface{}) {
l.colorize(Debug.String(), Colors[Purple])
l.Formatter.SetMessage(msg, args...)
l.Formatter.Log()
}
// Warn logs a warning message with the specified message and arguments.
func (l *LoggerOptions) Warn(msg string, args ...interface{}) {
l.colorize(Warn.String(), Colors[Yellow])
l.Formatter.SetMessage(msg, args...)
l.Formatter.Log()
}
// Error logs an error message with the specified message and arguments.
func (l *LoggerOptions) Error(msg string, args ...interface{}) {
l.colorize(Error.String(), Colors[Red])
l.Formatter.SetMessage(msg, args...)
l.Formatter.Log()
}
// Fatal logs a fatal message with the specified message and arguments.
func (l *LoggerOptions) Fatal(msg string, args ...interface{}) {
l.colorize(Fatal.String(), Colors[Orange])
l.Formatter.SetMessage(msg, args...)
l.Formatter.Log()
}
func (l *LoggerOptions) Test(msg string, args ...interface{}) {
l.colorize(Test.String(), Colors[Green])
l.Formatter.SetMessage(msg, args...)
l.Formatter.Log()
}