Skip to content

Commit

Permalink
Merge pull request #2 from ivanglie/feature/log
Browse files Browse the repository at this point in the history
Add debug mode and logging
  • Loading branch information
ivanglie authored May 21, 2022
2 parents f5d8e5a + 1277ce2 commit c9aae31
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion _example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../
9 changes: 7 additions & 2 deletions coingate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// {
Expand All @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c9aae31

Please sign in to comment.