Skip to content

Commit

Permalink
Make logging configurable (dgraph-io#605)
Browse files Browse the repository at this point in the history
* added Logger interface to attach logging systems.

Closes dgraph-io#556
  • Loading branch information
srfrog authored Oct 20, 2018
1 parent 42bf595 commit fbb2778
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p/
6 changes: 2 additions & 4 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import (
"bufio"
"encoding/binary"
"io"
"log"
"sync"

"github.com/dgraph-io/badger/y"

"github.com/dgraph-io/badger/protos"
"github.com/dgraph-io/badger/y"
)

func writeTo(entry *protos.KVPair, w io.Writer) error {
Expand Down Expand Up @@ -63,7 +61,7 @@ func (db *DB) Backup(w io.Writer, since uint64) (uint64, error) {
}
valCopy, err := item.ValueCopy(nil)
if err != nil {
log.Printf("Key [%x]. Error while fetching value [%v]\n", item.Key(), err)
Errorf("Key [%x]. Error while fetching value [%v]\n", item.Key(), err)
continue
}

Expand Down
11 changes: 4 additions & 7 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"encoding/binary"
"expvar"
"log"
"math"
"os"
"path/filepath"
Expand All @@ -30,13 +29,11 @@ import (
"time"

"github.com/dgraph-io/badger/options"

"golang.org/x/net/trace"

"github.com/dgraph-io/badger/skl"
"github.com/dgraph-io/badger/table"
"github.com/dgraph-io/badger/y"
"github.com/pkg/errors"
"golang.org/x/net/trace"
)

var (
Expand Down Expand Up @@ -683,7 +680,7 @@ func (db *DB) doWrites(lc *y.Closer) {

writeRequests := func(reqs []*request) {
if err := db.writeRequests(reqs); err != nil {
log.Printf("ERROR in Badger::writeRequests: %v", err)
Errorf("writeRequests: %v", err)
}
<-pendingCh
}
Expand Down Expand Up @@ -903,7 +900,7 @@ func (db *DB) flushMemtable(lc *y.Closer) error {
break
}
// Encounterd error. Retry indefinitely.
log.Printf("Error while flushing memtable to disk: %v. Retrying...\n", err)
Errorf("Failure while flushing memtable to disk: %v. Retrying...\n", err)
time.Sleep(time.Second)
}
}
Expand Down Expand Up @@ -1260,7 +1257,7 @@ func (op *MergeOperator) runCompactions(dur time.Duration) {
case <-ticker.C: // wait for tick
}
if err := op.compact(); err != nil {
log.Printf("Error while running merge operation: %s", err)
Errorf("failure while running merge operation: %s", err)
}
if stop {
ticker.Stop()
Expand Down
69 changes: 69 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package badger

import (
"log"
"os"
)

// Logger is implemented by any logging system that is used for standard logs.
type Logger interface {
Errorf(string, ...interface{})
Infof(string, ...interface{})
Warningf(string, ...interface{})
}

var badgerLogger Logger

func SetLogger(l Logger) { badgerLogger = l }

func Errorf(format string, v ...interface{}) {
badgerLogger.Errorf(format, v...)
}

func Infof(format string, v ...interface{}) {
badgerLogger.Infof(format, v...)
}

func Warningf(format string, v ...interface{}) {
badgerLogger.Warningf(format, v...)
}

type defaultLog struct {
*log.Logger
}

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

func UseDefaultLogger() { SetLogger(defaultLogger) }

func (l *defaultLog) Errorf(f string, v ...interface{}) {
l.Printf("ERROR: "+f, v...)
}

func (l *defaultLog) Infof(f string, v ...interface{}) {
l.Printf("INFO: "+f, v...)
}

func (l *defaultLog) Warningf(f string, v ...interface{}) {
l.Printf("WARNING: "+f, v...)
}

func init() {
UseDefaultLogger()
}
6 changes: 2 additions & 4 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"hash/crc32"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
Expand All @@ -36,7 +35,6 @@ import (
"time"

"github.com/dgraph-io/badger/options"

"github.com/dgraph-io/badger/y"
"github.com/pkg/errors"
"golang.org/x/net/trace"
Expand Down Expand Up @@ -403,7 +401,7 @@ func (vlog *valueLog) rewrite(f *logFile, tr trace.Trace) error {
wb = wb[:0]
}
} else {
log.Printf("WARNING: This entry should have been caught. %+v\n", e)
Warningf("This entry should have been caught. %+v\n", e)
}
return nil
}
Expand All @@ -421,7 +419,7 @@ func (vlog *valueLog) rewrite(f *logFile, tr trace.Trace) error {
for i := 0; i < len(wb); {
loops++
if batchSize == 0 {
log.Printf("WARNING: We shouldn't reach batch size of zero.")
Warningf("We shouldn't reach batch size of zero.")
return ErrNoRewrite
}
end := i + batchSize
Expand Down

0 comments on commit fbb2778

Please sign in to comment.