logrus_hook.go
2.1 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package log
import (
"encoding/json"
"errors"
"fmt"
"github.com/beego/beego/v2/core/logs"
"io"
"sync/atomic"
)
var errOutOfMaxSize = errors.New("msg size is out of limit ")
type BaseHook struct {
syncWrite bool
msgChan chan []byte
maxSize int32
currentSize int32
closeChan chan struct{}
w io.Writer
}
func (b *BaseHook) Write(p []byte) (n int, err error) {
if b.syncWrite {
return b.w.Write(p)
}
if b.currentSize >= b.maxSize {
fmt.Println(errOutOfMaxSize.Error(), b.currentSize)
return 0, errOutOfMaxSize
}
b.msgChan <- p
atomic.AddInt32(&b.currentSize, 1)
return len(p), nil
}
// NewBaseHook
// syncWriteFlag 同步写标识 true:同步写 false:异步写
func NewBaseHook(syncWriteFlag bool, internalIo io.WriteCloser) (*BaseHook, error) {
writer := &BaseHook{
syncWrite: syncWriteFlag,
maxSize: 10000,
msgChan: make(chan []byte, 10000),
closeChan: make(chan struct{}),
w: internalIo,
}
go writer.ConsumeMsg()
return writer, nil
}
func (b *BaseHook) ConsumeMsg() {
for {
select {
case <-b.closeChan:
return
case m, ok := <-b.msgChan:
if ok {
atomic.AddInt32(&b.currentSize, -1)
if _, err := b.w.Write(m); err != nil {
fmt.Println(err)
}
}
}
}
}
func (b *BaseHook) Close() {
close(b.msgChan)
b.closeChan <- struct{}{}
//b.wc.Close()
}
type LoggerConfig struct {
Level int `json:"level,omitempty"`
Filename string `json:"filename,omitempty"`
MaxSize int `json:"maxsize,omitempty"`
//MaxBackups int `json:"max_backups,omitempty"`
//MaxAge int `json:"max_age,omitempty"`
//Compress bool `json:"compress,omitempty"`
}
type internalLog struct {
*logs.BeeLogger
}
func NewBeegoLogWriter(conf LoggerConfig) io.Writer {
logger := logs.GetBeeLogger()
logger.SetLevel(conf.Level)
logger.EnableFuncCallDepth(true)
logger.SetLogFuncCallDepth(2)
confByte, _ := json.Marshal(conf)
err := logger.SetLogger(logs.AdapterFile, string(confByte))
if err != nil {
fmt.Println(err.Error())
}
return &internalLog{logger}
}
func (l *internalLog) Write(p []byte) (n int, err error) {
l.Debug(string(p))
return len(p), nil
}