From ffe2bdceef3a56d365b528e490a18f9f1b710bf3 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet <juan@benet.ai> Date: Wed, 8 Oct 2014 02:50:36 -0700 Subject: [PATCH] init SetupLoggers this is useful so that loggers are all setup during tests --- cmd/ipfs/ipfs.go | 2 +- util/util.go | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cmd/ipfs/ipfs.go b/cmd/ipfs/ipfs.go index 7b75ba5c..8031f275 100644 --- a/cmd/ipfs/ipfs.go +++ b/cmd/ipfs/ipfs.go @@ -80,7 +80,7 @@ func main() { u.Debug = false // setup logging - u.SetupLogging() + // u.SetupLogging() done in an init() block now. // if debugging, setup profiling. if u.Debug { diff --git a/util/util.go b/util/util.go index 7a1a34b3..a2248837 100644 --- a/util/util.go +++ b/util/util.go @@ -17,8 +17,12 @@ import ( logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging" ) +func init() { + SetupLogging() +} + // LogFormat is the format used for our logger. -var LogFormat = "%{color}%{time:01-02 15:04:05.9999} %{shortfile} %{level}: %{color:reset}%{message}" +var LogFormat = "%{color}%{time:2006-01-02 15:04:05.999999} %{shortfile} %{level}: %{color:reset}%{message}" // Debug is a global flag for debugging. var Debug bool @@ -122,12 +126,14 @@ func DOut(format string, a ...interface{}) { } } -var loggers = []string{} +var loggers = map[string]*logging.Logger{} // SetupLogging will initialize the logger backend and set the flags. func SetupLogging() { backend := logging.NewLogBackend(os.Stderr, "", 0) logging.SetBackend(backend) + logging.SetFormatter(logging.MustStringFormatter(LogFormat)) + /* if Debug { logging.SetLevel(logging.DEBUG, "") @@ -135,10 +141,10 @@ func SetupLogging() { logging.SetLevel(logging.ERROR, "") } */ - logging.SetFormatter(logging.MustStringFormatter(LogFormat)) - for _, n := range loggers { + for n, log := range loggers { logging.SetLevel(logging.ERROR, n) + log.Error("setting logger: %s to %v\n", n, logging.ERROR) } } @@ -146,7 +152,7 @@ func SetupLogging() { func Logger(name string) *logging.Logger { log := logging.MustGetLogger(name) // logging.SetLevel(lvl, name) // can't set level here. - loggers = append(loggers, name) + loggers[name] = log return log } -- GitLab