Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger added. #66

Merged
merged 5 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ Temporary Items
*.db
*.test
*.cover
*.log
8 changes: 7 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package surrealdb_test

import (
"bytes"
"fmt"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/surrealdb/surrealdb.go"
gorilla "github.com/surrealdb/surrealdb.go/pkg/gorilla"
"github.com/surrealdb/surrealdb.go/pkg/logger"
"github.com/surrealdb/surrealdb.go/pkg/websocket"
)

Expand Down Expand Up @@ -36,7 +39,10 @@ func TestSurrealDBSuite(t *testing.T) {
SurrealDBSuite.wsImplementations["gorilla"] = gorilla.Create()

// With options
SurrealDBSuite.wsImplementations["gorilla_opt"] = gorilla.Create().SetTimeOut(time.Minute).SetCompression(true)
phughk marked this conversation as resolved.
Show resolved Hide resolved
buff := bytes.NewBuffer([]byte{})
logData, err := logger.New().FromBuffer(buff).Make()
require.NoError(t, err)
SurrealDBSuite.wsImplementations["gorilla_opt"] = gorilla.Create().SetTimeOut(time.Minute).SetCompression(true).Logger(logData)

RunWsMap(t, SurrealDBSuite)
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ go 1.18

require (
github.com/gorilla/websocket v1.5.0
github.com/rs/zerolog v1.29.1
github.com/stretchr/testify v1.8.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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=
Expand Down
20 changes: 18 additions & 2 deletions pkg/gorilla/gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

gorilla "github.com/gorilla/websocket"
"github.com/surrealdb/surrealdb.go/internal/rpc"
"github.com/surrealdb/surrealdb.go/pkg/logger"
"github.com/surrealdb/surrealdb.go/pkg/rand"
"github.com/surrealdb/surrealdb.go/pkg/websocket"
)
Expand All @@ -29,6 +30,7 @@ type WebSocket struct {
connLock sync.Mutex
Timeout time.Duration
Option []Option
logger *logger.LogData

responseChannels map[string]chan rpc.RPCResponse
responseChannelsLock sync.RWMutex
Expand Down Expand Up @@ -74,6 +76,17 @@ func (ws *WebSocket) SetTimeOut(timeout time.Duration) *WebSocket {
return ws
}

// If path is empty it will use os.stdout/os.stderr
func (ws *WebSocket) Logger(logData *logger.LogData) *WebSocket {
ws.logger = logData
return ws
}

func (ws *WebSocket) RawLogger(logData *logger.LogData) *WebSocket {
ws.logger = logData
return ws
}

func (ws *WebSocket) SetCompression(compress bool) *WebSocket {
ws.Option = append(ws.Option, func(ws *WebSocket) error {
ws.Conn.EnableWriteCompression(compress)
Expand Down Expand Up @@ -189,12 +202,15 @@ func (ws *WebSocket) initialize() {
var res rpc.RPCResponse
err := ws.read(&res)
if err != nil {
// TODO need to find a proper way to log this error
ws.logger.Logger.Err(err)
phughk marked this conversation as resolved.
Show resolved Hide resolved
ws.logger.LogChannel <- err.Error()
continue
}
responseChan, ok := ws.getResponseChannel(fmt.Sprintf("%v", res.ID))
if !ok {
// TODO need to find a proper way to log this
err = errors.New("ResponseChannel is not ok")
ws.logger.Logger.Err(err)
ws.logger.LogChannel <- err.Error()
continue
}
responseChan <- res
Expand Down
60 changes: 60 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package logger

import (
"io"
"os"

"github.com/rs/zerolog"
)

const (
permission = 0664
)

type LogBuild struct {
writer io.Writer
path string
LogChannel chan string
}

type LogData struct {
writer io.Writer
LogFile *os.File
Logger zerolog.Logger
LogChannel chan string
}

func New() *LogBuild {
return &LogBuild{}
}

func (build *LogBuild) FromPath(path string) *LogBuild {
build.path = path
return build
}

func (build *LogBuild) FromBuffer(w io.Writer) *LogBuild {
build.writer = w
return build
}

func (build *LogBuild) FromChannel(chn chan string) *LogBuild {
build.LogChannel = chn
return build
}

func (build *LogBuild) Make() (logData *LogData, err error) {
logData = new(LogData)
logData.writer = os.Stdout
logData.writer = build.writer
logData.LogChannel = build.LogChannel
if build.path != "" {
logData.LogFile, err = os.OpenFile(build.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, permission)
if err != nil {
return nil, err
}
logData.writer = zerolog.SyncWriter(logData.LogFile)
}
logData.Logger = zerolog.New(logData.writer).With().Timestamp().Logger()
return
}
22 changes: 22 additions & 0 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package logger_test

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
"github.com/surrealdb/surrealdb.go/pkg/logger"
)

func TestLog(t *testing.T) {
buff := bytes.NewBuffer([]byte{})
templogger, err := logger.New().FromBuffer(buff).Make()
require.NoError(t, err)
require.NotNil(t, templogger)
require.NotNil(t, templogger.Logger)
// Get Stats Before
require.Equal(t, buff.Len(), 0)
templogger.Logger.Info().Msg("Test")
// Get Stats After
require.Contains(t, buff.String(), "Test")
}