This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
microservices-collection/logging/cmd/main.go

75 lines
1.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/bdlm/log"
"github.com/satori/go.uuid"
"github.com/streadway/amqp"
srv "dev.sum7.eu/genofire/microservices-collection/logging"
)
const ServiceName = "eu.sum7.log-cmd"
var ServiceID = uuid.Must(uuid.NewV4())
func main() {
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
})
logLevel, err := strconv.ParseUint(os.Args[1], 10, 32)
if err != nil {
log.Fatalf("Failed to parse LogLevel: %s", err)
}
logMessage := srv.LogMessage{
ServiceName: ServiceName,
ServiceID: ServiceID,
Time: time.Now(),
Level: uint32(logLevel),
Message: strings.Join(os.Args[2:], " "),
}
body, err := json.Marshal(logMessage)
if err != nil {
log.Fatalf("Failed to parse LogLevel: %s", err)
}
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("Failed to connect to RabbitMQ: %s", err)
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
log.Fatalf("Failed to open a channel: %s", err)
}
defer ch.Close()
if err = srv.ExchangeDeclare(ch); err != nil {
log.Fatalf("Failed to declare an exchange: %s", err)
}
err = ch.Publish(
srv.EXCHANGE,
fmt.Sprintf("%d.%s", logLevel, ServiceName),
false,
false,
amqp.Publishing{
ContentType: "application/json",
DeliveryMode: amqp.Persistent,
Body: body,
})
if err != nil {
log.Fatalf("Failed to publish log message: %s", err)
}
log.Printf(" [x] Sent %s", body)
}