From e906dcd862bd1cfb9b228ceda4fad746f34187cb Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Sun, 16 Nov 2014 05:12:19 -0800 Subject: [PATCH] refactor(eventlog) extract entry License: MIT Signed-off-by: Brian Tiger Chow --- util/eventlog/entry.go | 39 +++++++++++++++++++++++++++++++++++++++ util/eventlog/log.go | 34 ---------------------------------- 2 files changed, 39 insertions(+), 34 deletions(-) create mode 100644 util/eventlog/entry.go diff --git a/util/eventlog/entry.go b/util/eventlog/entry.go new file mode 100644 index 000000000..aaac78a2b --- /dev/null +++ b/util/eventlog/entry.go @@ -0,0 +1,39 @@ +package eventlog + +import ( + "time" + + "github.com/jbenet/go-ipfs/util" + "github.com/maybebtc/logrus" +) + +type entry struct { + loggables []Loggable + system string + event string +} + +// Log logs the event unconditionally (regardless of log level) +// TODO add support for leveled-logs once we decide which levels we want +// for our structured logs +func (e *entry) Log() { + e.log() +} + +// log is a private method invoked by the public Log, Info, Error methods +func (e *entry) log() { + // accumulate metadata + accum := Metadata{} + for _, loggable := range e.loggables { + accum = DeepMerge(accum, loggable.Loggable()) + } + + // apply final attributes to reserved keys + // TODO accum["level"] = level + accum["event"] = e.event + accum["system"] = e.system + accum["time"] = util.FormatRFC3339(time.Now()) + + // TODO roll our own event logger + logrus.WithFields(map[string]interface{}(accum)).Info(e.event) +} diff --git a/util/eventlog/log.go b/util/eventlog/log.go index 1b622f0f4..4f9757bd1 100644 --- a/util/eventlog/log.go +++ b/util/eventlog/log.go @@ -1,10 +1,7 @@ package eventlog import ( - "time" - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/Sirupsen/logrus" logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging" "github.com/jbenet/go-ipfs/util" ) @@ -91,34 +88,3 @@ func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Logg e.Log() // TODO replace this when leveled-logs have been implemented } - -type entry struct { - loggables []Loggable - system string - event string -} - -// Log logs the event unconditionally (regardless of log level) -// TODO add support for leveled-logs once we decide which levels we want -// for our structured logs -func (e *entry) Log() { - e.log() -} - -// log is a private method invoked by the public Log, Info, Error methods -func (e *entry) log() { - // accumulate metadata - accum := Metadata{} - for _, loggable := range e.loggables { - accum = DeepMerge(accum, loggable.Loggable()) - } - - // apply final attributes to reserved keys - // TODO accum["level"] = level - accum["event"] = e.event - accum["system"] = e.system - accum["time"] = util.FormatRFC3339(time.Now()) - - // TODO roll our own event logger - logrus.WithFields(map[string]interface{}(accum)).Info(e.event) -}