Skip to content

Commit

Permalink
New logging system (thrasher-corp#319)
Browse files Browse the repository at this point in the history
* First pass at adding new logging system

* NewLogger

* NewLogger

* WIP

* silly bug fix

* :D removed files

* removed old logging interface

* added tests

* added tests

* Started to add new lines to all f calls

* Added subsystem log types

* Logger improvements

* Further performance improvements

* changes to logger and sublogger creation

* Renamed Logging types

* removed old print statement

* changes based on feedback

* moved sublogger types to own file

* :)

* added console as output type

* added get level command

* added get/set log level via grpc command

* added check for output being empty for migration support

* first pass at log rotation

* added log rotation

* :D derp fixed

* added tests

* changes based on feedback

* changed log type

* comments

* renamed file -> fileSettings

* typo fix

* changes based on feedback

* gofmt ran on additional files

* gofmt ran on additional files
  • Loading branch information
xtda authored and thrasher- committed Jul 6, 2019
1 parent 7112a89 commit 3de1d94
Show file tree
Hide file tree
Showing 137 changed files with 2,900 additions and 1,630 deletions.
6 changes: 3 additions & 3 deletions cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func ({{.Variable}} *{{.CapitalName}}) Start(wg *sync.WaitGroup) {
// Run implements the {{.CapitalName}} wrapper
func ({{.Variable}} *{{.CapitalName}}) Run() {
if {{.Variable}}.Verbose {
{{if .WS}} log.Debugf("%s Websocket: %s. (url: %s).\n", {{.Variable}}.GetName(), common.IsEnabled({{.Variable}}.Websocket.IsEnabled()), {{.Variable}}.Websocket.GetWebsocketURL()) {{end}}
log.Debugf("%s polling delay: %ds.\n", {{.Variable}}.GetName(), {{.Variable}}.RESTPollingDelay)
log.Debugf("%s %d currencies enabled: %s.\n", {{.Variable}}.GetName(), len({{.Variable}}.EnabledPairs), {{.Variable}}.EnabledPairs)
{{if .WS}} log.Debugf(log.ExchangeSys, "%s Websocket: %s. (url: %s).\n", {{.Variable}}.GetName(), common.IsEnabled({{.Variable}}.Websocket.IsEnabled()), {{.Variable}}.Websocket.GetWebsocketURL()) {{end}}
log.Debugf(log.ExchangeSys, "%s polling delay: %ds.\n", {{.Variable}}.GetName(), {{.Variable}}.RESTPollingDelay)
log.Debugf(log.ExchangeSys, "%s %d currencies enabled: %s.\n", {{.Variable}}.GetName(), len({{.Variable}}.EnabledPairs), {{.Variable}}.EnabledPairs)
}
}

Expand Down
101 changes: 101 additions & 0 deletions cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1948,3 +1948,104 @@ var withdrawFiatFundsCommand = cli.Command{
func withdrawFiatFunds(_ *cli.Context) error {
return common.ErrNotYetImplemented
}

var getLoggerDetailsCommand = cli.Command{
Name: "getloggerdetails",
Action: getLoggerDetails,
Flags: []cli.Flag{
cli.StringFlag{
Name: "logger",
Usage: "logger to get level details of",
},
},
}

func getLoggerDetails(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
cli.ShowCommandHelp(c, "getloggerdetails")
return nil
}

var logger string
if c.IsSet("logger") {
logger = c.String("logger")
} else {
logger = c.Args().First()
}

conn, err := setupClient()
if err != nil {
return err
}
defer conn.Close()

client := gctrpc.NewGoCryptoTraderClient(conn)

result, err := client.GetLoggerDetails(context.Background(),
&gctrpc.GetLoggerDetailsRequest{
Logger: logger,
},
)
if err != nil {
return err
}
jsonOutput(result)
return nil
}

var setLoggerDetailsCommand = cli.Command{
Name: "setloggerdetails",
Action: setLoggerDetails,
Flags: []cli.Flag{
cli.StringFlag{
Name: "logger",
Usage: "logger to get level details of",
},
cli.StringFlag{
Name: "flags",
Usage: "pipe separated value of loggers e.g INFO|WARN",
},
},
}

func setLoggerDetails(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
cli.ShowCommandHelp(c, "setloggerdetails")
return nil
}

var logger string
var level string

if c.IsSet("logger") {
logger = c.String("logger")
} else {
logger = c.Args().First()
}

if c.IsSet("level") {
level = c.String("level")
} else {
level = c.Args().Get(1)
}

conn, err := setupClient()
if err != nil {
return err
}
defer conn.Close()

client := gctrpc.NewGoCryptoTraderClient(conn)

result, err := client.SetLoggerDetails(context.Background(),
&gctrpc.SetLoggerDetailsRequest{
Logger: logger,
Level: level,
},
)
if err != nil {
return err
}
jsonOutput(result)
return nil
}
2 changes: 2 additions & 0 deletions cmd/gctcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func main() {
getCryptocurrencyDepositAddressCommand,
withdrawCryptocurrencyFundsCommand,
withdrawFiatFundsCommand,
getLoggerDetailsCommand,
setLoggerDetailsCommand,
}

