Skip to content

Commit

Permalink
Implement Logger (thrasher-corp#228)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xtda authored and thrasher- committed Jan 8, 2019
1 parent bfbd496 commit d01e7ba
Show file tree
Hide file tree
Showing 103 changed files with 1,028 additions and 657 deletions.
11 changes: 6 additions & 5 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"hash"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
Expand All @@ -29,6 +28,8 @@ import (
"strconv"
"strings"
"time"

log "github.com/thrasher-/gocryptotrader/logger"
)

// Vars for common.go operations
Expand Down Expand Up @@ -390,7 +391,7 @@ func SendHTTPRequest(method, path string, headers map[string]string, body io.Rea
// on failure.
func SendHTTPGetRequest(url string, jsonDecode, isVerbose bool, result interface{}) error {
if isVerbose {
log.Println("Raw URL: ", url)
log.Debugf("Raw URL: %s", url)
}

initialiseHTTPClient()
Expand All @@ -410,7 +411,7 @@ func SendHTTPGetRequest(url string, jsonDecode, isVerbose bool, result interface
}

if isVerbose {
log.Println("Raw Resp: ", string(contents[:]))
log.Debugf("Raw Resp: %s", string(contents[:]))
}

defer res.Body.Close()
Expand Down Expand Up @@ -639,8 +640,8 @@ func CheckDir(dir string, create bool) error {
return fmt.Errorf("directory %s does not exist. Err: %s", dir, err)
}

log.Printf("Directory %s does not exist.. creating.", dir)
err = os.Mkdir(dir, 0777)
log.Warnf("Directory %s does not exist.. creating.", dir)
err = os.MkdirAll(dir, 0777)
if err != nil {
return fmt.Errorf("failed to create dir. Err: %s", err)
}
Expand Down
10 changes: 5 additions & 5 deletions communications/base/base_interface.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package base

import (
"log"
"time"

"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
log "github.com/thrasher-/gocryptotrader/logger"
)

// IComm is the main interface array across the communication packages
Expand All @@ -33,7 +33,7 @@ func (c IComm) Setup() {
if c[i].IsEnabled() && !c[i].IsConnected() {
err := c[i].Connect()
if err != nil {
log.Printf("Communications: %s failed to connect. Err: %s", c[i].GetName(), err)
log.Errorf("Communications: %s failed to connect. Err: %s", c[i].GetName(), err)
}
}
}
Expand All @@ -45,7 +45,7 @@ func (c IComm) PushEvent(event Event) {
if c[i].IsEnabled() && c[i].IsConnected() {
err := c[i].PushEvent(event)
if err != nil {
log.Printf("Communications error - PushEvent() in package %s with %v",
log.Errorf("Communications error - PushEvent() in package %s with %v",
c[i].GetName(), event)
}
}
Expand All @@ -58,12 +58,12 @@ func (c IComm) GetEnabledCommunicationMediums() {
var count int
for i := range c {
if c[i].IsEnabled() && c[i].IsConnected() {
log.Printf("Communications: Medium %s is enabled.", c[i].GetName())
log.Debugf("Communications: Medium %s is enabled.", c[i].GetName())
count++
}
}
if count == 0 {
log.Println("Communications: No communication mediums are enabled.")
log.Warnf("Communications: No communication mediums are enabled.")
}
}

Expand Down
28 changes: 14 additions & 14 deletions communications/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"sync"
"time"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/communications/base"
"github.com/thrasher-/gocryptotrader/config"
log "github.com/thrasher-/gocryptotrader/logger"
)

// const declares main slack url and commands that will be supported on client
Expand Down Expand Up @@ -154,13 +154,13 @@ func (s *Slack) NewConnection() error {
}

if s.Verbose {
log.Printf("%s [%s] connected to %s [%s] \nWebsocket URL: %s.\n",
log.Debugf("%s [%s] connected to %s [%s] \nWebsocket URL: %s.\n",
s.Details.Self.Name,
s.Details.Self.ID,
s.Details.Team.Domain,
s.Details.Team.ID,
s.Details.URL)
log.Printf("Slack channels: %s", s.GetChannelsString())
log.Debugf("Slack channels: %s", s.GetChannelsString())
}

s.TargetChannelID, err = s.GetIDByName(s.TargetChannel)
Expand Down Expand Up @@ -197,14 +197,14 @@ func (s *Slack) WebsocketReader() {
for {
_, resp, err := s.WebsocketConn.ReadMessage()
if err != nil {
log.Fatal(err)
log.Error(err)
}

var data WebsocketResponse

err = common.JSONDecode(resp, &data)
if err != nil {
log.Println(err)
log.Error(err)
continue
}

Expand Down Expand Up @@ -239,10 +239,10 @@ func (s *Slack) WebsocketReader() {

case "pong":
if s.Verbose {
log.Println("Pong received from server")
log.Debugf("Pong received from server")
}
default:
log.Println(string(resp))
log.Debugf(string(resp))
}
}
}
Expand All @@ -254,7 +254,7 @@ func (s *Slack) handlePresenceChange(resp []byte) error {
return err
}
if s.Verbose {
log.Printf("Presence change. User %s [%s] changed status to %s\n",
log.Debugf("Presence change. User %s [%s] changed status to %s\n",
s.GetUsernameByID(pres.User),
pres.User, pres.Presence)
}
Expand All @@ -271,7 +271,7 @@ func (s *Slack) handleMessageResponse(resp []byte, data WebsocketResponse) error
return err
}
if s.Verbose {
log.Printf("Msg received by %s [%s] with text: %s\n",
log.Debugf("Msg received by %s [%s] with text: %s\n",
s.GetUsernameByID(msg.User),
msg.User, msg.Text)
}
Expand All @@ -283,15 +283,15 @@ func (s *Slack) handleMessageResponse(resp []byte, data WebsocketResponse) error
func (s *Slack) handleErrorResponse(data WebsocketResponse) error {
if data.Error.Msg == "Socket URL has expired" {
if s.Verbose {
log.Println("Slack websocket URL has expired.. Reconnecting")
log.Debugf("Slack websocket URL has expired.. Reconnecting")
}

if s.WebsocketConn == nil {
return errors.New("Websocket connection is nil")
}

if err := s.WebsocketConn.Close(); err != nil {
log.Println(err)
log.Error(err)
}

s.ReconnectURL = ""
Expand All @@ -303,7 +303,7 @@ func (s *Slack) handleErrorResponse(data WebsocketResponse) error {

func (s *Slack) handleHelloResponse(data WebsocketResponse) {
if s.Verbose {
log.Println("Websocket connected successfully.")
log.Debugln("Websocket connected successfully.")
}
s.Connected = true
go s.WebsocketKeepAlive()
Expand All @@ -320,7 +320,7 @@ func (s *Slack) handleReconnectResponse(resp []byte) error {
}
s.ReconnectURL = recURL.URL
if s.Verbose {
log.Printf("Reconnect URL set to %s\n", s.ReconnectURL)
log.Debugf("Reconnect URL set to %s\n", s.ReconnectURL)
}
return nil
}
Expand All @@ -332,7 +332,7 @@ func (s *Slack) WebsocketKeepAlive() {
for {
<-ticker.C
if err := s.WebsocketSend("ping", ""); err != nil {
log.Println("slack WebsocketKeepAlive() error", err)
log.Debugf("slack WebsocketKeepAlive() error %s", err)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions communications/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"bytes"
"errors"
"fmt"
"log"

"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/communications/base"
"github.com/thrasher-/gocryptotrader/config"
log "github.com/thrasher-/gocryptotrader/logger"
)

const (
Expand Down Expand Up @@ -87,15 +87,15 @@ func (t *Telegram) PollerStart() {
for {
resp, err := t.GetUpdates()
if err != nil {
log.Fatal(err)
log.Error(err)
}

for i := range resp.Result {
if resp.Result[i].UpdateID > t.Offset {
if string(resp.Result[i].Message.Text[0]) == "/" {
err = t.HandleMessages(resp.Result[i].Message.Text, resp.Result[i].Message.From.ID)
if err != nil {
log.Fatal(err)
log.Error(err)
}
}
t.Offset = resp.Result[i].UpdateID
Expand Down
Loading

0 comments on commit d01e7ba

Please sign in to comment.