Skip to content

Commit

Permalink
fix: use noop logger by default (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
iskorotkov authored Jan 13, 2024
1 parent 846716d commit e0f1786
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
5 changes: 2 additions & 3 deletions client_web_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"time"
Expand Down Expand Up @@ -100,7 +99,7 @@ func (c *WebSocketClient) Start(ctx context.Context, executors []WebsocketExecut
if IsErrWebsocketClosed(err) {
return
}
log.Println(err)
logger.Println(err)
return
}
}
Expand All @@ -124,7 +123,7 @@ func (c *WebSocketClient) Start(ctx context.Context, executors []WebsocketExecut
}
}
case <-ctx.Done():
log.Println("interrupt")
logger.Println("interrupt")

for _, executor := range executors {
if err := executor.Close(); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package bybit

import (
"io"
"log"
)

type Logger interface {
Println(values ...any)
}

var logger Logger = newNoopLogger()

func SetLogger(l Logger) {
if l != nil {
// Use provided logger.
logger = l
} else {
// Disable logging.
logger = newNoopLogger()
}
}

func newNoopLogger() Logger {
return log.New(io.Discard, "", log.LstdFlags)
}
3 changes: 1 addition & 2 deletions v5_market_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"

"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -305,7 +304,7 @@ func (l *V5GetPremiumIndexPriceKlineList) UnmarshalJSON(data []byte) error {
if len(d) != 5 {
return errors.New("so far len(items) must be 5, please check it on documents")
}
log.Println(d)
logger.Println(d)
*l = append(*l, V5GetPremiumIndexPriceKlineItem{
StartTime: d[0].(string),
Open: d[1].(string),
Expand Down
3 changes: 1 addition & 2 deletions v5_ws_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/signal"
"sync"
Expand Down Expand Up @@ -156,7 +155,7 @@ func (s *V5WebsocketPrivateService) Start(ctx context.Context, errHandler ErrHan
return err
}
case <-ctx.Done():
log.Println("interrupt")
logger.Println("interrupt")

if err := s.Close(); err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions v5_ws_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"errors"
"log"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -185,7 +184,7 @@ func (s *V5WebsocketPublicService) Start(ctx context.Context, errHandler ErrHand
return err
}
case <-ctx.Done():
log.Println("interrupt")
logger.Println("interrupt")

if err := s.Close(); err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions ws_spot_v1_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"errors"
"log"
"os"
"os/signal"
"time"
Expand Down Expand Up @@ -190,7 +189,7 @@ func (s *SpotWebsocketV1PrivateService) Start(ctx context.Context) {
if IsErrWebsocketClosed(err) {
return
}
log.Println(err)
logger.Println(err)
return
}
}
Expand All @@ -211,7 +210,7 @@ func (s *SpotWebsocketV1PrivateService) Start(ctx context.Context) {
return
}
case <-ctx.Done():
log.Println("interrupt")
logger.Println("interrupt")

if err := s.Close(); err != nil {
return
Expand Down
5 changes: 2 additions & 3 deletions ws_spot_v1_public_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"errors"
"log"
"os"
"os/signal"
"time"
Expand Down Expand Up @@ -193,7 +192,7 @@ func (s *SpotWebsocketV1PublicV1Service) Start(ctx context.Context) {
if IsErrWebsocketClosed(err) {
return
}
log.Println(err)
logger.Println(err)
return
}
}
Expand All @@ -214,7 +213,7 @@ func (s *SpotWebsocketV1PublicV1Service) Start(ctx context.Context) {
return
}
case <-ctx.Done():
log.Println("interrupt")
logger.Println("interrupt")

if err := s.Close(); err != nil {
return
Expand Down
5 changes: 2 additions & 3 deletions ws_spot_v1_public_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"errors"
"log"
"os"
"os/signal"
"time"
Expand Down Expand Up @@ -192,7 +191,7 @@ func (s *SpotWebsocketV1PublicV2Service) Start(ctx context.Context) {
if IsErrWebsocketClosed(err) {
return
}
log.Println(err)
logger.Println(err)
return
}
}
Expand All @@ -213,7 +212,7 @@ func (s *SpotWebsocketV1PublicV2Service) Start(ctx context.Context) {
return
}
case <-ctx.Done():
log.Println("interrupt")
logger.Println("interrupt")

if err := s.Close(); err != nil {
return
Expand Down

0 comments on commit e0f1786

Please sign in to comment.