-
Notifications
You must be signed in to change notification settings - Fork 1
/
emit_log_topic.go
70 lines (62 loc) · 1.25 KB
/
emit_log_topic.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
package main
import (
"bufio"
"fmt"
"github.com/streadway/amqp"
"log"
"os"
)
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
}
func main() {
conn, err := amqp.Dial("amqp://guest:guest@127.0.0.1:5672/")
failOnError(err,"Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
err = ch.ExchangeDeclare(
"logs_topic",
"topic",
true,
false,
false,
false,
nil,
)
failOnError(err, "Failed to declare an exchange")
fmt.Println("enter message: ")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
body := scanner.Text()
fmt.Println("sending: " + body)
publishMessage(ch, body)
}
}
func publishMessage(ch *amqp.Channel, body string) {
err := ch.Publish(
"logs_topic",
severityForm(os.Args),
false,
false,
amqp.Publishing{
DeliveryMode:amqp.Persistent,
ContentType:"text/plain",
Body: []byte(body),
})
log.Printf(" [x] Sent %s", body)
failOnError(err, "Failed to publish a message")
}
func severityForm(args []string) string {
var s string
if (len(args) < 2) || os.Args[1] == "" {
s = "info"
} else {
s = os.Args[1]
}
return s
}