-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_builder.go
61 lines (51 loc) · 1.51 KB
/
log_builder.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
package main
import (
"encoding/json"
"fmt"
"net"
"time"
"github.com/sirupsen/logrus"
)
type jsonTime struct {
time.Time
}
type jsonLogLine struct {
Message string `json:"message"`
ContainerId string `json:"container_id"`
ContainerName string `json:"container_name"`
ContainerCreated jsonTime `json:"container_created"`
ImageId string `json:"image_id"`
ImageName string `json:"image_name"`
Command string `json:"command"`
Tag string `json:"tag"`
Extra map[string]string `json:"extra"`
Host string `json:"host"`
Timestamp jsonTime `json:"time"`
}
func logMessage(lp *logPair, message []byte) error {
lp.logLine.Message = string(message[:])
lp.logLine.Timestamp = jsonTime{time.Now()}
bytes, err := json.Marshal(lp.logLine)
if err != nil {
return err
}
sendUDP(lp, bytes)
return nil
}
func (t jsonTime) MarshalJSON() ([]byte, error) {
str := fmt.Sprintf("\"%s\"", t.Format(time.RFC3339Nano))
return []byte(str), nil
}
func sendUDP(lp *logPair, message []byte) {
conn, err := net.Dial("udp", lp.dst)
if err != nil {
logrus.WithField("id", lp.info.ContainerID).Error(fmt.Sprintf("Error opening UDP connection %s", err))
return
}
defer conn.Close()
_, err = fmt.Fprintf(conn, string(message))
if err != nil {
logrus.WithField("id", lp.info.ContainerID).Error(fmt.Sprintf("Error sending: %s", err))
return
}
}