Skip to content

Commit

Permalink
chore: refactor logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Primexz committed May 13, 2024
1 parent c3ec56b commit 8477d71
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 175 deletions.
20 changes: 14 additions & 6 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/primexz/KrakenDCA/config"
"github.com/primexz/KrakenDCA/kraken"
"github.com/primexz/KrakenDCA/logger"
log "github.com/sirupsen/logrus"
)

type Bot struct {
Expand All @@ -16,15 +16,17 @@ type Bot struct {
fiatAmount float64
initialRun bool

log *logger.Logger
log *log.Entry
krakenApi *kraken.KrakenApi
}

func NewBot() *Bot {
return &Bot{
initialRun: true,
log: logger.NewLogger("bot"),
krakenApi: kraken.NewKrakenApi(),
log: log.WithFields(log.Fields{
"prefix": "bot",
}),
krakenApi: kraken.NewKrakenApi(),
}
}

Expand Down Expand Up @@ -65,7 +67,9 @@ func (b *Bot) run() {

b.timeOfEmptyFiat = computeNextFiatDepositDay()

b.log.Info("Next Fiat deposit required at", b.timeOfEmptyFiat)
b.log.WithFields(log.Fields{
"time": b.timeOfEmptyFiat,
}).Info("Next fiat deposit in")

b.lastFiatBalance = b.fiatAmount
}
Expand All @@ -88,5 +92,9 @@ func (b *Bot) run() {
b.calculateTimeOfNextOrder()
}

b.log.Info("Next order in", fmtDuration(time.Until(b.timeOfNextOrder)))
b.log.WithFields(log.Fields{
"fiat_balance": b.fiatAmount,
"duration": fmtDuration(time.Until(b.timeOfNextOrder)),
}).Info("Next order in")

}
25 changes: 13 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"strconv"

"github.com/primexz/KrakenDCA/logger"
log "github.com/sirupsen/logrus"
)

var (
Expand All @@ -18,15 +18,12 @@ var (
ExperimentalMakerFee bool
)

var log *logger.Logger
var logger = log.WithFields(log.Fields{
"prefix": "bot",
})

func init() {
log = logger.NewLogger("config")
loadConfiguration()
}

func loadConfiguration() {
log.Info("Loading configuration..")
func LoadConfiguration() {
logger.Info("Loading configuration..")

KrakenPublicKey = loadRequiredEnvVariable("KRAKEN_PUBLIC_KEY")
KrakenPrivateKey = loadRequiredEnvVariable("KRAKEN_PRIVATE_KEY")
Expand All @@ -45,7 +42,7 @@ func loadRequiredEnvVariable(envVar string) string {
envData := os.Getenv(envVar)

if envData == "" {
log.Fatal("Required environment variable", envVar, "missing.")
logger.WithField("var", envVar).Fatal("Required environment variable missing.")
}

return envData
Expand All @@ -68,7 +65,7 @@ func loadFloatEnvVariableWithFallback(envVar string, fallback float64) float64 {
} else if s, err := strconv.ParseFloat(envData, 32); err == nil {
return s
} else {
log.Fatal("Failed to parse float environment variable.", envVar)
logger.WithField("var", envVar).Fatal("Failed to parse float environment variable.")
}

return fallback
Expand All @@ -81,7 +78,11 @@ func loadBoolEnvVariableWithFallback(envVar string, fallback bool) bool {
} else if s, err := strconv.ParseBool(envData); err == nil {
return s
} else {
log.Fatal("Failed to parse bool environment variable.", envVar)
logger.WithFields(log.Fields{
"var": envVar,
}).Error("Failed to parse bool environment variable.")

logger.Fatal("Failed to parse bool environment variable.", envVar)
}

return fallback
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ require github.com/aopoltorzhicky/go_kraken/rest v0.0.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,34 @@ github.com/aopoltorzhicky/go_kraken/rest v0.0.4/go.mod h1:iJnPlD/QYjh7kPNEO4nVdf
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/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
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=
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
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=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 5 additions & 2 deletions kraken/kraken_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

"github.com/aopoltorzhicky/go_kraken/rest"
"github.com/primexz/KrakenDCA/config"
"github.com/primexz/KrakenDCA/logger"
log "github.com/sirupsen/logrus"
)

type KrakenApi struct {
api *rest.Kraken

log *logger.Logger
log *log.Entry
}

type KrakenSpread struct {
Expand All @@ -26,6 +26,9 @@ type KrakenSpread struct {
func NewKrakenApi() *KrakenApi {
return &KrakenApi{
api: rest.New(config.KrakenPublicKey, config.KrakenPrivateKey),
log: log.WithFields(log.Fields{
"prefix": "kraken",
}),
}
}

Expand Down
140 changes: 0 additions & 140 deletions logger/logger.go

This file was deleted.

25 changes: 17 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package main

import (
"fmt"
"runtime"

"github.com/primexz/KrakenDCA/bot"
"github.com/primexz/KrakenDCA/config"
"github.com/primexz/KrakenDCA/logger"
)

var (
log *logger.Logger
log "github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

func init() {
log = logger.NewLogger("main")
log.SetFormatter(&prefixed.TextFormatter{
TimestampFormat: "2006/01/02 - 15:04:05",
FullTimestamp: true,
QuoteEmptyFields: true,
SpacePadding: 45,
})
log.SetReportCaller(true)
}

func main() {
log.Info(fmt.Sprintf("Kraken DCA 🐙 %s, commit %s, built at %s (%s [%s, %s])", version, commit, date, runtime.Version(), runtime.GOOS, runtime.GOARCH))
log.WithFields(log.Fields{
"version": version,
"commit": commit,
"runtime": runtime.Version(),
"arch": runtime.GOARCH,
}).Infof("Kraken DCA 🐙 %s", version)

config.LoadConfiguration()

if config.ExperimentalMakerFee {
log.Warn("Experimental maker fee is enabled. This feature is not recommended for production use.")
Expand Down
Loading

0 comments on commit 8477d71

Please sign in to comment.