Skip to content

Commit

Permalink
skip callers in badger log for useful line info.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Nov 10, 2020
1 parent 8f5847b commit bc6965c
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/blockstore/badger/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
"github.com/multiformats/go-base32"
"go.uber.org/zap"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -57,14 +58,17 @@ func DefaultOptions(path string) Options {
}
}

// badgerLog is a local wrapper for go-log to make the interface
// badgerLogger is a local wrapper for go-log to make the interface
// compatible with badger.Logger (namely, aliasing Warnf to Warningf)
type badgerLog struct {
logger.ZapEventLogger
type badgerLogger struct {
*zap.SugaredLogger // skips 1 caller to get useful line info, skipping over badger.Options.

skip2 *zap.SugaredLogger // skips 2 callers, just like above + this logger.
}

func (b *badgerLog) Warningf(format string, args ...interface{}) {
b.Warnf(format, args...)
// Warningf is required by the badger logger APIs.
func (b *badgerLogger) Warningf(format string, args ...interface{}) {
b.skip2.Warnf(format, args...)
}

const (
Expand Down Expand Up @@ -96,7 +100,10 @@ var _ io.Closer = (*Blockstore)(nil)

// Open creates a new badger-backed blockstore, with the supplied options.
func Open(opts Options) (*Blockstore, error) {
opts.Logger = &badgerLog{*log}
opts.Logger = &badgerLogger{
SugaredLogger: log.Desugar().WithOptions(zap.AddCallerSkip(1)).Sugar(),
skip2: log.Desugar().WithOptions(zap.AddCallerSkip(2)).Sugar(),
}

db, err := badger.Open(opts.Options)
if err != nil {
Expand Down

0 comments on commit bc6965c

Please sign in to comment.