-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdrain.go
More file actions
34 lines (30 loc) · 785 Bytes
/
drain.go
File metadata and controls
34 lines (30 loc) · 785 Bytes
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
package udp
import (
"fmt"
"net"
"net/url"
)
type logDrain struct {
uri string
}
// NewDrain returns a pointer to a new instance of a UDP-based drain.LogDrain.
func NewDrain(drainURL string) (*logDrain, error) {
u, err := url.Parse(drainURL)
if err != nil {
return nil, err
}
if u.Scheme != "udp" && u.Scheme != "syslog" {
return nil, fmt.Errorf("Invalid drain url scheme: %s", u.Scheme)
}
return &logDrain{uri: u.Host + u.Path}, nil
}
// Send forwards the provided log message to an external destination using UDP for transport.
func (d *logDrain) Send(message string) error {
conn, err := net.Dial("udp", d.uri)
if err != nil {
return fmt.Errorf("Error dialing log drain at %s over udp", d.uri)
}
defer conn.Close()
fmt.Fprintln(conn, message)
return nil
}