forked from hhkbp2/go-logging
/
handler_thrift.go
104 lines (95 loc) · 2.7 KB
/
handler_thrift.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
package logging
import (
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
gen "github.com/hhkbp2/go-logging/gen-go/logging"
"time"
)
const (
ThriftDefaultTimeout = 3 * time.Second
)
// A handler class which write logging records, in thrift.
// the thrift connection is kept open across logging calss. If there is
// any problem with it, an attempt is made to reconnect on the next call.
type ThriftHandler struct {
*BaseHandler
host string
port uint16
timeout time.Duration
transport thrift.TTransport
client *gen.ThriftLoggingServiceClient
}
// Initializes the handler with a specific host address and port.
func NewThriftHandler(host string, port uint16) *ThriftHandler {
object := &ThriftHandler{
BaseHandler: NewBaseHandler("", LevelNotset),
host: host,
port: port,
timeout: ThriftDefaultTimeout,
}
Closer.AddHandler(object)
return object
}
// Try to establish the thrift connection to specific host and port.
func (self *ThriftHandler) connect() error {
address := fmt.Sprintf("%s:%d", self.host, self.port)
socket, err := thrift.NewTSocket(address)
if err != nil {
return err
}
transport := thrift.NewTFramedTransport(socket)
factory := thrift.NewTBinaryProtocolFactoryDefault()
client := gen.NewThriftLoggingServiceClientFactory(transport, factory)
if err := transport.Open(); err != nil {
return err
}
self.transport = transport
self.client = client
return nil
}
// Emit a record.
// Report the logging record to server(establish the connectino if necessary).
// If there is an error with connection, silently drop the packet.
func (self *ThriftHandler) Emit(record *LogRecord) error {
self.Format(record)
r := &gen.ThriftLogRecord{
Name: record.Name,
Level: int32(record.Level),
PathName: record.PathName,
FileName: record.FileName,
LineNo: int32(record.LineNo),
FuncName: record.FuncName,
Message: record.Message,
}
if self.client == nil {
if err := self.connect(); err != nil {
return err
}
}
return self.client.Report(r)
}
func (self *ThriftHandler) Handle(record *LogRecord) int {
return self.Handle2(self, record)
}
// Handles an error during logging.
// An error has occurred during logging. Most likely cause connection lost.
// Close the thrift connection so that we can retry on the next event.
func (self *ThriftHandler) HandleError(record *LogRecord, err error) {
if self.client != nil {
self.transport.Close()
self.transport = nil
self.client = nil
}
self.BaseHandler.HandleError(record, err)
}
// Close the thrift client.
func (self *ThriftHandler) Close() {
self.Lock()
defer self.Unlock()
if self.client != nil {
self.transport.Close()
self.transport = nil
self.client = nil
}
self.BaseHandler.Close()
}