Commit 210b5df7 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

use my go-logging fork

until https://github.com/op/go-logging/pull/30 is merged
parent 762faa74
......@@ -87,6 +87,10 @@
"ImportPath": "github.com/jbenet/go-is-domain",
"Rev": "93b717f2ae17838a265e30277275ee99ee7198d6"
},
{
"ImportPath": "github.com/jbenet/go-logging",
"Rev": "74bec4b83f6d45d1402c1e9d94c0c29e39f6e0ea"
},
{
"ImportPath": "github.com/jbenet/go-msgio",
"Rev": "c9069ab79c95aa0686347b516972c7329c4391f2"
......@@ -109,10 +113,6 @@
"ImportPath": "github.com/mitchellh/go-homedir",
"Rev": "7d2d8c8a4e078ce3c58736ab521a40b37a504c52"
},
{
"ImportPath": "github.com/op/go-logging",
"Rev": "3df864a88c7f005e676db4f026a4fe2f14929be3"
},
{
"ImportPath": "github.com/syndtr/goleveldb/leveldb",
"Rev": "99056d50e56252fbe0021d5c893defca5a76baf8"
......
......@@ -80,4 +80,3 @@ You can use `go get -u` to update the package.
For docs, see http://godoc.org/github.com/op/go-logging or run:
$ godoc github.com/op/go-logging
......@@ -3,7 +3,7 @@ package main
import (
"os"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("example")
......
......@@ -55,6 +55,7 @@ type Record struct {
formatted string
}
// Formatted returns the string-formatted version of a record.
func (r *Record) Formatted(calldepth int) string {
if r.formatted == "" {
var buf bytes.Buffer
......@@ -64,6 +65,8 @@ func (r *Record) Formatted(calldepth int) string {
return r.formatted
}
// Message returns a string message for outputting. Redacts any record args
// that implement the Redactor interface
func (r *Record) Message() string {
if r.message == nil {
// Redact the arguments that implements the Redactor interface
......@@ -78,12 +81,22 @@ func (r *Record) Message() string {
return *r.message
}
// Logger is a logging unit. It controls the flow of messages to a given
// (swappable) backend.
type Logger struct {
Module string
Module string
backend LeveledBackend
haveBackend bool
}
// SetBackend changes the backend of the logger.
func (l *Logger) SetBackend(backend LeveledBackend) {
l.backend = backend
l.haveBackend = true
}
// TODO call NewLogger and remove MustGetLogger?
// GetLogger creates and returns a Logger object based on the module name.
// TODO call NewLogger and remove MustGetLogger?
func GetLogger(module string) (*Logger, error) {
return &Logger{Module: module}, nil
}
......@@ -148,6 +161,11 @@ func (l *Logger) log(lvl Level, format string, args ...interface{}) {
// calldepth=2 brings the stack up to the caller of the level
// methods, Info(), Fatal(), etc.
if l.haveBackend {
l.backend.Log(lvl, 2, record)
return
}
defaultBackend.Log(lvl, 2, record)
}
......@@ -178,33 +196,69 @@ func (l *Logger) Panicf(format string, args ...interface{}) {
panic(s)
}
// Critical logs a message using CRITICAL as log level.
func (l *Logger) Critical(format string, args ...interface{}) {
// Critical logs a message using CRITICAL as log level. (fmt.Sprint())
func (l *Logger) Critical(args ...interface{}) {
s := fmt.Sprint(args...)
l.log(CRITICAL, "%s", s)
}
// Criticalf logs a message using CRITICAL as log level.
func (l *Logger) Criticalf(format string, args ...interface{}) {
l.log(CRITICAL, format, args...)
}
// Error logs a message using ERROR as log level.
func (l *Logger) Error(format string, args ...interface{}) {
// Error logs a message using ERROR as log level. (fmt.Sprint())
func (l *Logger) Error(args ...interface{}) {
s := fmt.Sprint(args...)
l.log(ERROR, "%s", s)
}
// Errorf logs a message using ERROR as log level.
func (l *Logger) Errorf(format string, args ...interface{}) {
l.log(ERROR, format, args...)
}
// Warning logs a message using WARNING as log level.
func (l *Logger) Warning(format string, args ...interface{}) {
func (l *Logger) Warning(args ...interface{}) {
s := fmt.Sprint(args...)
l.log(WARNING, "%s", s)
}
// Warningf logs a message using WARNING as log level.
func (l *Logger) Warningf(format string, args ...interface{}) {
l.log(WARNING, format, args...)
}
// Notice logs a message using NOTICE as log level.
func (l *Logger) Notice(format string, args ...interface{}) {
func (l *Logger) Notice(args ...interface{}) {
s := fmt.Sprint(args...)
l.log(NOTICE, "%s", s)
}
// Noticef logs a message using NOTICE as log level.
func (l *Logger) Noticef(format string, args ...interface{}) {
l.log(NOTICE, format, args...)
}
// Info logs a message using INFO as log level.
func (l *Logger) Info(format string, args ...interface{}) {
func (l *Logger) Info(args ...interface{}) {
s := fmt.Sprint(args...)
l.log(INFO, "%s", s)
}
// Infof logs a message using INFO as log level.
func (l *Logger) Infof(format string, args ...interface{}) {
l.log(INFO, format, args...)
}
// Debug logs a message using DEBUG as log level.
func (l *Logger) Debug(format string, args ...interface{}) {
func (l *Logger) Debug(args ...interface{}) {
s := fmt.Sprint(args...)
l.log(DEBUG, "%s", s)
}
// Debugf logs a message using DEBUG as log level.
func (l *Logger) Debugf(format string, args ...interface{}) {
l.log(DEBUG, format, args...)
}
......
......@@ -29,8 +29,25 @@ func TestRedact(t *testing.T) {
backend := InitForTesting(DEBUG)
password := Password("123456")
log := MustGetLogger("test")
log.Debug("foo %s", password)
log.Debugf("foo %s", password)
if "foo ******" != MemoryRecordN(backend, 0).Formatted(0) {
t.Errorf("redacted line: %v", MemoryRecordN(backend, 0))
}
}
func TestPrivateBackend(t *testing.T) {
stdBackend := InitForTesting(DEBUG)
log := MustGetLogger("test")
privateBackend := NewMemoryBackend(10240)
lvlBackend := AddModuleLevel(privateBackend)
lvlBackend.SetLevel(DEBUG, "")
log.SetBackend(lvlBackend)
log.Debug("to private backend")
if stdBackend.size > 0 {
t.Errorf("something in stdBackend, size of backend: %d", stdBackend.size)
}
if "to private baсkend" == MemoryRecordN(privateBackend, 0).Formatted(0) {
t.Errorf("logged to defaultBackend: %s", MemoryRecordN(privateBackend, 0))
}
}
......@@ -51,7 +51,7 @@ func TestMemoryBackend(t *testing.T) {
// Run 13 times, the resulting vector should be [5..12]
for i := 0; i < 13; i++ {
log.Info("%d", i)
log.Infof("%d", i)
}
if 8 != backend.size {
......@@ -89,7 +89,7 @@ func TestChannelMemoryBackend(t *testing.T) {
// Run 13 times, the resulting vector should be [5..12]
for i := 0; i < 13; i++ {
log.Info("%d", i)
log.Infof("%d", i)
}
backend.Flush()
......
......@@ -6,7 +6,6 @@ import (
"io/ioutil"
"testing"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
bs "github.com/jbenet/go-ipfs/blockservice"
"github.com/jbenet/go-ipfs/importer/chunk"
mdag "github.com/jbenet/go-ipfs/merkledag"
......@@ -14,6 +13,7 @@ import (
u "github.com/jbenet/go-ipfs/util"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
)
func getMockDagServ(t *testing.T) *mdag.DAGService {
......
......@@ -4,7 +4,7 @@ import (
"fmt"
"os"
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
)
func init() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment