/
logger.go
48 lines (42 loc) · 1.01 KB
/
logger.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
package main
import (
"github.com/streadway/amqp"
"code.google.com/p/goprotobuf/proto"
"github.com/Sophiacom/RabbitMQGoDemo/protobuf"
)
var (
exchange = "logger-exchange"
routingKey = ""
)
func Log(connection *amqp.Connection, inChannel chan *protobuf.Log) {
// create a channel on this connection
channel, err := connection.Channel()
if err != nil {
panic(err)
}
defer channel.Close()
for log := range inChannel {
body, err := proto.Marshal(log)
if err != nil {
panic(err)
}
err = channel.Publish(
exchange, // publish to an exchange
routingKey, // routing to 0 or more queues
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{},
ContentType: "",
ContentEncoding: "",
Body: body,
DeliveryMode: amqp.Transient, // 1=non-persistent, 2=persistent
Priority: 0, // 0-9
// a bunch of application/implementation-specific fields
},
)
if err != nil {
panic(err)
}
}
}