From e0f178684dc4d1c22d2c4019fcd2e60eb49eac73 Mon Sep 17 00:00:00 2001 From: Ivan Korotkov Date: Sat, 13 Jan 2024 03:34:19 +0100 Subject: [PATCH] fix: use noop logger by default (#159) --- client_web_socket.go | 5 ++--- logger.go | 26 ++++++++++++++++++++++++++ v5_market_service.go | 3 +-- v5_ws_private.go | 3 +-- v5_ws_public.go | 3 +-- ws_spot_v1_private.go | 5 ++--- ws_spot_v1_public_v1.go | 5 ++--- ws_spot_v1_public_v2.go | 5 ++--- 8 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 logger.go diff --git a/client_web_socket.go b/client_web_socket.go index 30fc848..553affa 100644 --- a/client_web_socket.go +++ b/client_web_socket.go @@ -7,7 +7,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "log" "os" "os/signal" "time" @@ -100,7 +99,7 @@ func (c *WebSocketClient) Start(ctx context.Context, executors []WebsocketExecut if IsErrWebsocketClosed(err) { return } - log.Println(err) + logger.Println(err) return } } @@ -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 { diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..0a4c7e1 --- /dev/null +++ b/logger.go @@ -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) +} diff --git a/v5_market_service.go b/v5_market_service.go index 4f4fb99..38a320d 100644 --- a/v5_market_service.go +++ b/v5_market_service.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "log" "github.com/google/go-querystring/query" ) @@ -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), diff --git a/v5_ws_private.go b/v5_ws_private.go index 3fbd5b8..8852cd3 100644 --- a/v5_ws_private.go +++ b/v5_ws_private.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "log" "os" "os/signal" "sync" @@ -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 diff --git a/v5_ws_public.go b/v5_ws_public.go index 931fd4d..fcb4ce4 100644 --- a/v5_ws_public.go +++ b/v5_ws_public.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "log" "os" "os/signal" "strings" @@ -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 diff --git a/ws_spot_v1_private.go b/ws_spot_v1_private.go index a2fbc2a..ba497c6 100644 --- a/ws_spot_v1_private.go +++ b/ws_spot_v1_private.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "log" "os" "os/signal" "time" @@ -190,7 +189,7 @@ func (s *SpotWebsocketV1PrivateService) Start(ctx context.Context) { if IsErrWebsocketClosed(err) { return } - log.Println(err) + logger.Println(err) return } } @@ -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 diff --git a/ws_spot_v1_public_v1.go b/ws_spot_v1_public_v1.go index fb77ed7..4936624 100644 --- a/ws_spot_v1_public_v1.go +++ b/ws_spot_v1_public_v1.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "log" "os" "os/signal" "time" @@ -193,7 +192,7 @@ func (s *SpotWebsocketV1PublicV1Service) Start(ctx context.Context) { if IsErrWebsocketClosed(err) { return } - log.Println(err) + logger.Println(err) return } } @@ -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 diff --git a/ws_spot_v1_public_v2.go b/ws_spot_v1_public_v2.go index 7944f6d..8491223 100644 --- a/ws_spot_v1_public_v2.go +++ b/ws_spot_v1_public_v2.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "log" "os" "os/signal" "time" @@ -192,7 +191,7 @@ func (s *SpotWebsocketV1PublicV2Service) Start(ctx context.Context) { if IsErrWebsocketClosed(err) { return } - log.Println(err) + logger.Println(err) return } } @@ -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