Adds ability to set log level from toml or CLI args

This commit is contained in:
Andrew J Yao 2019-06-25 17:24:56 -07:00
parent f0a7a7d999
commit 11a65c525c
8 changed files with 38 additions and 16 deletions

View File

@ -17,7 +17,6 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"plugin" "plugin"
syn "sync" syn "sync"
"time" "time"
@ -82,7 +81,7 @@ func execute() {
log.Info("linking plugin", pluginPath) log.Info("linking plugin", pluginPath)
plug, err := plugin.Open(pluginPath) plug, err := plugin.Open(pluginPath)
if err != nil { if err != nil {
log.Debug("linking plugin failed") log.Warn("linking plugin failed")
log.Fatal(err) log.Fatal(err)
} }
@ -90,15 +89,14 @@ func execute() {
log.Info("loading transformers from plugin") log.Info("loading transformers from plugin")
symExporter, err := plug.Lookup("Exporter") symExporter, err := plug.Lookup("Exporter")
if err != nil { if err != nil {
log.Debug("loading Exporter symbol failed") log.Warn("loading Exporter symbol failed")
log.Fatal(err) log.Fatal(err)
} }
// Assert that the symbol is of type Exporter // Assert that the symbol is of type Exporter
exporter, ok := symExporter.(Exporter) exporter, ok := symExporter.(Exporter)
if !ok { if !ok {
log.Debug("plugged-in symbol not of type Exporter") log.Fatal("plugged-in symbol not of type Exporter")
os.Exit(1)
} }
// Use the Exporters export method to load the EventTransformerInitializer, StorageTransformerInitializer, and ContractTransformerInitializer sets // Use the Exporters export method to load the EventTransformerInitializer, StorageTransformerInitializer, and ContractTransformerInitializer sets

View File

@ -96,7 +96,7 @@ func fullSync() {
if err != nil { if err != nil {
log.Error("fullSync: error in validateBlocks: ", err) log.Error("fullSync: error in validateBlocks: ", err)
} }
log.Info(window.GetString()) log.Debug(window.GetString())
case <-missingBlocksPopulated: case <-missingBlocksPopulated:
go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber) go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber)
} }

View File

