forked from TarsCloud/TarsGo
/
remotelogger.go
151 lines (127 loc) · 3.78 KB
/
remotelogger.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
142
143
144
145
146
147
148
149
150
151
package tars
import (
"time"
"github.com/jslyzt/tarsgo/tars/protocol/res/logf"
)
// RemoteTimeWriter writer for writing remote log.
type RemoteTimeWriter struct {
logInfo *logf.LogInfo
logs chan string
logPtr *logf.Log
reportSuccPtr *PropertyReport
reportFailPtr *PropertyReport
hasPrefix bool
}
// NewRemoteTimeWriter new and init RemoteTimeWriter
func NewRemoteTimeWriter() *RemoteTimeWriter {
rw := new(RemoteTimeWriter)
rw.logInfo = new(logf.LogInfo)
rw.logInfo.SFormat = "%Y%m%d"
rw.logInfo.SConcatStr = "_"
logs := make(chan string, remoteLogQueueSize)
rw.logs = logs
rw.logPtr = new(logf.Log)
comm := NewCommunicator()
node := GetServerConfig().Log
rw.EnableSufix(true)
rw.EnablePrefix(true)
rw.SetSeparator("|")
rw.SetPrefix(true)
comm.StringToProxy(node, rw.logPtr)
go rw.Sync2remote()
return rw
}
// Sync2remote syncs the log buffer to remote.
func (rw *RemoteTimeWriter) Sync2remote() {
maxLen := remoteLogMaxNumOneTime
interval := time.Second
v := make([]string, 0, maxLen)
for {
select {
case log := <-rw.logs:
v = append(v, log)
if len(v) >= maxLen {
err := rw.sync2remote(v)
if err != nil {
TLOG.Error("sync to remote error")
rw.reportFailPtr.Report(len(v))
}
rw.reportSuccPtr.Report(len(v))
v = make([]string, 0, maxLen) //reset the slice after syncing log to remote
}
case <-time.After(interval):
if len(v) > 0 {
err := rw.sync2remote(v)
if err != nil {
TLOG.Error("sync to remote error")
rw.reportFailPtr.Report(len(v))
}
rw.reportSuccPtr.Report(len(v))
v = make([]string, 0, maxLen) //reset the slice after syncing log to remote
}
}
}
}
func (rw *RemoteTimeWriter) sync2remote(s []string) error {
err := rw.logPtr.LoggerbyInfo(rw.logInfo, s)
return err
}
// InitServerInfo init the remote log server info.
func (rw *RemoteTimeWriter) InitServerInfo(app string, server string, filename string, setdivision string) {
rw.logInfo.Appname = app
rw.logInfo.Servername = server
rw.logInfo.SFilename = filename
rw.logInfo.Setdivision = setdivision
serverInfo := app + "." + server + "." + filename
failServerInfo := serverInfo + "_log_send_fail"
failSum := NewSum()
rw.reportFailPtr = CreatePropertyReport(failServerInfo, failSum)
succServerInfo := serverInfo + "_log_send_succ"
succSum := NewSum()
rw.reportSuccPtr = CreatePropertyReport(succServerInfo, succSum)
}
// EnableSufix puts sufix after logs.
func (rw *RemoteTimeWriter) EnableSufix(hasSufix bool) {
rw.logInfo.BHasSufix = hasSufix
}
// EnablePrefix puts prefix before logs.
func (rw *RemoteTimeWriter) EnablePrefix(hasAppNamePrefix bool) {
rw.logInfo.BHasAppNamePrefix = hasAppNamePrefix
}
// SetFileNameConcatStr sets the filename concat string.
func (rw *RemoteTimeWriter) SetFileNameConcatStr(s string) {
rw.logInfo.SConcatStr = s
}
// SetSeparator set seprator between logs.
func (rw *RemoteTimeWriter) SetSeparator(s string) {
rw.logInfo.SSepar = s
}
// EnableSqarewrapper enables SquareBracket wrapper for the logs.
func (rw *RemoteTimeWriter) EnableSqarewrapper(hasSquareBracket bool) {
rw.logInfo.BHasSquareBracket = hasSquareBracket
}
// SetLogType sets the log type.
func (rw *RemoteTimeWriter) SetLogType(logType string) {
rw.logInfo.SLogType = logType
}
// InitFormat sets the log format.
func (rw *RemoteTimeWriter) InitFormat(s string) {
rw.logInfo.SFormat = s
}
// NeedPrefix return if need prefix for the logger.
func (rw *RemoteTimeWriter) NeedPrefix() bool {
return rw.hasPrefix
}
// SetPrefix set if need prefix for the logger.
func (rw *RemoteTimeWriter) SetPrefix(enable bool) {
rw.hasPrefix = enable
}
// Write Writes the logs to the buffer.
func (rw *RemoteTimeWriter) Write(b []byte) {
s := string(b[:])
select {
case rw.logs <- s:
default:
TLOG.Error("remote log chan is full")
}
}