Skip to content

Commit

Permalink
goflow2: use slog instead of logrus (#199)
Browse files Browse the repository at this point in the history
This removes the `logrus` dependency as it's in maintenance-only mode.
  • Loading branch information
lspgn authored Aug 19, 2024
1 parent b01b1aa commit d4bffe2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 79 deletions.
43 changes: 28 additions & 15 deletions cmd/enricher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"flag"
"fmt"
"io"
"log"
"log/slog"
"net"
"os"
"strings"
Expand All @@ -25,7 +27,6 @@ import (
_ "github.com/netsampler/goflow2/v2/transport/kafka"

"github.com/oschwald/geoip2-golang"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/encoding/protodelim"
)

Expand Down Expand Up @@ -92,23 +93,39 @@ func main() {
os.Exit(0)
}

lvl, _ := log.ParseLevel(*LogLevel)
log.SetLevel(lvl)
var loglevel slog.Level
if err := loglevel.UnmarshalText([]byte(*LogLevel)); err != nil {
log.Fatal("error parsing log level")
}

lo := slog.HandlerOptions{
Level: loglevel,
}
logger := slog.New(slog.NewTextHandler(os.Stderr, &lo))

switch *LogFmt {
case "json":
logger = slog.New(slog.NewJSONHandler(os.Stderr, &lo))
}

slog.SetDefault(logger)

var dbAsn, dbCountry *geoip2.Reader
var err error
if *DbAsn != "" {
dbAsn, err = geoip2.Open(*DbAsn)
if err != nil {
log.Fatal(err)
slog.Error("error opening asn db", slog.String("error", err.Error()))
os.Exit(1)
}
defer dbAsn.Close()
}

if *DbCountry != "" {
dbCountry, err = geoip2.Open(*DbCountry)
if err != nil {
log.Fatal(err)
slog.Error("error opening country db", slog.String("error", err.Error()))
os.Exit(1)
}
defer dbCountry.Close()
}
Expand All @@ -120,16 +137,12 @@ func main() {

transporter, err := transport.FindTransport(*Transport)
if err != nil {
log.Fatal(err)
slog.Error("error transporter", slog.String("error", err.Error()))
os.Exit(1)
}
defer transporter.Close()

switch *LogFmt {
case "json":
log.SetFormatter(&log.JSONFormatter{})
}

log.Info("starting enricher")
logger.Info("starting enricher")

rdr := bufio.NewReader(os.Stdin)

Expand All @@ -138,7 +151,7 @@ func main() {
if err := protodelim.UnmarshalFrom(rdr, &msg); err != nil && errors.Is(err, io.EOF) {
return
} else if err != nil {
log.Error(err)
slog.Error("error unmarshalling message", slog.String("error", err.Error()))
continue
}

Expand All @@ -150,13 +163,13 @@ func main() {

key, data, err := formatter.Format(&msg)
if err != nil {
log.Error(err)
slog.Error("error formatting message", slog.String("error", err.Error()))
continue
}

err = transporter.Send(key, data)
if err != nil {
log.Error(err)
slog.Error("error sending message", slog.String("error", err.Error()))
continue
}

Expand Down
Loading

0 comments on commit d4bffe2

Please sign in to comment.