-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
134 lines (117 loc) · 4.24 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
package GOgger
import (
"fmt"
"io"
"os"
)
// Logger struct will hold information needed
// to log information
type Logger struct {
// the Logger must have a strict imprtance level attributed
// to its message. User subjectivity but there is a general
// ideal of what should be of great and lesser importance.
// should start at Debug/trace and increase in criticalness up to Fatal.
// --------------------------------------------------------------
// threshold -> represents the level of the logger that the user wishes
// to access.
// no need to expose this field. The internl struct of the logger of the library callers.
threshold Level
// output will implement the io.Writer so that the user can write to what ever they wish to write to
// to log the messages that they have to a network, file, HTTP, etc.
output io.Writer
}
// Debugf method-> takes no less that one argument
// the Logger struct instance now can be used by
// the Debugf method and manipulate its fields,
// as its the first argument passed to the Debugf method
func(l *Logger) Debugf(format string, args ...any) {
// This ensures that there is always a safe output stream to write to.
if l.output == nil {
l.output = os.Stdout
}
if l.threshold > LevelDebug {
return
}
_, _ = fmt.Fprintf(l.output, format+"\n", args...)
}
// Infof method-> takes no less that one argument,
// the Logger struct instance now can be used by
func(l *Logger) Infof(format string, args ...any) {
// This ensures that there is always a safe output stream to write to.
if l.output == nil {
l.output = os.Stdout
}
if l.threshold > LevelInfo {
return
}
_, _ = fmt.Fprintf(l.output, format+"\n", args...)
}
// Warnf method-> takes no less that one argument
// the Logger struct instance now can be used by
func (l *Logger) Warnf(format string, args ...any) {
// This ensures that there is always a safe output stream to write to.
if l.output == nil {
l.output = os.Stdout
}
if l.threshold > LevelWarn {
return
}
_, _ = fmt.Fprintf(l.output, format+"\n", args...)
}
// Errorf method-> takes no less that one argument
// the Logger struct instance now can be used by
func (l *Logger) Errorf(format string, args ...any) {
// This ensures that there is always a safe output stream to write to.
if l.output == nil {
l.output = os.Stdout
}
if l.threshold > LevelError {
return
}
_, _ = fmt.Fprintf(l.output, format+"\n", args...)
}
// Fatalf method-> takes no less that one argument
// the Logger struct instance now can be used by
func (l *Logger) Fatalf(format string, args ...any) {
// This ensures that there is always a safe output stream to write to.
if l.output == nil {
l.output = os.Stdout
}
if l.threshold > LevelFatal {
return
}
_, _ = fmt.Fprintf(l.output, format+"\n", args...)
}
// logf -> logs the message to the Stdout.
// this is * not * exposed.
func (l *Logger) logf(lvl Level, format string, args ...any) {
_, _ = fmt.Fprintf(l.output, format+"\n", args...)
}
// Logf logs the log.
func (l *Logger) Logf(lvl Level, format string, args ...any) {
if l.threshold > lvl {
return
}
l.logf(lvl, format , args...)
}
// New -> takes one argument with of the type Level
// and returns a pointer to a new Logger instance
// with the threshold field filled with the threshold argument
// and outout filled with the io.Writer
// the default it Stdout.
// this will correspond to the Level type.
// *** this function invocation should be the first step in creating a Logger instance ***
func New(threshold Level, opts ...Option) *Logger {
// lgr -> creates a new instance of Logger
lgr := &Logger{threshold: threshold, output: os.Stdout}
// the loop then iterates through the opts slice
// and call each of the Option functions with the Logger instance lgr.
// Each Option function modifies the lgr as needed, giving the user
// the ablility to config the logger with as many args as needed.
for _, configFunc := range opts {
configFunc(lgr)
}
return lgr
}
func main(){
}