Skip to content

Commit

Permalink
Do not have a process wide logger. Allow a nil logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
manishrjain committed Feb 12, 2019
1 parent 28ef9bf commit 2f79792
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 77 deletions.
35 changes: 0 additions & 35 deletions badger/cmd/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,41 +177,6 @@ func diff(a, b []account) string {

var errFailure = errors.New("Found an balance mismatch. Test failed.")

// // iterateTotal retrives the total of all accounts by iterating over the DB.
// func iterateTotal(db *badger.DB) ([]account, error) {
// expected := uint64(numAccounts) * uint64(initialBal)
// var accounts []account
// err := db.View(func(txn *badger.Txn) error {
// // start := time.Now()
// itr := txn.NewIterator(badger.DefaultIteratorOptions)
// defer itr.Close()

// var total uint64
// for itr.Seek([]byte(keyPrefix)); itr.ValidForPrefix([]byte(keyPrefix)); itr.Next() {
// item := itr.Item()
// val, err := item.ValueCopy(nil)
// if err != nil {
// return err
// }
// acc := account{
// Id: toAccount(item.Key()),
// Bal: toUint64(val),
// }
// accounts = append(accounts, acc)
// total += acc.Bal
// }
// if total != expected {
// log.Printf("Balance did NOT match up. Expected: %d. Received: %d",
// expected, total)
// atomic.AddInt32(&stopAll, 1)
// return errFailure
// }
// // log.Printf("totalMoney took: %s\n", time.Since(start).String())
// return nil
// })
// return accounts, err
// }

// seekTotal retrives the total of all accounts by seeking for each account key.
func seekTotal(txn *badger.Txn) ([]account, error) {
expected := uint64(numAccounts) * uint64(initialBal)
Expand Down
6 changes: 3 additions & 3 deletions badger/cmd/fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func fill(cmd *cobra.Command, args []string) error {
defer func() {
start := time.Now()
err := db.Close()
badger.Infof("DB.Close. Error: %v. Time taken: %s", err, time.Since(start))
opts.Infof("DB.Close. Error: %v. Time taken: %s", err, time.Since(start))
}()

start := time.Now()
Expand All @@ -82,12 +82,12 @@ func fill(cmd *cobra.Command, args []string) error {
return err
}
if i%1e5 == 0 {
badger.Infof("Written keys: %d\n", i)
opts.Infof("Written keys: %d\n", i)
}
}
if err := batch.Flush(); err != nil {
return err
}
badger.Infof("%d keys written. Time taken: %s\n", num, time.Since(start))
opts.Infof("%d keys written. Time taken: %s\n", num, time.Since(start))
return nil
}
46 changes: 8 additions & 38 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,29 @@ type Logger interface {
Warningf(string, ...interface{})
}

var badgerLogger Logger

// SetLogger sets the global logger.
func SetLogger(l Logger) { badgerLogger = l }

// Errorf logs an ERROR message to the global logger.
func Errorf(format string, v ...interface{}) {
badgerLogger.Errorf(format, v...)
}

// Errorf logs an ERROR log message to the logger specified in opts or to the
// global logger if no logger is specified in opts.
func (opt *Options) Errorf(format string, v ...interface{}) {
if opt.Logger != nil {
opt.Logger.Errorf(format, v...)
if opt.Logger == nil {
return
}
Errorf(format, v...)
}

// Infof logs an INFO message to the global logger.
func Infof(format string, v ...interface{}) {
badgerLogger.Infof(format, v...)
opt.Logger.Errorf(format, v...)
}

// Infof is like Errorf but for INFO messages.
// Infof logs an INFO message to the logger specified in opts.
func (opt *Options) Infof(format string, v ...interface{}) {
if opt.Logger != nil {
opt.Logger.Infof(format, v...)
if opt.Logger == nil {
return
}
Infof(format, v...)
opt.Logger.Infof(format, v...)
}

// Warningf logs a WARNING message to the global logger.
func Warningf(format string, v ...interface{}) {
badgerLogger.Warningf(format, v...)
}

// Warningf is like Errorf but for WARNING messages.
// Warningf logs a WARNING message to the logger specified in opts.
func (opt *Options) Warningf(format string, v ...interface{}) {
if opt.Logger != nil {
opt.Logger.Warningf(format, v...)
if opt.Logger == nil {
return
}
Warningf(format, v...)
opt.Logger.Warningf(format, v...)
}

type defaultLog struct {
Expand All @@ -82,9 +59,6 @@ type defaultLog struct {

var defaultLogger = &defaultLog{Logger: log.New(os.Stderr, "badger ", log.LstdFlags)}

// UseDefaultLogger sets the global logger to the default logger.
func UseDefaultLogger() { SetLogger(defaultLogger) }

func (l *defaultLog) Errorf(f string, v ...interface{}) {
l.Printf("ERROR: "+f, v...)
}
Expand All @@ -96,7 +70,3 @@ func (l *defaultLog) Infof(f string, v ...interface{}) {
func (l *defaultLog) Warningf(f string, v ...interface{}) {
l.Printf("WARNING: "+f, v...)
}

func init() {
UseDefaultLogger()
}
2 changes: 1 addition & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func TestDbLog(t *testing.T) {
// Test that the global logger is used when no logger is specified in Options.
func TestNoDbLog(t *testing.T) {
l := &mockLogger{}
SetLogger(l)
opt := Options{}
opt.Logger = l

opt.Errorf("test")
require.Equal(t, "ERROR: test", l.output)
Expand Down

0 comments on commit 2f79792

Please sign in to comment.