github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/v2/ws-custom-logger/main.go (about) 1 package main 2 3 import ( 4 "context" 5 _ "net/http/pprof" 6 "os" 7 8 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/trade" 9 "github.com/bitfinexcom/bitfinex-api-go/v2/websocket" 10 "github.com/op/go-logging" 11 ) 12 13 func main() { 14 // create a new go-logger instance 15 var log = logging.MustGetLogger("bfx-websocket") 16 // create string formatter 17 var format = logging.MustStringFormatter( 18 `%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`, 19 ) 20 // apply to logging instance 21 backend := logging.NewLogBackend(os.Stderr, "", 0) 22 backendFormatter := logging.NewBackendFormatter(backend, format) 23 logging.SetBackend(backendFormatter) 24 25 // create websocket client and pass logger 26 p := websocket.NewDefaultParameters() 27 p.Logger = log 28 client := websocket.NewWithParams(p) 29 err := client.Connect() 30 if err != nil { 31 log.Errorf("could not connect: %s", err.Error()) 32 return 33 } 34 35 for obj := range client.Listen() { 36 switch obj.(type) { 37 case error: 38 log.Errorf("channel closed: %s", obj) 39 return 40 case *trade.Trade: 41 log.Infof("New trade: %+v\n", obj) 42 case *websocket.InfoEvent: 43 // Info event confirms connection to the bfx websocket 44 log.Info("Subscribing to tBTCUSD") 45 _, err := client.SubscribeTrades(context.Background(), "tBTCUSD") 46 if err != nil { 47 log.Infof("could not subscribe to trades: %s", err.Error()) 48 } 49 default: 50 log.Infof("MSG RECV: %#v", obj) 51 } 52 } 53 }