forked from redhat-aqe/kafka-logger-go-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_setup.go
65 lines (54 loc) · 1.19 KB
/
example_setup.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
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/tracer0tong/kafkalogrus"
"github.com/sirupsen/logrus"
)
func loadTLSConfig(caCertPath string) *tls.Config {
// Load CA cert
caCert, err := ioutil.ReadFile(caCertPath)
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Setup HTTPS client
tlsConfig := &tls.Config{
RootCAs: caCertPool,
}
return tlsConfig
}
func main() {
brokers := strings.Split(os.Getenv("KAFKA_BROKER"), ";")
caCert := os.Getenv("KAFKA_CACERT")
topic := os.Getenv("KAFKA_TOPIC")
tlsConfig := loadTLSConfig(caCert)
// Create a new hook
hook, err := kafkalogrus.NewKafkaLogrusHook(
topic,
logrus.AllLevels,
&logrus.JSONFormatter{},
brokers,
topic,
false,
tlsConfig)
if err != nil {
panic(err)
}
// Create a new logrus.Logger
logger := logrus.New()
// Add hook to logger
logger.Hooks.Add(hook)
// Send message to logger
logger.WithField("Field", "Value").Info("This is an Info msg")
logger.Warn("This is a Warn msg")
logger.Error("This is an Error msg")
// Ensure log messages were written to Kafka
time.Sleep(time.Second)
}