-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgostash.go
77 lines (64 loc) · 1.42 KB
/
gostash.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
package gostash
import (
"net"
)
type Config struct {
LogstashHost string
LogstashPort string
InputType string
ThisHostName string
CodeVersion string
}
type LogstashClient struct {
logstashHost string
logstashPort string
serverAddr *net.UDPAddr
conn *net.UDPConn
err error
formatter *Formatter
}
func NewLogstashClient(config *Config) *LogstashClient {
c := LogstashClient{}
c.formatter = NewFormatter(config)
c.logstashHost = config.LogstashHost
c.logstashPort = config.LogstashPort
c.serverAddr, c.err = net.ResolveUDPAddr("udp", c.logstashHost+":"+c.logstashPort)
if c.err != nil {
// This is an invalid connection with error info
return &c
}
c.conn, c.err = net.DialUDP("udp", nil, c.serverAddr)
return &c
}
func (c *LogstashClient) Formatter() *Formatter {
return c.formatter
}
func (c *LogstashClient) LastError() error {
return c.err
}
func (c *LogstashClient) SendMessage(msg string, metadata map[string]string) {
if c.err != nil {
return
}
var encoded []byte
encoded, c.err = c.formatter.encode(msg, metadata)
if c.err != nil {
return
}
c.Write(encoded)
}
func (c *LogstashClient) Write(msg []byte) (n int, err error) {
if msg == nil {
return 0, nil
}
if c.conn != nil {
_, c.err = c.conn.Write(msg)
}
// Allow logging to fail silently when used with loggo
return len(msg), nil
}
func (c *LogstashClient) Close() {
if c.conn != nil {
c.err = c.conn.Close()
}
}