forked from aryming/logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
141 lines (123 loc) · 2.8 KB
/
conn.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package logger
import (
"encoding/json"
"fmt"
"io"
"net"
"os"
"strings"
"sync"
"time"
)
type connLogger struct {
sync.Mutex
innerWriter io.WriteCloser
ReconnectOnMsg bool `json:"reconnectOnMsg"`
Reconnect bool `json:"reconnect"`
Net string `json:"net"`
Addr string `json:"addr"`
Level string `json:"level"`
LogLevel int
illNetFlag bool //网络异常标记
}
func (c *connLogger) Init(jsonConfig string) error {
if len(jsonConfig) == 0 {
return nil
}
fmt.Printf("consoleWriter Init:%s\n", jsonConfig)
err := json.Unmarshal([]byte(jsonConfig), c)
if err != nil {
return err
}
if l, ok := LevelMap[c.Level]; ok {
c.LogLevel = l
}
if c.innerWriter != nil {
c.innerWriter.Close()
c.innerWriter = nil
}
return nil
}
func (c *connLogger) LogWrite(when time.Time, msgText interface{}, level int) (err error) {
if level > c.LogLevel {
return nil
}
msg, ok := msgText.(*loginfo)
if !ok {
return
}
if c.needToConnectOnMsg() {
err = c.connect()
if err != nil {
return
}
//重连成功
c.illNetFlag = false
}
//每条消息都重连一次日志中心,适用于写日志频率极低的情况下的服务调用,避免长时间连接,占用资源
if c.ReconnectOnMsg { // 频繁日志发送切勿开启
defer c.innerWriter.Close()
}
//网络异常时,消息发出
if !c.illNetFlag {
err = c.println(when, msg)
//网络异常,通知处理网络的go程自动重连
if err != nil {
c.illNetFlag = true
}
}
return
}
func (c *connLogger) Destroy() {
if c.innerWriter != nil {
c.innerWriter.Close()
}
}
func (c *connLogger) connect() error {
if c.innerWriter != nil {
c.innerWriter.Close()
c.innerWriter = nil
}
addrs := strings.Split(c.Addr, ";")
for _, addr := range addrs {
conn, err := net.Dial(c.Net, addr)
if err != nil {
fmt.Fprintf(os.Stderr, "net.Dial error:%v\n", err)
continue
//return err
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.SetKeepAlive(true)
}
c.innerWriter = conn
return nil
}
return fmt.Errorf("hava no valid logs service addr:%v", c.Addr)
}
func (c *connLogger) needToConnectOnMsg() bool {
if c.Reconnect {
c.Reconnect = false
return true
}
if c.innerWriter == nil {
return true
}
if c.illNetFlag {
return true
}
return c.ReconnectOnMsg
}
func (c *connLogger) println(when time.Time, msg *loginfo) error {
c.Lock()
defer c.Unlock()
ss, err := json.Marshal(msg)
if err != nil {
return err
}
_, err = c.innerWriter.Write(append(ss, '\n'))
//返回err,解决日志系统网络异常后的自动重连
return err
}
func init() {
Register(AdapterConn, &connLogger{LogLevel: LevelTrace})
}