forked from zhuxiujia/GoMybatis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogSystem.go
57 lines (51 loc) · 1.11 KB
/
LogSystem.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
package GoMybatis
import (
"github.com/zhuxiujia/GoMybatis/utils"
"bytes"
)
type LogSystem struct {
log Log
logChan chan []byte
started bool
}
//logImpl:日志实现类,queueLen:消息队列缓冲长度
func (it LogSystem) New(logImpl Log, queueLen int) (LogSystem, error) {
if it.started == true {
return it, utils.NewError("LogSystem", "log system is started!")
}
if logImpl == nil {
logImpl = &LogStandard{}
}
it.logChan = make(chan []byte, queueLen)
it.log = logImpl
//启动接受者
go it.receiver()
it.started = true
return it, nil
}
//关闭日志系统和队列
func (it *LogSystem) Close() error {
close(it.logChan)
it.started = false
return nil
}
//日志发送者
func (it *LogSystem) SendLog(logs ...string) error {
if it.started == false {
return utils.NewError("LogSystem", "no log Receiver! you must call go GoMybatis.LogSystem{}.New()")
}
var buf bytes.Buffer
for _, v := range logs {
buf.WriteString(v)
}
it.logChan <- buf.Bytes()
return nil
}
//日志接受者
func (it *LogSystem) receiver() error {
for {
logs := <-it.logChan
it.log.Println(logs)
}
return nil
}