Make logging configurable (#605)

* added Logger interface to attach logging systems.

Closes #556
This commit is contained in:
Gus
2018-10-19 21:27:26 -07:00
committed by GitHub
parent 42bf595679
commit fbb2778624
5 changed files with 78 additions and 15 deletions
+1
View File
@@ -0,0 +1 @@
p/
+2 -4
View File
@@ -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 {
@@ -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
}
+4 -7
View File
@@ -20,7 +20,6 @@ import (
"bytes"
"encoding/binary"
"expvar"
"log"
"math"
"os"
"path/filepath"
@@ -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 (
@@ -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
}
@@ -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)
}
}
@@ -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()
+69
View File
@@ -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()
}
+2 -4
View File
@@ -24,7 +24,6 @@ import (
"hash/crc32"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
@@ -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"
@@ -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
}
@@ -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