Skip to content

Commit

Permalink
Replace logrus with the standard slog package
Browse files Browse the repository at this point in the history
  • Loading branch information
IngmarStein committed Nov 7, 2024
1 parent 0efdcb4 commit fa017d7
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 95 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"makefile.configureOnOpen": false
}
1 change: 1 addition & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"fmt"

"github.com/ingmarstein/tcp-multiplexer/pkg/message"
"github.com/spf13/cobra"
)
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"

"github.com/spf13/cobra"
)

var verbose bool
Expand Down
23 changes: 10 additions & 13 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ THE SOFTWARE.
package cmd

import (
"github.com/ingmarstein/tcp-multiplexer/pkg/message"
"github.com/ingmarstein/tcp-multiplexer/pkg/multiplexer"
"github.com/sirupsen/logrus"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/ingmarstein/tcp-multiplexer/pkg/message"
"github.com/ingmarstein/tcp-multiplexer/pkg/multiplexer"
"github.com/spf13/cobra"
)

Expand All @@ -41,29 +42,25 @@ var serverCmd = &cobra.Command{
Use: "server",
Short: "start multiplexer proxy server",
Run: func(cmd *cobra.Command, args []string) {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
logrus.SetLevel(logrus.WarnLevel)
slog.SetLogLoggerLevel(slog.LevelWarn)
if verbose {
logrus.SetLevel(logrus.InfoLevel)
slog.SetLogLoggerLevel(slog.LevelInfo)
}
if debug {
logrus.SetReportCaller(true)
logrus.SetLevel(logrus.DebugLevel)
slog.SetLogLoggerLevel(slog.LevelDebug)
}

msgReader, ok := message.Readers[applicationProtocol]
if !ok {
logrus.Errorf("%s application protocol is not supported", applicationProtocol)
slog.Error(fmt.Sprintf("%s application protocol is not supported", applicationProtocol))
os.Exit(2)
}

mux := multiplexer.New(targetServer, port, msgReader)
go func() {
err := mux.Start()
if err != nil {
logrus.Error(err)
slog.Error(err.Error())
os.Exit(2)
}
}()
Expand All @@ -79,7 +76,7 @@ var serverCmd = &cobra.Command{

err := mux.Close()
if err != nil {
logrus.Error(err)
slog.Error(err.Error())
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions example/dummy-iso8583-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"bytes"
"encoding/binary"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/ingmarstein/tcp-multiplexer/pkg/message"
"net"
"os"

"github.com/davecgh/go-spew/spew"
"github.com/ingmarstein/tcp-multiplexer/pkg/message"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion example/dummy-iso8583-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package main

import (
"fmt"
"github.com/ingmarstein/tcp-multiplexer/pkg/message"
"io"
"net"
"os"

"github.com/ingmarstein/tcp-multiplexer/pkg/message"
)

func handleConnection(conn net.Conn) {
Expand Down
3 changes: 2 additions & 1 deletion example/http-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"fmt"
"github.com/davecgh/go-spew/spew"
"html"
"net/http"
"net/http/httputil"
"os"

"github.com/davecgh/go-spew/spew"
)

func getPort() string {
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ go 1.23.2

require (
github.com/davecgh/go-spew v1.1.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
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/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
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/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16 changes: 8 additions & 8 deletions pkg/message/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bufio"
"bytes"
"errors"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"fmt"
"io"
"log/slog"
"net/textproto"
"strconv"
"strings"

"github.com/davecgh/go-spew/spew"
)

// https://tools.ietf.org/html/rfc2616
Expand Down Expand Up @@ -41,13 +43,13 @@ func (H HTTPMessageReader) ReadMessage(conn io.Reader) ([]byte, error) {
if err != nil {
return nil, err
}
logrus.Debug(startLine)
slog.Debug(startLine)

headers, err := tp.ReadMIMEHeader()
if err != nil {
return nil, err
}
logrus.Debug(headers)
slog.Debug(fmt.Sprintf("%v", headers))

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#body
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#body_2
Expand All @@ -57,7 +59,7 @@ func (H HTTPMessageReader) ReadMessage(conn io.Reader) ([]byte, error) {
// first check form type
isFormContentType := false
if vv, ok := headers[headerKeyContentType]; ok {
logrus.Debug(vv[0])
slog.Debug(vv[0])
parts := strings.Split(vv[0], ";")
contentType := strings.TrimSpace(parts[0])
// 3. Multiple-resource bodies
Expand Down Expand Up @@ -108,9 +110,7 @@ func (H HTTPMessageReader) ReadMessage(conn io.Reader) ([]byte, error) {

msg := dumpHTTPMessage(startLine, headers, body)

if logrus.GetLevel() == logrus.DebugLevel {
spew.Dump(msg)
}
slog.Debug(spew.Sdump(msg))

return msg, err
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/message/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package message
import (
"bytes"
"fmt"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"log"
"log/slog"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand All @@ -14,14 +13,18 @@ import (
)

func TestHTTPMessageReader_ReadMessage(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
slog.SetLogLoggerLevel(slog.LevelDebug)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dump, err := httputil.DumpRequest(r, true)
assert.Equal(t, nil, err)
if err != nil {
t.Fatal("Expected no error, but got:", err)
}
fmt.Println(string(dump))

dump2, err := HTTPMessageReader{}.ReadMessage(bytes.NewReader(dump))
assert.Equal(t, nil, err)
if err != nil {
t.Fatal("Expected no error, but got:", err)
}
// headers may not in same orders
fmt.Println(string(dump2))
}))
Expand Down
8 changes: 5 additions & 3 deletions pkg/message/mpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package message
import (
"bytes"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"testing"

"github.com/davecgh/go-spew/spew"
)

func TestMPUMessageReader_ReadMessage(t *testing.T) {
Expand All @@ -20,6 +20,8 @@ func TestMPUMessageReader_ReadMessage(t *testing.T) {
spew.Dump(buf.Bytes())

iso, err := MPUMessageReader{}.ReadMessage(bytes.NewReader(buf.Bytes()))
assert.Equal(t, nil, err)
if err != nil {
t.Fatal("Expected no error, but got:", err)
}
spew.Dump(iso)
}
Loading

0 comments on commit fa017d7

Please sign in to comment.