-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
129 lines (107 loc) · 2.98 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"github.com/nats-io/nats"
"github.com/pivotal-golang/lager"
"log/syslog"
"os"
"os/signal"
"strings"
"syscall"
)
type logEntry struct {
Data string
Reply string
Subject string
}
var stop chan bool
var logger lager.Logger
func main() {
logger = lager.NewLogger("nats_to_syslog")
stop = make(chan bool)
buffer := make(chan *nats.Msg, 1000)
trapSignals()
var natsUri = flag.String("nats-uri", "nats://localhost:4222", "The NATS server URI")
var natsSubject = flag.String("nats-subject", ">", "The NATS subject to subscribe to")
var syslogEndpoint = flag.String("syslog-endpoint", "localhost:514", "The remote syslog server host:port")
var debug = flag.Bool("debug", false, "debug logging true/false")
flag.Parse()
setupLogger(*debug)
syslog := connectToSyslog(*syslogEndpoint)
defer syslog.Close()
natsClient := connectToNATS(*natsUri)
defer natsClient.Close()
go func() {
for message := range buffer {
sendToSyslog(message, syslog)
}
}()
natsClient.Subscribe(*natsSubject, func(message *nats.Msg) {
buffer <- message
})
logger.Info("subscribed-to-subject", lager.Data{"subject": *natsSubject})
<-stop
logger.Info("bye.")
}
func handleError(err error, context string) {
if err != nil {
context = strings.Replace(context, " ", "-", -1)
errorLogger := logger.Session(context)
errorLogger.Error("error", err)
os.Exit(1)
}
}
func buildLogMessage(message *nats.Msg) string {
entry := logEntry{
Data: string(message.Data),
Reply: message.Reply,
Subject: message.Subject,
}
data, err := json.Marshal(entry)
if err != nil {
logger.Error("unmarshalling-log-failed", err, lager.Data{"data": string(message.Data)})
return ""
}
return string(data)
}
func connectToSyslog(endpoint string) *syslog.Writer {
syslog, err := syslog.Dial("tcp", endpoint, syslog.LOG_INFO, "nats_to_syslog")
handleError(err, "connecting to syslog")
logger.Info("connected-to-syslog", lager.Data{"endpoint": endpoint})
return syslog
}
func connectToNATS(natsUri string) *nats.Conn {
natsClient, err := nats.Connect(natsUri)
handleError(err, "connecting to nats")
logger.Info("connected-to-nats", lager.Data{"uri": natsUri})
return natsClient
}
func sendToSyslog(message *nats.Msg, syslog *syslog.Writer) {
logMessage := buildLogMessage(message)
logger.Debug("message-sent-to-syslog", lager.Data{"message": logMessage})
err := syslog.Info(logMessage)
if err != nil {
logger.Error("logging-to-syslog-failed", err)
stop <- true
}
}
func setupLogger(debug bool) {
if debug {
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
} else {
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.INFO))
}
}
func trapSignals() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT)
signal.Notify(signals, syscall.SIGKILL)
signal.Notify(signals, syscall.SIGTERM)
go func() {
for signal := range signals {
logger.Info("signal-caught", lager.Data{"signal": signal})
stop <- true
}
}()
}