Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Adds ability to set log level from toml or CLI args
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoandrew committed Jun 26, 2019
1 parent f0a7a7d commit 2fbf623
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 16 deletions.
8 changes: 3 additions & 5 deletions cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"fmt"
"os"
"plugin"
syn "sync"
"time"
Expand Down Expand Up @@ -82,23 +81,22 @@ func execute() {
log.Info("linking plugin", pluginPath)
plug, err := plugin.Open(pluginPath)
if err != nil {
log.Debug("linking plugin failed")
log.Warn("linking plugin failed")
log.Fatal(err)
}

// Load the `Exporter` symbol from the plugin
log.Info("loading transformers from plugin")
symExporter, err := plug.Lookup("Exporter")
if err != nil {
log.Debug("loading Exporter symbol failed")
log.Warn("loading Exporter symbol failed")
log.Fatal(err)
}

// Assert that the symbol is of type Exporter
exporter, ok := symExporter.(Exporter)
if !ok {
log.Debug("plugged-in symbol not of type Exporter")
os.Exit(1)
log.Fatal("plugged-in symbol not of type Exporter")
}

// Use the Exporters export method to load the EventTransformerInitializer, StorageTransformerInitializer, and ContractTransformerInitializer sets
Expand Down
2 changes: 1 addition & 1 deletion cmd/fullSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func fullSync() {
if err != nil {
log.Error("fullSync: error in validateBlocks: ", err)
}
log.Info(window.GetString())
log.Debug(window.GetString())
case <-missingBlocksPopulated:
go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/headerSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func headerSync() {
if err != nil {
log.Error("headerSync: ValidateHeaders failed: ", err)
}
log.Info(window.GetString())
log.Debug(window.GetString())
case n := <-missingBlocksPopulated:
if n == 0 {
time.Sleep(3 * time.Second)
Expand Down
24 changes: 19 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"fmt"
"os"
"strings"
"time"

Expand Down Expand Up @@ -56,17 +55,22 @@ const (

var rootCmd = &cobra.Command{
Use: "vulcanizedb",
PersistentPreRun: database,
PersistentPreRun: initFuncs,
}

func Execute() {
log.Info("----- Starting vDB -----")
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}

func initFuncs(cmd *cobra.Command, args []string) {
database(cmd, args)
logLevel(cmd, args)

}

func database(cmd *cobra.Command, args []string) {
ipc = viper.GetString("client.ipcpath")
levelDbPath = viper.GetString("client.leveldbpath")
Expand All @@ -81,6 +85,16 @@ func database(cmd *cobra.Command, args []string) {
viper.Set("database.config", databaseConfig)
}

func logLevel(cmd *cobra.Command, args []string) error {
lvl, err := log.ParseLevel(viper.GetString("log.level"))
if err != nil {
return err
}
log.SetLevel(lvl)
log.Info("Log level set to ", lvl.String())
return nil
}

func init() {
cobra.OnInitialize(initConfig)
// When searching for env variables, replace dots in config keys with underscores
Expand All @@ -97,6 +111,7 @@ func init() {
rootCmd.PersistentFlags().String("client-levelDbPath", "", "location of levelDb chaindata")
rootCmd.PersistentFlags().String("filesystem-storageDiffsPath", "", "location of storage diffs csv file")
rootCmd.PersistentFlags().String("exporter-name", "exporter", "name of exporter plugin")
rootCmd.PersistentFlags().String("log-level", log.InfoLevel.String(), "Log level (trace, debug, info, warn, error, fatal, panic")

viper.BindPFlag("database.name", rootCmd.PersistentFlags().Lookup("database-name"))
viper.BindPFlag("database.port", rootCmd.PersistentFlags().Lookup("database-port"))
Expand All @@ -107,6 +122,7 @@ func init() {
viper.BindPFlag("client.levelDbPath", rootCmd.PersistentFlags().Lookup("client-levelDbPath"))
viper.BindPFlag("filesystem.storageDiffsPath", rootCmd.PersistentFlags().Lookup("filesystem-storageDiffsPath"))
viper.BindPFlag("exporter.fileName", rootCmd.PersistentFlags().Lookup("exporter-name"))
viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level"))
}

func initConfig() {
Expand All @@ -116,7 +132,6 @@ func initConfig() {
noConfigError := "No config file passed with --config flag"
fmt.Println("Error: ", noConfigError)
log.Fatal(noConfigError)
os.Exit(1)
}

if err := viper.ReadInConfig(); err == nil {
Expand All @@ -125,7 +140,6 @@ func initConfig() {
invalidConfigError := "Couldn't read config file"
fmt.Println("Error: ", invalidConfigError)
log.Fatal(invalidConfigError)
os.Exit(1)
}
}

Expand Down
6 changes: 5 additions & 1 deletion libraries/shared/fetcher/storage_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package fetcher

import (
"strings"

log "github.com/sirupsen/logrus"

"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
"github.com/vulcanize/vulcanizedb/pkg/fs"
"strings"
)

type IStorageFetcher interface {
Expand All @@ -39,6 +42,7 @@ func (storageFetcher CsvTailStorageFetcher) FetchStorageDiffs(out chan<- utils.S
if tailErr != nil {
errs <- tailErr
}
log.Debug("fetching storage diffs...")
for line := range t.Lines {
row, parseErr := utils.FromStrings(strings.Split(line.Text, ","))
if parseErr != nil {
Expand Down
7 changes: 6 additions & 1 deletion pkg/history/populate_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package history

import (
"fmt"
log "github.com/sirupsen/logrus"

"github.com/vulcanize/vulcanizedb/pkg/core"
Expand All @@ -35,7 +36,7 @@ func PopulateMissingBlocks(blockchain core.BlockChain, blockRepository datastore
return 0, nil
}

log.Printf("Backfilling %d blocks\n\n", len(blockRange))
log.Debug(getBlockRangeString(blockRange))
_, err = RetrieveAndUpdateBlocks(blockchain, blockRepository, blockRange)
if err != nil {
log.Error("PopulateMissingBlocks: error gettings/updating blocks: ", err)
Expand All @@ -61,3 +62,7 @@ func RetrieveAndUpdateBlocks(blockchain core.BlockChain, blockRepository datasto
}
return len(blockNumbers), nil
}

func getBlockRangeString(blockRange []int64) string {
return fmt.Sprintf("Backfilling |%v| blocks", len(blockRange))
}
7 changes: 6 additions & 1 deletion pkg/history/populate_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package history

import (
"fmt"
log "github.com/sirupsen/logrus"

"github.com/vulcanize/vulcanizedb/pkg/core"
Expand All @@ -39,7 +40,7 @@ func PopulateMissingHeaders(blockChain core.BlockChain, headerRepository datasto
return 0, nil
}

log.Printf("Backfilling %d blocks\n\n", len(blockNumbers))
log.Debug(getBlockNumbersString(blockNumbers))
_, err = RetrieveAndUpdateHeaders(blockChain, headerRepository, blockNumbers)
if err != nil {
log.Error("PopulateMissingHeaders: Error getting/updating headers:", err)
Expand All @@ -61,3 +62,7 @@ func RetrieveAndUpdateHeaders(blockChain core.BlockChain, headerRepository datas
}
return len(blockNumbers), nil
}

func getBlockNumbersString(blockNumbers []int64) string {
return fmt.Sprintf("Backfilling |%v| blocks", len(blockNumbers))
}
2 changes: 1 addition & 1 deletion pkg/history/validation_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ func MakeRange(min, max int64) []int64 {
}

func (window ValidationWindow) GetString() string {
return fmt.Sprintf("Validating Blocks |%v|-- Validation Window --|%v}|",
return fmt.Sprintf("Validating Blocks |%v|-- Validation Window --|%v|",
window.LowerBound, window.UpperBound)
}

0 comments on commit 2fbf623

Please sign in to comment.