This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgogger.go
196 lines (154 loc) · 5.48 KB
/
gogger.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Package gogger is a console logger designed for simplicity and great features at the same time!
package gogger
import (
"fmt"
"time"
"github.com/fatih/color"
)
const (
// VerboseLevel represents the level at which Verbose logs can be displayed.
VerboseLevel int = 0
// InfoLevel represents the level at which Info logs can be displayed.
InfoLevel int = 1
// WarnLevel represents the level at which Warn logs can be displayed.
WarnLevel int = 2
// ErrorLevel represents the level at which Error logs can be displayed.
ErrorLevel int = 3
// FatalLevel represents the level at which Fatal logs can be displayed.
FatalLevel int = 4
// UKDateTimeFormat represents the time in the UK standard format.
UKDateTimeFormat string = "02-01-2006 15:04:05"
// USDateTimeFormat represents the time in the US standard format.
USDateTimeFormat string = "01-02-2006 15:04:05"
)
var (
// ProjectNameColor is the color used for the project name in logs.
ProjectNameColor = color.FgGreen
// LevelColors is a list of github.com/faith/color color values to use for each level
LevelColors = [5]color.Attribute{color.FgMagenta,color.FgCyan,color.FgYellow,color.FgRed,color.FgRed}
// LevelStrings represents the strings used in logs to represent that particular level.
LevelStrings [5]string = [5]string{"verbose","info ","warn ","error ","fatal "}
)
// GoggerLogger is the logging handler used for printing messages to the console.
type GoggerLogger struct {
ProjectName string
Level int
TimeFormat string
Colorful bool
}
// SetLevel will change the current level of the GoggerLogger.
func (g *GoggerLogger) SetLevel(level int) error {
var possibleLevels []int = []int{VerboseLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel}
for _, possibleLevel := range possibleLevels {
if possibleLevel == level {
g.Level = level
return nil
}
}
return fmt.Errorf("gogger: provided level (%d) is invalid", level)
}
func (g *GoggerLogger) fmtMsg(level int, message string) string {
if g.Colorful {
return fmt.Sprintf("%s %s [%s]: %s", time.Now().Format(g.TimeFormat), color.New(LevelColors[level]).Sprint(LevelStrings[level]), color.New(ProjectNameColor).Sprint(g.ProjectName), message)
}
return fmt.Sprintf("%s %s [%s]: %s", time.Now().Format(g.TimeFormat), LevelStrings[level], g.ProjectName, message)
}
// Verbose messages are at the lowest log level and represent very detailed information.
func (g *GoggerLogger) Verbose(message string) bool {
const requiredLevel int = VerboseLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, message))
return true
}
// Verbosef is like Verbose but with Sprintf.
func (g *GoggerLogger) Verbosef(message string, formatOptions ...interface{}) bool {
const requiredLevel int = VerboseLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, fmt.Sprintf(message, formatOptions...)))
return true
}
// Info logs are at the default logging level and represent informative information without being too specific.
func (g *GoggerLogger) Info(message string) bool {
const requiredLevel int = InfoLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, message))
return true
}
// Infof is like Info but with Sprintf.
func (g *GoggerLogger) Infof(message string, formatOptions ...interface{}) bool {
const requiredLevel int = InfoLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, fmt.Sprintf(message, formatOptions...)))
return true
}
// Warn messages at used to represent something bad happening without it being catastrophic.
func (g *GoggerLogger) Warn(message string) bool {
const requiredLevel int = WarnLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, message))
return true
}
// Warnf is like Warn but with Sprintf.
func (g *GoggerLogger) Warnf(message string, formatOptions ...interface{}) bool {
const requiredLevel int = WarnLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, fmt.Sprintf(message, formatOptions...)))
return true
}
// Error messages are used to represent an error that needs logging but was still handled.
func (g *GoggerLogger) Error(message string) bool {
const requiredLevel int = ErrorLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, message))
return true
}
// Errorf is like Error but with Sprintf.
func (g *GoggerLogger) Errorf(message string, formatOptions ...interface{}) bool {
const requiredLevel int = ErrorLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, fmt.Sprintf(message, formatOptions...)))
return true
}
// Fatal messages denote that something really bad happened and the program has shut down.
func (g *GoggerLogger) Fatal(message string) bool {
const requiredLevel int = FatalLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, message))
return true
}
// Fatalf is like Fatal but with Sprintf.
func (g *GoggerLogger) Fatalf(message string, formatOptions ...interface{}) bool {
const requiredLevel int = FatalLevel
if g.Level > requiredLevel {
return false
}
fmt.Println(g.fmtMsg(requiredLevel, fmt.Sprintf(message, formatOptions...)))
return true
}
// New initialises a new GoggerLogger.
func New(projectName string, timeFormat string, colorful bool) GoggerLogger {
return GoggerLogger{
ProjectName: projectName,
TimeFormat: timeFormat,
Level: InfoLevel,
Colorful: colorful,
}
}