-
Notifications
You must be signed in to change notification settings - Fork 6
/
logger.go
69 lines (57 loc) · 1.44 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
package gaper
import (
"log"
"os"
"github.com/fatih/color"
)
// logger use by the whole package
var logger = newLogger("gaper")
// Logger give access to external packages to use gaper logger
func Logger() *LoggerEntity {
return logger
}
// LoggerEntity used by gaper
type LoggerEntity struct {
verbose bool
logDebug *log.Logger
logInfo *log.Logger
logError *log.Logger
}
// newLogger creates a new logger
func newLogger(prefix string) *LoggerEntity {
prefix = "[" + prefix + "] "
return &LoggerEntity{
verbose: false,
logDebug: log.New(os.Stdout, prefix, 0),
logInfo: log.New(os.Stdout, color.CyanString(prefix), 0),
logError: log.New(os.Stdout, color.RedString(prefix), 0),
}
}
// Verbose toggle this logger verbosity
func (l *LoggerEntity) Verbose(verbose bool) {
l.verbose = verbose
}
// Debug logs a debug message
func (l *LoggerEntity) Debug(v ...interface{}) {
if l.verbose {
l.logDebug.Println(v...)
}
}
// Debugf logs a debug message with format
func (l *LoggerEntity) Debugf(format string, v ...interface{}) {
if l.verbose {
l.logDebug.Printf(format, v...)
}
}
// Info logs a info message
func (l *LoggerEntity) Info(v ...interface{}) {
l.logInfo.Println(v...)
}
// Error logs an error message
func (l *LoggerEntity) Error(v ...interface{}) {
l.logError.Println(v...)
}
// Errorf logs and error message with format
func (l *LoggerEntity) Errorf(format string, v ...interface{}) {
l.logError.Printf(format, v...)
}