-
Notifications
You must be signed in to change notification settings - Fork 0
/
logit.go
98 lines (79 loc) · 1.37 KB
/
logit.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
package logit
import (
"fmt"
"io"
"strings"
"time"
)
type Status int
func (s Status) String() string {
switch s {
case 1:
return " (WARNING)"
case 2:
return " *DEBUG*"
case 3:
return " -Error-"
case 4:
return " !Panic!"
default:
return ""
}
}
const (
MSG = iota
WARN
DEBUG
ERROR
PANIC
)
//Logger Logic
type Logger struct {
TimeFormat string
file io.WriteCloser
log chan msg
closure chan bool
}
type msg struct {
lvl Status
s string
}
func Start(f io.WriteCloser) (*Logger, error) {
l := new(Logger)
l.file = f
l.log = make(chan msg)
l.closure = make(chan bool)
l.TimeFormat = "[2006/01/02 15:04:05.999999]"
go logger(l)
return l, nil
}
func logger(l *Logger) {
defer l.file.Close()
defer close(l.log)
defer close(l.closure)
loop:
for {
select {
case log := <-l.log:
l.file.Write(genString(log, time.Now().Format(l.TimeFormat)))
case <-l.closure:
break loop
}
}
}
func genString(s msg, t string) []byte {
return []byte(fmt.Sprintf("%s%s %s\n", t, s.lvl.String(), s.s))
}
func (l *Logger) Quit() {
l.closure <- false
<-l.closure
}
func (l *Logger) Log(e Status, a ...string) {
l.log <- msg{lvl: e, s: strings.Join(a, " ")}
}
func (l *Logger) Logf(e Status, format string, a ...interface{}) {
l.log <- msg{lvl: e, s: fmt.Sprintf(format, a...)}
}
func (l *Logger) LogError(o Status, e error) {
l.Log(o, e.Error())
}