From b2342c194ed21c6fd0b99f31e617b534e7ac615c Mon Sep 17 00:00:00 2001 From: Mohit Kumbhare Date: Sun, 25 May 2025 14:10:38 +0530 Subject: [PATCH 1/2] FIX : change fmt.Println into a debug level logger #113 --- go.mod | 1 + go.sum | 6 ++++++ ticker/ticker.go | 12 ++++++------ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 0fedf28..aa05837 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/gocarina/gocsv v0.0.0-20180809181117-b8c38cb1ba36 github.com/google/go-querystring v1.0.0 github.com/gorilla/websocket v1.4.2 + github.com/sirupsen/logrus v1.9.3 // indirect github.com/stretchr/testify v1.7.0 gopkg.in/jarcoal/httpmock.v1 v1.0.0-20180719183105-8007e27cdb32 ) diff --git a/go.sum b/go.sum index ff68e5a..81d4bf4 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/gocarina/gocsv v0.0.0-20180809181117-b8c38cb1ba36 h1:IlBbYij72r3CoD3fKTbP5jD0NJjrvemKsaxkW/QUdGE= github.com/gocarina/gocsv v0.0.0-20180809181117-b8c38cb1ba36/go.mod h1:/oj50ZdPq/cUjA02lMZhijk5kR31SEydKyqah1OgBuo= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= @@ -8,9 +10,13 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 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= +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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/jarcoal/httpmock.v1 v1.0.0-20180719183105-8007e27cdb32 h1:30DLrQoRqdUHslVMzxuKUnY4GKJGk1/FJtKy3yx4TKE= diff --git a/ticker/ticker.go b/ticker/ticker.go index 23206e1..bc2ce6b 100644 --- a/ticker/ticker.go +++ b/ticker/ticker.go @@ -12,6 +12,8 @@ import ( "sync/atomic" "time" + log "github.com/sirupsen/logrus" + "github.com/gorilla/websocket" kiteconnect "github.com/zerodha/gokiteconnect/v4" "github.com/zerodha/gokiteconnect/v4/models" @@ -47,12 +49,12 @@ type Ticker struct { type atomicTime struct { v atomic.Value } - + // Get returns the current timestamp. func (b *atomicTime) Get() time.Time { return b.v.Load().(time.Time) } - + // Set sets the current timestamp. func (b *atomicTime) Set(value time.Time) { b.v.Store(value) @@ -356,7 +358,6 @@ func (t *Ticker) handleClose(code int, reason string) error { return nil } - // Trigger callback methods func (t *Ticker) triggerError(err error) { if t.callbacks.onError != nil { @@ -388,7 +389,6 @@ func (t *Ticker) triggerNoReconnect(attempt int) { } } - func (t *Ticker) triggerMessage(messageType int, message []byte) { if t.callbacks.onMessage != nil { t.callbacks.onMessage(messageType, message) @@ -553,6 +553,7 @@ func (t *Ticker) SetMode(mode Mode, tokens []uint32) error { // Resubscribe resubscribes to the current stored subscriptions func (t *Ticker) Resubscribe() error { + log.SetLevel(log.DebugLevel) var tokens []uint32 modes := map[Mode][]uint32{ ModeFull: []uint32{}, @@ -568,7 +569,7 @@ func (t *Ticker) Resubscribe() error { } } - fmt.Println("Subscribe again: ", tokens, t.subscribedTokens) + log.Debugf("Subscribe again: %v %v", tokens, t.subscribedTokens) // Subscribe to tokens if len(tokens) > 0 { @@ -776,4 +777,3 @@ func convertPrice(seg uint32, val float64) float64 { return val / 100.0 } } - From f123faecee6fe9b332d99b6d6187550ccf6566d1 Mon Sep 17 00:00:00 2001 From: Mohit Kumbhare Date: Sun, 25 May 2025 14:59:19 +0530 Subject: [PATCH 2/2] Added Debug Level Log : Replace fmt.Print with structured logging in ticker.go #114 --- ticker/ticker.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ticker/ticker.go b/ticker/ticker.go index bc2ce6b..5069f2e 100644 --- a/ticker/ticker.go +++ b/ticker/ticker.go @@ -6,14 +6,14 @@ import ( "encoding/binary" "encoding/json" "fmt" + "log" "math" "net/url" + "os" "sync" "sync/atomic" "time" - log "github.com/sirupsen/logrus" - "github.com/gorilla/websocket" kiteconnect "github.com/zerodha/gokiteconnect/v4" "github.com/zerodha/gokiteconnect/v4/models" @@ -41,7 +41,8 @@ type Ticker struct { subscribedTokens map[uint32]Mode - cancel context.CancelFunc + cancel context.CancelFunc + debugLog *log.Logger } // atomicTime is wrapper over time.Time to safely access @@ -167,6 +168,7 @@ func New(apiKey string, accessToken string) *Ticker { reconnectMaxRetries: defaultReconnectMaxAttempts, connectTimeout: defaultConnectTimeout, subscribedTokens: map[uint32]Mode{}, + debugLog: log.New(os.Stdout, "DEBUG : ", log.Ldate|log.Ltime|log.Lshortfile), } return ticker @@ -553,7 +555,6 @@ func (t *Ticker) SetMode(mode Mode, tokens []uint32) error { // Resubscribe resubscribes to the current stored subscriptions func (t *Ticker) Resubscribe() error { - log.SetLevel(log.DebugLevel) var tokens []uint32 modes := map[Mode][]uint32{ ModeFull: []uint32{}, @@ -569,7 +570,7 @@ func (t *Ticker) Resubscribe() error { } } - log.Debugf("Subscribe again: %v %v", tokens, t.subscribedTokens) + t.debugLog.Println("Subscribe again: ", tokens, t.subscribedTokens) // Subscribe to tokens if len(tokens) > 0 {