forked from eth0izzle/shhgit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
executable file
·88 lines (70 loc) · 1.73 KB
/
log.go
File metadata and controls
executable file
·88 lines (70 loc) · 1.73 KB
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
package core
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"sync"
"github.com/fatih/color"
)
const (
FATAL = 5
ERROR = 4
WARN = 3
IMPORTANT = 2
INFO = 1
DEBUG = 0
)
var LogColors = map[int]*color.Color{
FATAL: color.New(color.FgRed).Add(color.Bold),
ERROR: color.New(color.FgRed),
WARN: color.New(color.FgYellow),
IMPORTANT: color.New(),
DEBUG: color.New(color.Faint),
}
type Logger struct {
sync.Mutex
debug bool
}
func (l *Logger) SetDebug(d bool) {
l.debug = d
}
func (l *Logger) Log(level int, format string, args ...interface{}) {
l.Lock()
defer l.Unlock()
if level == DEBUG && !l.debug {
return
}
if c, ok := LogColors[level]; ok {
c.Printf(format+"\n", args...)
} else {
fmt.Printf(format+"\n", args...)
}
if level > INFO && session.Config.SlackWebhook != "" {
values := map[string]string{"text": fmt.Sprintf(format+"\n", args...)}
jsonValue, _ := json.Marshal(values)
http.Post(session.Config.SlackWebhook, "application/json", bytes.NewBuffer(jsonValue))
}
if level == FATAL {
os.Exit(1)
}
}
func (l *Logger) Fatal(format string, args ...interface{}) {
l.Log(FATAL, format, args...)
}
func (l *Logger) Error(format string, args ...interface{}) {
l.Log(ERROR, format, args...)
}
func (l *Logger) Warn(format string, args ...interface{}) {
l.Log(WARN, format, args...)
}
func (l *Logger) Important(format string, args ...interface{}) {
l.Log(IMPORTANT, format, args...)
}
func (l *Logger) Info(format string, args ...interface{}) {
l.Log(INFO, format, args...)
}
func (l *Logger) Debug(format string, args ...interface{}) {
l.Log(DEBUG, format, args...)
}