From 52f205f3f1f2a99e011376750e57f63f591824b7 Mon Sep 17 00:00:00 2001 From: Dmitrii Okunev Date: Sat, 3 Aug 2024 00:34:03 +0100 Subject: [PATCH] Fix building and passing tests --- go.mod | 10 ++++++++++ go.sum | 17 +++++++++++++++++ logstash.go | 19 ++++++++++++++----- logstash_formatter_test.go | 2 +- 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..61203d0 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/xaionaro-go/logrustash + +go 1.22 + +require github.com/sirupsen/logrus v1.9.3 + +require ( + github.com/xaionaro-go/goautosocket v0.0.0-20240803221104-cef7f165571a + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..68f5f65 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/xaionaro-go/goautosocket v0.0.0-20240803221104-cef7f165571a h1:nnSVcn4e9TkQ5GuN/MoeET18gSYudJJMtlKXFRHvVjQ= +github.com/xaionaro-go/goautosocket v0.0.0-20240803221104-cef7f165571a/go.mod h1:7X2d4ohzI2SqqM/dNgIlBx3hUl6dB6clspwt+9c9IqA= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logstash.go b/logstash.go index 705d26c..58ba02d 100644 --- a/logstash.go +++ b/logstash.go @@ -10,7 +10,7 @@ import ( "time" "github.com/sirupsen/logrus" - "github.com/teh-cmc/goautosocket" + gas "github.com/xaionaro-go/goautosocket" ) // Hook represents a connection to a Logstash instance @@ -73,7 +73,16 @@ func NewAsyncHookWithFields(protocol, address, appName string, alwaysSentFields // NewHookWithFieldsAndPrefix creates a new hook to a Logstash instance, which listens on // `protocol`://`address`. alwaysSentFields will be sent with every log entry. prefix is used to select fields to filter. func NewHookWithFieldsAndPrefix(protocol, address, appName string, alwaysSentFields logrus.Fields, prefix string) (*Hook, error) { - conn, err := gas.Dial(protocol, address) + var ( + conn net.Conn + err error + ) + switch protocol { + case "tcp": + conn, err = gas.Dial("tcp", address) + default: + conn, err = net.Dial(protocol, address) + } if err != nil { return nil, err } @@ -110,7 +119,7 @@ func NewAsyncHookWithFieldsAndConn(conn net.Conn, appName string, alwaysSentFiel return NewAsyncHookWithFieldsAndConnAndPrefix(conn, appName, alwaysSentFields, "") } -//NewHookWithFieldsAndConnAndPrefix creates a new hook to a Logstash instance using the suppolied connection and prefix. +// NewHookWithFieldsAndConnAndPrefix creates a new hook to a Logstash instance using the suppolied connection and prefix. func NewHookWithFieldsAndConnAndPrefix(conn net.Conn, appName string, alwaysSentFields logrus.Fields, prefix string) (*Hook, error) { return &Hook{conn: conn, appName: appName, alwaysSentFields: alwaysSentFields, hookOnlyPrefix: prefix}, nil } @@ -172,12 +181,12 @@ func (h *Hook) filterHookOnly(entry *logrus.Entry) { } -//WithPrefix sets a prefix filter to use in all subsequent logging +// WithPrefix sets a prefix filter to use in all subsequent logging func (h *Hook) WithPrefix(prefix string) { h.hookOnlyPrefix = prefix } -//WithField add field with value that will be sent with each message +// WithField add field with value that will be sent with each message func (h *Hook) WithField(key string, value interface{}) { h.alwaysSentFields[key] = value } diff --git a/logstash_formatter_test.go b/logstash_formatter_test.go index 783362a..4dc9e8a 100644 --- a/logstash_formatter_test.go +++ b/logstash_formatter_test.go @@ -47,7 +47,7 @@ func TestLogstashFormatter(t *testing.T) { {"abc", "type"}, {"msg", "message"}, {"info", "level"}, - {"Get http://example.com: The error", "error"}, + {`Get "http://example.com": The error`, "error"}, // substituted fields {"def", "fields.message"}, {"ijk", "fields.level"},