-
Notifications
You must be signed in to change notification settings - Fork 1
/
default.go
142 lines (117 loc) · 2.89 KB
/
default.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
package log
import (
"fmt"
"runtime"
"strings"
"sync"
"time"
"github.com/andrewmolyuk/pixar/exitor"
)
// Ensure defaultLogger implements ILog interface
var _ ILog = (*defaultLogger)(nil)
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorYellow = "\033[33m"
colorCyan = "\033[36m"
)
var colors = map[string]string{
"DEBUG": "", "INFO": colorCyan, "WARN": colorYellow, "ERROR": colorRed,
}
type defaultLogger struct {
secrets []string
debugMode bool
lock sync.Mutex
isColored bool
exitor exitor.IExitor
}
func (l *defaultLogger) print(level string, args ...interface{}) {
s := ""
if len(args) == 1 {
s = fmt.Sprint(args...)
} else {
f := fmt.Sprintf("%s", args[0])
s = fmt.Sprintf(f, args[1:]...)
}
if level == "DEBUG" {
skip := 0
_, file, no, ok := runtime.Caller(skip)
for ok {
if !strings.HasSuffix(file, "/log/default.go") {
file = strings.Split(file, "/")[len(strings.Split(file, "/"))-1]
s = fmt.Sprintf("(%s:%d) %s", file, no, s)
break
}
skip++
_, file, no, ok = runtime.Caller(skip)
}
}
s = fmt.Sprintf("%s [%s] %s", time.Now().Format("2006/01/02 15:04:05.000"), level, s)
for _, secret := range l.secrets {
s = strings.Replace(s, secret, "*****", -1)
}
if l.isColored {
s = fmt.Sprint(colors[level], s, colorReset)
}
l.lock.Lock()
defer l.lock.Unlock()
fmt.Println(s)
}
func (l *defaultLogger) Debug(args ...interface{}) {
if l.debugMode {
l.print("DEBUG", args...)
}
}
func (l *defaultLogger) Info(args ...interface{}) {
l.print("INFO", args...)
}
func (l *defaultLogger) Warn(args ...interface{}) {
l.print("WARN", args...)
}
func (l *defaultLogger) Error(args ...interface{}) {
l.print("ERROR", args...)
l.exitor.Exit(1)
}
// New creates a new instance of defaultLogger implementing ILog interface
func New(debugMode bool, secrets []string, isColored bool, e exitor.IExitor) ILog {
if e == nil {
e = exitor.Default()
}
return &defaultLogger{
secrets: secrets,
debugMode: debugMode,
isColored: isColored,
exitor: e,
}
}
var logger ILog
// Default creates a new instance of defaultLogger with default values that can be used as a singleton
func Default() ILog {
if logger == nil {
InitDefault(false, nil, true, exitor.Default())
}
return logger
}
// InitDefault initializes default logger with specific values
func InitDefault(debugMode bool, secrets []string, isColored bool, e exitor.IExitor) {
if e == nil {
e = exitor.Default()
}
logger = New(debugMode, secrets, isColored, e)
}
// Debug prints debug message if debug mode is enabled
func Debug(args ...interface{}) {
Default().Debug(args...)
}
// Info prints info message
func Info(args ...interface{}) {
Default().Info(args...)
}
// Warn prints warn message
func Warn(args ...interface{}) {
Default().Warn(args...)
}
// Error prints error message and exits with code 1
func Error(args ...interface{}) {
Default().Error(args...)
}