|
|
1
|
+package log
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "encoding/json"
|
|
|
5
|
+ "errors"
|
|
|
6
|
+ "fmt"
|
|
|
7
|
+ "github.com/beego/beego/v2/core/logs"
|
|
|
8
|
+ "io"
|
|
|
9
|
+ "sync/atomic"
|
|
|
10
|
+)
|
|
|
11
|
+
|
|
|
12
|
+var errOutOfMaxSize = errors.New("msg size is out of limit ")
|
|
|
13
|
+
|
|
|
14
|
+type BaseHook struct {
|
|
|
15
|
+ syncWrite bool
|
|
|
16
|
+ msgChan chan []byte
|
|
|
17
|
+ maxSize int32
|
|
|
18
|
+ currentSize int32
|
|
|
19
|
+ closeChan chan struct{}
|
|
|
20
|
+ w io.Writer
|
|
|
21
|
+}
|
|
|
22
|
+
|
|
|
23
|
+func (b *BaseHook) Write(p []byte) (n int, err error) {
|
|
|
24
|
+ if b.syncWrite {
|
|
|
25
|
+ return b.w.Write(p)
|
|
|
26
|
+ }
|
|
|
27
|
+ if b.currentSize >= b.maxSize {
|
|
|
28
|
+ fmt.Println(errOutOfMaxSize.Error(), b.currentSize)
|
|
|
29
|
+ return 0, errOutOfMaxSize
|
|
|
30
|
+ }
|
|
|
31
|
+ b.msgChan <- p
|
|
|
32
|
+ atomic.AddInt32(&b.currentSize, 1)
|
|
|
33
|
+
|
|
|
34
|
+ return len(p), nil
|
|
|
35
|
+}
|
|
|
36
|
+
|
|
|
37
|
+// NewBaseHook
|
|
|
38
|
+// syncWriteFlag 同步写标识 true:同步写 false:异步写
|
|
|
39
|
+func NewBaseHook(syncWriteFlag bool, internalIo io.WriteCloser) (*BaseHook, error) {
|
|
|
40
|
+ writer := &BaseHook{
|
|
|
41
|
+ syncWrite: syncWriteFlag,
|
|
|
42
|
+ maxSize: 10000,
|
|
|
43
|
+ msgChan: make(chan []byte, 10000),
|
|
|
44
|
+ closeChan: make(chan struct{}),
|
|
|
45
|
+ w: internalIo,
|
|
|
46
|
+ }
|
|
|
47
|
+ go writer.ConsumeMsg()
|
|
|
48
|
+ return writer, nil
|
|
|
49
|
+}
|
|
|
50
|
+
|
|
|
51
|
+func (b *BaseHook) ConsumeMsg() {
|
|
|
52
|
+ for {
|
|
|
53
|
+ select {
|
|
|
54
|
+ case <-b.closeChan:
|
|
|
55
|
+ return
|
|
|
56
|
+ case m, ok := <-b.msgChan:
|
|
|
57
|
+ if ok {
|
|
|
58
|
+ atomic.AddInt32(&b.currentSize, -1)
|
|
|
59
|
+ if _, err := b.w.Write(m); err != nil {
|
|
|
60
|
+ fmt.Println(err)
|
|
|
61
|
+ }
|
|
|
62
|
+ }
|
|
|
63
|
+ }
|
|
|
64
|
+ }
|
|
|
65
|
+}
|
|
|
66
|
+
|
|
|
67
|
+func (b *BaseHook) Close() {
|
|
|
68
|
+ close(b.msgChan)
|
|
|
69
|
+ b.closeChan <- struct{}{}
|
|
|
70
|
+ //b.wc.Close()
|
|
|
71
|
+}
|
|
|
72
|
+
|
|
|
73
|
+type LoggerConfig struct {
|
|
|
74
|
+ Level int `json:"level,omitempty"`
|
|
|
75
|
+ Filename string `json:"filename,omitempty"`
|
|
|
76
|
+ MaxSize int `json:"maxsize,omitempty"`
|
|
|
77
|
+ //MaxBackups int `json:"max_backups,omitempty"`
|
|
|
78
|
+ //MaxAge int `json:"max_age,omitempty"`
|
|
|
79
|
+ //Compress bool `json:"compress,omitempty"`
|
|
|
80
|
+}
|
|
|
81
|
+
|
|
|
82
|
+type internalLog struct {
|
|
|
83
|
+ *logs.BeeLogger
|
|
|
84
|
+}
|
|
|
85
|
+
|
|
|
86
|
+func NewBeegoLogWriter(conf LoggerConfig) io.Writer {
|
|
|
87
|
+ logger := logs.GetBeeLogger()
|
|
|
88
|
+ logger.SetLevel(conf.Level)
|
|
|
89
|
+ logger.EnableFuncCallDepth(true)
|
|
|
90
|
+ logger.SetLogFuncCallDepth(2)
|
|
|
91
|
+ confByte, _ := json.Marshal(conf)
|
|
|
92
|
+ err := logger.SetLogger(logs.AdapterFile, string(confByte))
|
|
|
93
|
+ if err != nil {
|
|
|
94
|
+ fmt.Println(err.Error())
|
|
|
95
|
+ }
|
|
|
96
|
+ return &internalLog{logger}
|
|
|
97
|
+}
|
|
|
98
|
+
|
|
|
99
|
+func (l *internalLog) Write(p []byte) (n int, err error) {
|
|
|
100
|
+ l.Debug(string(p))
|
|
|
101
|
+ return len(p), nil
|
|
|
102
|
+} |