Skip to content

Commit

Permalink
fix: loglevel in gin log
Browse files Browse the repository at this point in the history
  • Loading branch information
tyzbit committed May 3, 2022
1 parent cefff51 commit c227bb4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ main:

currencyBalance, err := w.ConvertBalance(oldAddressInfo.Currency, addressSummary.TXHistory.BalanceSat)
if err != nil || currencyBalance == nil {
log.Errorf("unable to convert balance of %d to %s, err: %v", addressSummary.TXHistory.BalanceSat, w.Currency, err)
log.Errorf("unable to convert balance of %d to %s, err: %v", addressSummary.TXHistory.BalanceSat, w.Config.Currency, err)
currencyBalance[0] = "0.00"
}
addressInfo := AddressInfo{
Expand All @@ -103,7 +103,7 @@ main:
w.SendNotification(addressInfo, addressMessageTemplate)
}
// Check every second for a stop signal
for i := 0; i < w.SleepInterval; i++ {
for i := 0; i < w.Config.SleepInterval; i++ {
select {
case <-stop:
break main
Expand All @@ -124,7 +124,7 @@ func (w Watcher) CreateNewAddressInfo(address string, nickname string) (AddressI
Nickname: nickname,
BalanceSat: 0,
BalanceCurrency: "0.00",
Currency: w.Currency,
Currency: w.Config.Currency,
PreviousBalanceSat: 0,
PreviousBalanceCurrency: "0.00",
TXCount: 0,
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Watcher struct {
LogConfig logger.Interface
// Config is embedded into Watcher at the top level, except
// it needs to be explicitly called when using pointers.
Config
Config Config
}

type Config struct {
Expand Down Expand Up @@ -68,7 +68,7 @@ func init() {
}

func main() {
db, err := gorm.Open(sqlite.Open(watcher.DBPath), &gorm.Config{Logger: watcher.LogConfig})
db, err := gorm.Open(sqlite.Open(watcher.Config.DBPath), &gorm.Config{Logger: watcher.LogConfig})
if err != nil {
log.Fatal("unable to open db: ", err)
}
Expand All @@ -82,7 +82,7 @@ func main() {

// Set up BTC-RPC
watcher.BTCAPI = btcapi.Config{
ExplorerURL: watcher.BTCAPIEndpoint,
ExplorerURL: watcher.Config.BTCAPIEndpoint,
}

watcher.StartWatches()
Expand All @@ -91,7 +91,7 @@ func main() {
InitFrontend(r)
InitBackend(r)

if err := r.Run(":"+watcher.Port); err != nil {
if err := r.Run(":"+watcher.Config.Port); err != nil {
log.Fatal("could not start: ", err)
}
}
14 changes: 7 additions & 7 deletions pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ main:
continue
}

if w.CheckAllPubkeyTypes {
if w.Config.CheckAllPubkeyTypes {
for _, pubkeyType := range pubkeySummary.RelatedKeys {
pubKeys = append(pubKeys, pubkeyType.Key)
}
Expand All @@ -100,8 +100,8 @@ main:
totalPubkeyBalance, totalPubkeyTxCount, NoTXCount := 0, 0, 0

pubkey:
for offset := 0; 0 == 0; offset = offset + w.PageSize {
pubKeyPage, err := w.BTCAPI.ExtendedPublicKeyDetailsPage(pubkey, w.PageSize, offset)
for offset := 0; 0 == 0; offset = offset + w.Config.PageSize {
pubKeyPage, err := w.BTCAPI.ExtendedPublicKeyDetailsPage(pubkey, w.Config.PageSize, offset)
if err != nil {
log.Errorf("error calling btcapi: %v", err)
}
Expand Down Expand Up @@ -129,7 +129,7 @@ main:
continue
}
if pubkeyTxCount == addressSummary.TXHistory.TXCount {
if NoTXCount > w.Lookahead*2 {
if NoTXCount > w.Config.Lookahead*2 {
// Stop paging, we haven't had an address with
// transactions in w.Lookahead * 2 addresses.
// (we multiply by 2 because we're checking
Expand All @@ -155,7 +155,7 @@ main:

currencyBalance, err := w.ConvertBalance(oldPubkeyInfo.Currency, totalBalance)
if err != nil || currencyBalance == nil {
log.Errorf("unable to convert balance of %d to %s, err: %v", totalBalance, w.Currency, err)
log.Errorf("unable to convert balance of %d to %s, err: %v", totalBalance, w.Config.Currency, err)
currencyBalance[0] = "0.00"
}
pubkeyInfo := PubkeyInfo{
Expand All @@ -174,7 +174,7 @@ main:
w.SendNotification(pubkeyInfo, pubkeyMessageTemplate)
}
// Check every second for a stop signal
for i := 0; i < w.SleepInterval; i++ {
for i := 0; i < w.Config.SleepInterval; i++ {
select {
case <-stop:
break main
Expand All @@ -195,7 +195,7 @@ func (w Watcher) CreateNewPubkeyInfo(pubkey string, nickname string) (PubkeyInfo
Nickname: nickname,
BalanceSat: 0,
BalanceCurrency: "0.00",
Currency: w.Currency,
Currency: w.Config.Currency,
PreviousBalanceSat: 0,
PreviousBalanceCurrency: "0.00",
TXCount: 0,
Expand Down
48 changes: 24 additions & 24 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ func (w *Watcher) InitLogging() {
// Info level by default
LogLevelSelection := log.InfoLevel
switch {
case strings.EqualFold(w.LogLevel, "trace"):
case strings.EqualFold(w.Config.LogLevel, "trace"):
LogLevelSelection = log.TraceLevel
log.SetReportCaller(true)
case strings.EqualFold(w.LogLevel, "debug"):
case strings.EqualFold(w.Config.LogLevel, "debug"):
LogLevelSelection = log.DebugLevel
log.SetReportCaller(true)
case strings.EqualFold(w.LogLevel, "info"):
case strings.EqualFold(w.Config.LogLevel, "info"):
LogLevelSelection = log.InfoLevel
case strings.EqualFold(w.LogLevel, "warn"):
case strings.EqualFold(w.Config.LogLevel, "warn"):
LogLevelSelection = log.WarnLevel
case strings.EqualFold(w.LogLevel, "error"):
case strings.EqualFold(w.Config.LogLevel, "error"):
LogLevelSelection = log.ErrorLevel
}
log.SetLevel(LogLevelSelection)
Expand All @@ -77,7 +77,7 @@ func (w *Watcher) InitLogging() {
func GinJSONFormatter(param gin.LogFormatterParams) string {
jsonFormat := `{"level":"%s","time":"%s","clientip":"%s","method":"%s","uri":"%s","status":%3d,"latency":"%v","message":"%s","host":"%s","useragent":"%s","proto","%s","error_msg":"%s","size":%d}` + "\n"
return fmt.Sprintf(jsonFormat,
watcher.LogLevel,
watcher.Config.LogLevel,
param.TimeStamp.Format(TimeFormatter),
param.ClientIP,
param.Method,
Expand All @@ -97,35 +97,35 @@ func GinJSONFormatter(param gin.LogFormatterParams) string {
// values defined in the constants
func (w *Watcher) FillDefaults() {
// Set unitialized values to preset defaults
if w.BTCAPIEndpoint == "" {
w.BTCAPIEndpoint = DefaultApi
if w.Config.BTCAPIEndpoint == "" {
w.Config.BTCAPIEndpoint = DefaultApi
}
if w.SleepInterval == 0 {
w.SleepInterval = DefaultSleepInterval
if w.Config.SleepInterval == 0 {
w.Config.SleepInterval = DefaultSleepInterval
}
if w.Lookahead == 0 {
w.Lookahead = DefaultLookahead
if w.Config.Lookahead == 0 {
w.Config.Lookahead = DefaultLookahead
}
if w.PageSize == 0 {
w.PageSize = DefaultPageSize
if w.Config.PageSize == 0 {
w.Config.PageSize = DefaultPageSize
}
if w.DBPath == "" {
w.DBPath = DefaultDBPath
if w.Config.DBPath == "" {
w.Config.DBPath = DefaultDBPath
}
if w.Port == "" {
w.Port = "80"
if w.Config.Port == "" {
w.Config.Port = "80"
}
if w.Currency == "" {
w.Currency = CurrencyUSD
if w.Config.Currency == "" {
w.Config.Currency = CurrencyUSD
}

// Set up DB path
// Create the folder path if it doesn't exist
if _, err := os.Stat(w.DBPath); errors.Is(err, fs.ErrNotExist) {
dirPath := filepath.Dir(w.DBPath)
if _, err := os.Stat(w.Config.DBPath); errors.Is(err, fs.ErrNotExist) {
dirPath := filepath.Dir(w.Config.DBPath)
if err := os.MkdirAll(dirPath, 0660); err != nil {
log.Warn("unable to make directory path ", dirPath, " err: ", err)
w.DBPath = "./local.db"
w.Config.DBPath = "./local.db"
}
}
}
Expand Down Expand Up @@ -203,7 +203,7 @@ func (w Watcher) SendNotification(i interface{}, mt string) {
}

client := http.Client{}
resp, respErr := client.Post(w.DiscordWebhook, "application/json", &m)
resp, respErr := client.Post(w.Config.DiscordWebhook, "application/json", &m)
if respErr != nil || resp.StatusCode != 204 {
log.Errorf("error calling Discord API (%s): %v", resp.Status, respErr)
return
Expand Down

0 comments on commit c227bb4

Please sign in to comment.