@ -88,7 +88,7 @@ func headerSync() {
if err != nil { if err != nil {
log.Error("headerSync: ValidateHeaders failed: ", err) log.Error("headerSync: ValidateHeaders failed: ", err)
} }
log.Info(window.GetString()) log.Debug(window.GetString())
case n := <-missingBlocksPopulated: case n := <-missingBlocksPopulated:
if n == 0 { if n == 0 {
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)

View File

@ -18,7 +18,6 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
"time" "time"
@ -56,17 +55,22 @@ const (
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "vulcanizedb", Use: "vulcanizedb",
PersistentPreRun: database, PersistentPreRun: initFuncs,
} }
func Execute() { func Execute() {
log.Info("----- Starting vDB -----") log.Info("----- Starting vDB -----")
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
log.Fatal(err) 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) { func database(cmd *cobra.Command, args []string) {
ipc = viper.GetString("client.ipcpath") ipc = viper.GetString("client.ipcpath")
levelDbPath = viper.GetString("client.leveldbpath") levelDbPath = viper.GetString("client.leveldbpath")
@ -81,6 +85,16 @@ func database(cmd *cobra.Command, args []string) {
viper.Set("database.config", databaseConfig) 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() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
// When searching for env variables, replace dots in config keys with underscores // When searching for env variables, replace dots in config keys with underscores
@ -97,6 +111,7 @@ func init() {
rootCmd.PersistentFlags().String("client-levelDbPath", "", "location of levelDb chaindata") rootCmd.PersistentFlags().String("client-levelDbPath", "", "location of levelDb chaindata")
rootCmd.PersistentFlags().String("filesystem-storageDiffsPath", "", "location of storage diffs csv file") rootCmd.PersistentFlags().String("filesystem-storageDiffsPath", "", "location of storage diffs csv file")
rootCmd.PersistentFlags().String("exporter-name", "exporter", "name of exporter plugin") 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.name", rootCmd.PersistentFlags().Lookup("database-name"))
viper.BindPFlag("database.port", rootCmd.PersistentFlags().Lookup("database-port")) viper.BindPFlag("database.port", rootCmd.PersistentFlags().Lookup("database-port"))
@ -107,6 +122,7 @@ func init() {
viper.BindPFlag("client.levelDbPath", rootCmd.PersistentFlags().Lookup("client-levelDbPath")) viper.BindPFlag("client.levelDbPath", rootCmd.PersistentFlags().Lookup("client-levelDbPath"))
viper.BindPFlag("filesystem.storageDiffsPath", rootCmd.PersistentFlags().Lookup("filesystem-storageDiffsPath")) viper.BindPFlag("filesystem.storageDiffsPath", rootCmd.PersistentFlags().Lookup("filesystem-storageDiffsPath"))
viper.BindPFlag("exporter.fileName", rootCmd.PersistentFlags().Lookup("exporter-name")) viper.BindPFlag("exporter.fileName", rootCmd.PersistentFlags().Lookup("exporter-name"))
viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level"))
} }
func initConfig() { func initConfig() {
@ -116,7 +132,6 @@ func initConfig() {
noConfigError := "No config file passed with --config flag" noConfigError := "No config file passed with --config flag"
fmt.Println("Error: ", noConfigError) fmt.Println("Error: ", noConfigError)
log.Fatal(noConfigError) log.Fatal(noConfigError)
os.Exit(1)
} }
if err := viper.ReadInConfig(); err == nil { if err := viper.ReadInConfig(); err == nil {
@ -125,7 +140,6 @@ func initConfig() {
invalidConfigError := "Couldn't read config file" invalidConfigError := "Couldn't read config file"
fmt.Println("Error: ", invalidConfigError) fmt.Println("Error: ", invalidConfigError)
log.Fatal(invalidConfigError) log.Fatal(invalidConfigError)
os.Exit(1)
} }
} }

View File

@ -17,9 +17,12 @@
package fetcher package fetcher
import ( import (
"strings"
log "github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils" "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
"github.com/vulcanize/vulcanizedb/pkg/fs" "github.com/vulcanize/vulcanizedb/pkg/fs"
"strings"
) )
type IStorageFetcher interface { type IStorageFetcher interface {
@ -39,6 +42,7 @@ func (storageFetcher CsvTailStorageFetcher) FetchStorageDiffs(out chan<- utils.S
if tailErr != nil { if tailErr != nil {
errs <- tailErr errs <- tailErr
} }
log.Debug("fetching storage diffs...")
for line := range t.Lines { for line := range t.Lines {
row, parseErr := utils.FromStrings(strings.Split(line.Text, ",")) row, parseErr := utils.FromStrings(strings.Split(line.Text, ","))
if parseErr != nil { if parseErr != nil {

View File

@ -17,6 +17,7 @@
package history package history
import ( import (
"fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
@ -35,7 +36,7 @@ func PopulateMissingBlocks(blockchain core.BlockChain, blockRepository datastore
return 0, nil return 0, nil
} }
log.Printf("Backfilling %d blocks\n\n", len(blockRange)) log.Debug(getBlockRangeString(blockRange))
_, err = RetrieveAndUpdateBlocks(blockchain, blockRepository, blockRange) _, err = RetrieveAndUpdateBlocks(blockchain, blockRepository, blockRange)
if err != nil { if err != nil {
log.Error("PopulateMissingBlocks: error gettings/updating blocks: ", err) log.Error("PopulateMissingBlocks: error gettings/updating blocks: ", err)
@ -61,3 +62,7 @@ func RetrieveAndUpdateBlocks(blockchain core.BlockChain, blockRepository datasto
} }
return len(blockNumbers), nil return len(blockNumbers), nil
} }
func getBlockRangeString(blockRange []int64) string {
return fmt.Sprintf("Backfilling |%v| blocks", len(blockRange))
}

View File

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

View File

@ -50,6 +50,6 @@ func MakeRange(min, max int64) []int64 {
} }
func (window ValidationWindow) GetString() string { 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) window.LowerBound, window.UpperBound)
} }