mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-06 23:16:53 +00:00
Implement Logger (#228)
* Added new base logger * updated example and test configs * updated exchange helpers restful router & server * logPath is now passed to the logger to remove dependency on common package * updated everything besides exchanges to use new logger * alphapoint to bitmex done * updated bitmex bitstamp bittrex btcc and also performance changes to logger * btcmarkets coinbase coinut exmo gateio wrappers updated * gateio and gemini logger updated * hitbtc huobi itbit & kraken updated * All exchanges updatd * return correct error for disabled websocket * don't disconnect client on invalid json * updated router internal logging * log.Fatal to t.Error for tests * Changed from fatal to error failure to set maxprocs * output ANSI codes for everything but windows for now due to lack of windows support * added error handling to logger and unit tests * clear wording on print -> log.print * added benchmark test * cleaned up import sections * Updated logger based on PR requests (added default config options on failure/setting errors) * ah this should fix travici enc config issue * Load entire config and clear out logging to hopefully fix travisci issue * wording & test error handling * fixed formatting issues based on feedback * fixed formatting issues based on feedback * changed CheckDir to use mkdirall instead of mkdir and other changes based on feedback
This commit is contained in:
77
routines.go
77
routines.go
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -15,12 +14,13 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/stats"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
log "github.com/thrasher-/gocryptotrader/logger"
|
||||
)
|
||||
|
||||
func printCurrencyFormat(price float64) string {
|
||||
displaySymbol, err := symbol.GetSymbolByCurrencyName(bot.config.Currency.FiatDisplayCurrency)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get display symbol: %s", err)
|
||||
log.Errorf("Failed to get display symbol: %s", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%.8f", displaySymbol, price)
|
||||
@@ -30,17 +30,17 @@ func printConvertCurrencyFormat(origCurrency string, origPrice float64) string {
|
||||
displayCurrency := bot.config.Currency.FiatDisplayCurrency
|
||||
conv, err := currency.ConvertCurrency(origPrice, origCurrency, displayCurrency)
|
||||
if err != nil {
|
||||
log.Printf("Failed to convert currency: %s", err)
|
||||
log.Errorf("Failed to convert currency: %s", err)
|
||||
}
|
||||
|
||||
displaySymbol, err := symbol.GetSymbolByCurrencyName(displayCurrency)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get display symbol: %s", err)
|
||||
log.Errorf("Failed to get display symbol: %s", err)
|
||||
}
|
||||
|
||||
origSymbol, err := symbol.GetSymbolByCurrencyName(origCurrency)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get original currency symbol: %s", err)
|
||||
log.Errorf("Failed to get original currency symbol: %s", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%.2f %s (%s%.2f %s)",
|
||||
@@ -55,7 +55,7 @@ func printConvertCurrencyFormat(origCurrency string, origPrice float64) string {
|
||||
|
||||
func printTickerSummary(result ticker.Price, p pair.CurrencyPair, assetType, exchangeName string, err error) {
|
||||
if err != nil {
|
||||
log.Printf("Failed to get %s %s ticker. Error: %s",
|
||||
log.Errorf("Failed to get %s %s ticker. Error: %s",
|
||||
p.Pair().String(),
|
||||
exchangeName,
|
||||
err)
|
||||
@@ -65,7 +65,7 @@ func printTickerSummary(result ticker.Price, p pair.CurrencyPair, assetType, exc
|
||||
stats.Add(exchangeName, p, assetType, result.Last, result.Volume)
|
||||
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.String() != bot.config.Currency.FiatDisplayCurrency {
|
||||
origCurrency := p.SecondCurrency.Upper().String()
|
||||
log.Printf("%s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f",
|
||||
log.Infof("%s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f",
|
||||
exchangeName,
|
||||
exchange.FormatCurrency(p).String(),
|
||||
assetType,
|
||||
@@ -77,7 +77,7 @@ func printTickerSummary(result ticker.Price, p pair.CurrencyPair, assetType, exc
|
||||
result.Volume)
|
||||
} else {
|
||||
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.Upper().String() == bot.config.Currency.FiatDisplayCurrency {
|
||||
log.Printf("%s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f",
|
||||
log.Infof("%s %s %s: TICKER: Last %s Ask %s Bid %s High %s Low %s Volume %.8f",
|
||||
exchangeName,
|
||||
exchange.FormatCurrency(p).String(),
|
||||
assetType,
|
||||
@@ -88,7 +88,7 @@ func printTickerSummary(result ticker.Price, p pair.CurrencyPair, assetType, exc
|
||||
printCurrencyFormat(result.Low),
|
||||
result.Volume)
|
||||
} else {
|
||||
log.Printf("%s %s %s: TICKER: Last %.8f Ask %.8f Bid %.8f High %.8f Low %.8f Volume %.8f",
|
||||
log.Infof("%s %s %s: TICKER: Last %.8f Ask %.8f Bid %.8f High %.8f Low %.8f Volume %.8f",
|
||||
exchangeName,
|
||||
exchange.FormatCurrency(p).String(),
|
||||
assetType,
|
||||
@@ -104,7 +104,7 @@ func printTickerSummary(result ticker.Price, p pair.CurrencyPair, assetType, exc
|
||||
|
||||
func printOrderbookSummary(result orderbook.Base, p pair.CurrencyPair, assetType, exchangeName string, err error) {
|
||||
if err != nil {
|
||||
log.Printf("Failed to get %s %s orderbook. Error: %s",
|
||||
log.Errorf("Failed to get %s %s orderbook. Error: %s",
|
||||
p.Pair().String(),
|
||||
exchangeName,
|
||||
err)
|
||||
@@ -116,7 +116,7 @@ func printOrderbookSummary(result orderbook.Base, p pair.CurrencyPair, assetType
|
||||
|
||||
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.String() != bot.config.Currency.FiatDisplayCurrency {
|
||||
origCurrency := p.SecondCurrency.Upper().String()
|
||||
log.Printf("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s",
|
||||
log.Infof("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s",
|
||||
exchangeName,
|
||||
exchange.FormatCurrency(p).String(),
|
||||
assetType,
|
||||
@@ -131,7 +131,7 @@ func printOrderbookSummary(result orderbook.Base, p pair.CurrencyPair, assetType
|
||||
)
|
||||
} else {
|
||||
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.Upper().String() == bot.config.Currency.FiatDisplayCurrency {
|
||||
log.Printf("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s",
|
||||
log.Infof("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s",
|
||||
exchangeName,
|
||||
exchange.FormatCurrency(p).String(),
|
||||
assetType,
|
||||
@@ -145,7 +145,7 @@ func printOrderbookSummary(result orderbook.Base, p pair.CurrencyPair, assetType
|
||||
printCurrencyFormat(asksValue),
|
||||
)
|
||||
} else {
|
||||
log.Printf("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %f Asks len: %d Amount: %f %s. Total value: %f",
|
||||
log.Infof("%s %s %s: ORDERBOOK: Bids len: %d Amount: %f %s. Total value: %f Asks len: %d Amount: %f %s. Total value: %f",
|
||||
exchangeName,
|
||||
exchange.FormatCurrency(p).String(),
|
||||
assetType,
|
||||
@@ -171,15 +171,15 @@ func relayWebsocketEvent(result interface{}, event, assetType, exchangeName stri
|
||||
}
|
||||
err := BroadcastWebsocketMessage(evt)
|
||||
if err != nil {
|
||||
log.Println(fmt.Errorf("Failed to broadcast websocket event. Error: %s",
|
||||
err))
|
||||
log.Errorf("Failed to broadcast websocket event. Error: %s",
|
||||
err)
|
||||
}
|
||||
}
|
||||
|
||||
// TickerUpdaterRoutine fetches and updates the ticker for all enabled
|
||||
// currency pairs and exchanges
|
||||
func TickerUpdaterRoutine() {
|
||||
log.Println("Starting ticker updater routine.")
|
||||
log.Debugf("Starting ticker updater routine.")
|
||||
var wg sync.WaitGroup
|
||||
for {
|
||||
wg.Add(len(bot.exchanges))
|
||||
@@ -194,7 +194,7 @@ func TickerUpdaterRoutine() {
|
||||
supportsBatching := bot.exchanges[x].SupportsRESTTickerBatchUpdates()
|
||||
assetTypes, err := exchange.GetExchangeAssetTypes(exchangeName)
|
||||
if err != nil {
|
||||
log.Printf("failed to get %s exchange asset types. Error: %s",
|
||||
log.Debugf("failed to get %s exchange asset types. Error: %s",
|
||||
exchangeName, err)
|
||||
return
|
||||
}
|
||||
@@ -228,7 +228,7 @@ func TickerUpdaterRoutine() {
|
||||
}(x, &wg)
|
||||
}
|
||||
wg.Wait()
|
||||
log.Println("All enabled currency tickers fetched.")
|
||||
log.Debugln("All enabled currency tickers fetched.")
|
||||
time.Sleep(time.Second * 10)
|
||||
}
|
||||
}
|
||||
@@ -236,7 +236,7 @@ func TickerUpdaterRoutine() {
|
||||
// OrderbookUpdaterRoutine fetches and updates the orderbooks for all enabled
|
||||
// currency pairs and exchanges
|
||||
func OrderbookUpdaterRoutine() {
|
||||
log.Println("Starting orderbook updater routine.")
|
||||
log.Debugln("Starting orderbook updater routine.")
|
||||
var wg sync.WaitGroup
|
||||
for {
|
||||
wg.Add(len(bot.exchanges))
|
||||
@@ -251,7 +251,7 @@ func OrderbookUpdaterRoutine() {
|
||||
enabledCurrencies := bot.exchanges[x].GetEnabledCurrencies()
|
||||
assetTypes, err := exchange.GetExchangeAssetTypes(exchangeName)
|
||||
if err != nil {
|
||||
log.Printf("failed to get %s exchange asset types. Error: %s",
|
||||
log.Errorf("failed to get %s exchange asset types. Error: %s",
|
||||
exchangeName, err)
|
||||
return
|
||||
}
|
||||
@@ -275,19 +275,19 @@ func OrderbookUpdaterRoutine() {
|
||||
}(x, &wg)
|
||||
}
|
||||
wg.Wait()
|
||||
log.Println("All enabled currency orderbooks fetched.")
|
||||
log.Debugln("All enabled currency orderbooks fetched.")
|
||||
time.Sleep(time.Second * 10)
|
||||
}
|
||||
}
|
||||
|
||||
// WebsocketRoutine Initial routine management system for websocket
|
||||
func WebsocketRoutine(verbose bool) {
|
||||
log.Println("Connecting exchange websocket services...")
|
||||
log.Debugln("Connecting exchange websocket services...")
|
||||
|
||||
for i := range bot.exchanges {
|
||||
go func(i int) {
|
||||
if verbose {
|
||||
log.Printf("Establishing websocket connection for %s",
|
||||
log.Debugf("Establishing websocket connection for %s",
|
||||
bot.exchanges[i].GetName())
|
||||
}
|
||||
|
||||
@@ -303,9 +303,9 @@ func WebsocketRoutine(verbose bool) {
|
||||
if err != nil {
|
||||
switch err.Error() {
|
||||
case exchange.WebsocketNotEnabled:
|
||||
// Store in memory if enabled in future
|
||||
log.Warnf("%s - websocket disabled", bot.exchanges[i].GetName())
|
||||
default:
|
||||
log.Println(err)
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
}(i)
|
||||
@@ -320,7 +320,7 @@ var wg sync.WaitGroup
|
||||
func Websocketshutdown(ws *exchange.Websocket) error {
|
||||
err := ws.Shutdown() // shutdown routines on the exchange
|
||||
if err != nil {
|
||||
log.Fatalf("routines.go error - failed to shutodwn %s", err)
|
||||
log.Errorf("routines.go error - failed to shutodwn %s", err)
|
||||
}
|
||||
|
||||
timer := time.NewTimer(5 * time.Second)
|
||||
@@ -354,12 +354,12 @@ func streamDiversion(ws *exchange.Websocket, verbose bool) {
|
||||
|
||||
case <-ws.Connected:
|
||||
if verbose {
|
||||
log.Printf("exchange %s websocket feed connected", ws.GetName())
|
||||
log.Debugf("exchange %s websocket feed connected", ws.GetName())
|
||||
}
|
||||
|
||||
case <-ws.Disconnected:
|
||||
if verbose {
|
||||
log.Printf("exchange %s websocket feed disconnected, switching to REST functionality",
|
||||
log.Debugf("exchange %s websocket feed disconnected, switching to REST functionality",
|
||||
ws.GetName())
|
||||
}
|
||||
}
|
||||
@@ -385,12 +385,12 @@ func WebsocketDataHandler(ws *exchange.Websocket, verbose bool) {
|
||||
switch data.(string) {
|
||||
case exchange.WebsocketNotEnabled:
|
||||
if verbose {
|
||||
log.Printf("routines.go warning - exchange %s weboscket not enabled",
|
||||
log.Warnf("routines.go warning - exchange %s weboscket not enabled",
|
||||
ws.GetName())
|
||||
}
|
||||
|
||||
default:
|
||||
log.Println(data.(string))
|
||||
log.Infof(data.(string))
|
||||
}
|
||||
|
||||
case error:
|
||||
@@ -399,33 +399,33 @@ func WebsocketDataHandler(ws *exchange.Websocket, verbose bool) {
|
||||
go WebsocketReconnect(ws, verbose)
|
||||
continue
|
||||
default:
|
||||
log.Fatalf("routines.go exchange %s websocket error - %s", ws.GetName(), data)
|
||||
log.Errorf("routines.go exchange %s websocket error - %s", ws.GetName(), data)
|
||||
}
|
||||
|
||||
case exchange.TradeData:
|
||||
// Trade Data
|
||||
if verbose {
|
||||
log.Println("Websocket trades Updated: ", data.(exchange.TradeData))
|
||||
log.Infoln("Websocket trades Updated: ", data.(exchange.TradeData))
|
||||
}
|
||||
|
||||
case exchange.TickerData:
|
||||
// Ticker data
|
||||
if verbose {
|
||||
log.Println("Websocket Ticker Updated: ", data.(exchange.TickerData))
|
||||
log.Infoln("Websocket Ticker Updated: ", data.(exchange.TickerData))
|
||||
}
|
||||
case exchange.KlineData:
|
||||
// Kline data
|
||||
if verbose {
|
||||
log.Println("Websocket Kline Updated: ", data.(exchange.KlineData))
|
||||
log.Infoln("Websocket Kline Updated: ", data.(exchange.KlineData))
|
||||
}
|
||||
case exchange.WebsocketOrderbookUpdate:
|
||||
// Orderbook data
|
||||
if verbose {
|
||||
log.Println("Websocket Orderbook Updated:", data.(exchange.WebsocketOrderbookUpdate))
|
||||
log.Infoln("Websocket Orderbook Updated:", data.(exchange.WebsocketOrderbookUpdate))
|
||||
}
|
||||
default:
|
||||
if verbose {
|
||||
log.Println("Websocket Unknown type: ", data)
|
||||
log.Warnf("Websocket Unknown type: %s", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -435,12 +435,13 @@ func WebsocketDataHandler(ws *exchange.Websocket, verbose bool) {
|
||||
// WebsocketReconnect tries to reconnect to a websocket stream
|
||||
func WebsocketReconnect(ws *exchange.Websocket, verbose bool) {
|
||||
if verbose {
|
||||
log.Printf("Websocket reconnection requested for %s", ws.GetName())
|
||||
log.Debugf("Websocket reconnection requested for %s", ws.GetName())
|
||||
}
|
||||
|
||||
err := ws.Shutdown()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
Reference in New Issue
Block a user