err := app.Run(os.Args)
Expand Down
14 changes: 9 additions & 5 deletions cmd/portfolio/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"flag"
"fmt"
"log"
"os"

"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency"
"github.com/thrasher-/gocryptotrader/exchanges/bitfinex"
log "github.com/thrasher-/gocryptotrader/logger"
"github.com/thrasher-/gocryptotrader/portfolio"
)

Expand All @@ -25,7 +26,7 @@ func printSummary(msg string, amount float64) {
currency.USD,
displayCurrency)
if err != nil {
log.Error(err)
log.Println(err)
} else {
symb, err := currency.GetSymbolByCurrencyName(displayCurrency)
if err != nil {
Expand Down Expand Up @@ -64,7 +65,8 @@ func main() {

defaultCfg, err := config.GetFilePath("")
if err != nil {
log.Fatal(err)
log.Println(err)
os.Exit(1)
}

flag.StringVar(&inFile, "infile", defaultCfg, "The config input file to process.")
Expand All @@ -76,7 +78,8 @@ func main() {
var cfg config.Config
err = cfg.LoadConfig(inFile)
if err != nil {
log.Fatal(err)
log.Println(err)
os.Exit(1)
}
log.Println("Loaded config file.")

Expand Down Expand Up @@ -105,7 +108,8 @@ func main() {
}
err = currency.SeedForeignExchangeData(fiatCurrencies)
if err != nil {
log.Fatal(err)
log.Println(err)
os.Exit(1)
}

log.Println("Fetched currency data.")
Expand Down
4 changes: 2 additions & 2 deletions cmd/websocket_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type WebsocketEventResponse struct {
// WebsocketOrderbookTickerRequest is a struct used for ticker and orderbook
// requests
type WebsocketOrderbookTickerRequest struct {
Exchange string `json:"exchangeName"`
Currency string `json:"currency"`
Exchange string `json:"exchangeName"`
Currency string `json:"currency"`
AssetType asset.Item `json:"assetType"`
}

Expand Down
8 changes: 4 additions & 4 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func SendHTTPRequest(method, urlPath string, headers map[string]string, body io.
// on failure.
func SendHTTPGetRequest(urlPath string, jsonDecode, isVerbose bool, result interface{}) error {
if isVerbose {
log.Debugf("Raw URL: %s", urlPath)
log.Debugf(log.Global, "Raw URL: %s\n", urlPath)
}

initialiseHTTPClient()
Expand All @@ -221,7 +221,7 @@ func SendHTTPGetRequest(urlPath string, jsonDecode, isVerbose bool, result inter
}

if isVerbose {
log.Debugf("Raw Resp: %s", string(contents))
log.Debugf(log.Global, "Raw Resp: %s\n", string(contents))
}

defer res.Body.Close()
Expand Down Expand Up @@ -336,7 +336,7 @@ func GetDefaultDataDir(env string) string {

dir, err := os.UserHomeDir()
if err != nil {
log.Warn("Environment variable unset, defaulting to current directory")
log.Warnln(log.Global, "Environment variable unset, defaulting to current directory")
dir = "."
}
return filepath.Join(dir, ".gocryptotrader")
Expand All @@ -349,7 +349,7 @@ func CreateDir(dir string) error {
return nil
}

log.Warnf("Directory %s does not exist.. creating.", dir)
log.Warnf(log.Global, "Directory %s does not exist.. creating.\n", dir)
return os.MkdirAll(dir, 0770)
}

Expand Down
8 changes: 4 additions & 4 deletions communications/base/base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func (c IComm) Setup() {
if c[i].IsEnabled() && !c[i].IsConnected() {
err := c[i].Connect()
if err != nil {
log.Errorf("Communications: %s failed to connect. Err: %s", c[i].GetName(), err)
log.Errorf(log.CommunicationMgr, "Communications: %s failed to connect. Err: %s", c[i].GetName(), err)
continue
}
log.Debugf("Communications: %v is enabled and online.", c[i].GetName())
log.Debugf(log.CommunicationMgr, "Communications: %v is enabled and online.", c[i].GetName())
}
}
}
Expand All @@ -43,7 +43,7 @@ func (c IComm) PushEvent(event Event) {
if c[i].IsEnabled() && c[i].IsConnected() {
err := c[i].PushEvent(event)
if err != nil {
log.Errorf("Communications error - PushEvent() in package %s with %v. Err %s",
log.Errorf(log.CommunicationMgr, "Communications error - PushEvent() in package %s with %v. Err %s",
c[i].GetName(), event, err)
}
}
Expand All @@ -69,7 +69,7 @@ func (c IComm) GetEnabledCommunicationMediums() error {
var count int
for i := range c {
if c[i].IsEnabled() && c[i].IsConnected() {
log.Debugf("Communications: Medium %s is enabled.", c[i].GetName())
log.Debugf(log.CommunicationMgr, "Communications: Medium %s is enabled.", c[i].GetName())
count++
}
}
Expand Down
30 changes: 15 additions & 15 deletions communications/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,21 @@ func (s *Slack) NewConnection() error {
}

if s.Verbose {
log.Debugf("Slack: %s [%s] connected to %s [%s] \nWebsocket URL: %s.\n",
log.Debugf(log.CommunicationMgr, "Slack: %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.Debugf("Slack: Public channels: %s", s.GetChannelsString())
log.Debugf(log.CommunicationMgr, "Slack: Public channels: %s\n", s.GetChannelsString())
}

s.TargetChannelID, err = s.GetIDByName(s.TargetChannel)
if err != nil {
return err
}

log.Debugf("Slack: Target channel ID: %v [#%v]", s.TargetChannelID,
log.Debugf(log.CommunicationMgr, "Slack: Target channel ID: %v [#%v]\n", s.TargetChannelID,
s.TargetChannel)
return s.WebsocketConnect()
}
Expand Down Expand Up @@ -208,13 +208,13 @@ func (s *Slack) WebsocketReader() {
for {
_, resp, err := s.WebsocketConn.ReadMessage()
if err != nil {
log.Error(err)
log.Errorln(log.CommunicationMgr, err)
}

var data WebsocketResponse
err = common.JSONDecode(resp, &data)
if err != nil {
log.Error(err)
log.Errorln(log.CommunicationMgr, err)
continue
}

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

case "pong":
if s.Verbose {
log.Debugf("Slack: Pong received from server")
log.Debugln(log.CommunicationMgr, "Slack: Pong received from server")
}
default:
log.Debugf(string(resp))
log.Debugln(log.CommunicationMgr, string(resp))
}
}
}
Expand All @@ -264,7 +264,7 @@ func (s *Slack) handlePresenceChange(resp []byte) error {
return err
}
if s.Verbose {
log.Debugf("Slack: Presence change. User %s [%s] changed status to %s\n",
log.Debugf(log.CommunicationMgr, "Slack: Presence change. User %s [%s] changed status to %s\n",
s.GetUsernameByID(pres.User),
pres.User, pres.Presence)
}
Expand All @@ -281,7 +281,7 @@ func (s *Slack) handleMessageResponse(resp []byte, data WebsocketResponse) error
return err
}
if s.Verbose {
log.Debugf("Slack: Message received by %s [%s] with text: %s\n",
log.Debugf(log.CommunicationMgr, "Slack: Message received by %s [%s] with text: %s\n",
s.GetUsernameByID(msg.User),
msg.User, msg.Text)
}
Expand All @@ -293,15 +293,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.Debugf("Slack websocket URL has expired.. Reconnecting")
log.Debugln(log.CommunicationMgr, "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.Error(err)
log.Errorln(log.CommunicationMgr, err)
}

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

func (s *Slack) handleHelloResponse() {
if s.Verbose {
log.Debugln("Slack: Websocket connected successfully.")
log.Debugln(log.CommunicationMgr, "Slack: Websocket connected successfully.")
}
s.Connected = true
go s.WebsocketKeepAlive()
Expand All @@ -330,7 +330,7 @@ func (s *Slack) handleReconnectResponse(resp []byte) error {
}
s.ReconnectURL = recURL.URL
if s.Verbose {
log.Debugf("Slack: Reconnect URL set to %s\n", s.ReconnectURL)
log.Debugf(log.CommunicationMgr, "Slack: Reconnect URL set to %s\n", s.ReconnectURL)
}
return nil
}
Expand All @@ -343,7 +343,7 @@ func (s *Slack) WebsocketKeepAlive() {
for {
<-ticker.C
if err := s.WebsocketSend("ping", ""); err != nil {
log.Debugf("Slack: WebsocketKeepAlive() error %s", err)
log.Errorf(log.CommunicationMgr, "Slack: WebsocketKeepAlive() error %s\n", err)
}
}
}
Expand All @@ -364,7 +364,7 @@ func (s *Slack) WebsocketSend(eventType, text string) error {
}

if s.Verbose {
log.Debugf("Slack: Sending websocket message: %s", string(data))
log.Debugf(log.CommunicationMgr, "Slack: Sending websocket message: %s\n", string(data))
}

if s.WebsocketConn == nil {
Expand Down
Loading

0 comments on commit 3de1d94

Please sign in to comment.