diff --git a/_example/go.mod b/_example/go.mod index 4e44377..92ee451 100644 --- a/_example/go.mod +++ b/_example/go.mod @@ -2,6 +2,6 @@ module github.com/ivanglie/go-coingate-client/_example go 1.16 -require github.com/ivanglie/go-coingate-client v1.0.0 +require github.com/ivanglie/go-coingate-client v1.0.1 replace github.com/ivanglie/go-coingate-client => ../ diff --git a/coingate.go b/coingate.go index f63c349..42c7754 100644 --- a/coingate.go +++ b/coingate.go @@ -4,12 +4,15 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "strconv" ) const baseURL = "https://api.coingate.com/v2/rates/merchant" +// Debug mode +// If this variable is set to true, debug mode activated for the package +var Debug = false + // CoinGate API error responses // Response example: // { @@ -27,7 +30,9 @@ type Error struct { // Arguments are ISO Symbol. Example: EUR, USD, BTC, ETH, etc. // See https://developer.coingate.com/docs/get-rate func getRate(from, to string, fetch FetchFunction) (float64, error) { - log.Printf("Fetching the currency rate for %s\n", to) + if Debug { + log.Printf("Fetching the currency rate for %s\n", to) + } var res float64 = 0 url := fmt.Sprintf("%s/%s/%s", baseURL, from, to) diff --git a/log.go b/log.go new file mode 100644 index 0000000..e0bda19 --- /dev/null +++ b/log.go @@ -0,0 +1,24 @@ +package coingate + +import ( + "errors" + stdlog "log" + "os" +) + +// Logger is an interface that represents the required methods to log data. +type Logger interface { + Println(v ...interface{}) + Printf(format string, v ...interface{}) +} + +var log Logger = stdlog.New(os.Stderr, "", stdlog.LstdFlags) + +// SetLogger specifies the logger that the package should use. +func SetLogger(logger Logger) error { + if logger == nil { + return errors.New("logger is nil") + } + log = logger + return nil +}