log.go 1.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package elog

import (
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
	"github.com/jbenet/go-ipfs/util"
)

var eloggers = map[string]*logging.Logger{}

func init() {
	SetupLogging()
}

type EventLogger interface {
	StandardLogger
17
	Event(ctx context.Context, event string, m ...Loggable)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
}

type StandardLogger interface {
	Critical(args ...interface{})
	Criticalf(format string, args ...interface{})
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Notice(args ...interface{})
	Noticef(format string, args ...interface{})
	Panic(args ...interface{})
	Panicf(format string, args ...interface{})
	Warning(args ...interface{})
	Warningf(format string, args ...interface{})
}

// Logger retrieves a particular event logger
func Logger(system string) EventLogger {
	return &eventLogger{util.Logger(system)}
}

// eventLogger implements the EventLogger and wraps a go-logging Logger
type eventLogger struct {
	*logging.Logger
}

49
func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Loggable) {
50 51 52 53 54 55
	existing, err := MetadataFromContext(ctx)
	if err != nil {
		existing = Metadata{}
	}
	accum := existing
	for _, datum := range metadata {
56
		accum = DeepMerge(accum, datum.Loggable())
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	}
	accum["event"] = event

	str, err := accum.JsonString()
	if err != nil {
		return
	}
	el.Logger.Info(str)
}

// SetupLogging will initialize the logger backend and set the flags.
func SetupLogging() {
	// 	fmt := logging.DefaultFormatter

	// 	f, err := os.Create("events.ipfslog")
	// 	if err != nil {
	// 		panic("failed to open file for event logger")
	// 	}
}