-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
48 lines (40 loc) · 907 Bytes
/
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
package main
import (
"bytes"
"context"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"github.com/osbuild/logging/pkg/splunk"
)
func main() {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
fmt.Println(buf.String())
}))
defer srv.Close()
url, ok := os.LookupEnv("SPLUNK_URL")
if !ok {
url = srv.URL
}
token := os.Getenv("SPLUNK_TOKEN")
c := splunk.SplunkConfig{
Level: slog.LevelDebug,
URL: url,
Token: token,
Source: "source",
Hostname: "hostname",
}
h := splunk.NewSplunkHandler(context.Background(), c)
log := slog.New(h)
log.Debug("message", "k1", "v1")
defer func() {
// block until all logs are sent but not more than 2 seconds
h.Close()
s := h.Statistics()
fmt.Printf("sent %d events in %d batches\n", s.EventCount, s.BatchCount)
}()
}