From 117f56ebf7fac1ce97d3dba20253ad452c8436f2 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 23 Feb 2020 16:45:20 +0300 Subject: [PATCH] prune util/log and user logger (#1237) * prune util/log and user logger Signed-off-by: Vasiliy Tolstov * plaintext logger Signed-off-by: Vasiliy Tolstov * add newline Signed-off-by: Vasiliy Tolstov --- agent/input/discord/conn.go | 4 +- agent/input/telegram/conn.go | 4 +- api/handler/broker/broker.go | 6 +- api/server/acme/certmagic/certmagic_test.go | 4 + api/server/http/http.go | 4 +- broker/default.go | 6 +- broker/nats/nats.go | 4 +- broker/service/service.go | 2 +- broker/service/subscriber.go | 2 +- config/cmd/cmd.go | 2 +- config/source/service/service.go | 2 +- debug/log/os.go | 123 +----------- logger/default.go | 42 ++-- network/default.go | 2 +- proxy/mucp/mucp.go | 2 +- registry/cache/cache.go | 6 +- registry/etcd/etcd.go | 2 +- registry/kubernetes/watcher.go | 4 +- registry/memory/memory.go | 2 +- router/default.go | 12 +- router/default_test.go | 2 +- router/table.go | 2 +- runtime/default.go | 2 +- runtime/kubernetes/kubernetes.go | 2 +- runtime/kubernetes/service.go | 2 +- runtime/local/build/docker/docker.go | 2 +- runtime/service.go | 2 +- server/grpc/grpc.go | 28 +-- server/grpc/server.go | 20 +- server/grpc/subscriber.go | 6 +- server/rpc_router.go | 20 +- server/rpc_server.go | 44 ++--- server/server.go | 8 +- service.go | 4 +- service_test.go | 5 - store/cockroach/cockroach.go | 2 +- sync/cron.go | 8 +- transport/grpc/handler.go | 4 +- tunnel/default.go | 4 +- tunnel/link.go | 2 +- tunnel/listener.go | 2 +- tunnel/session.go | 2 +- util/kubernetes/client/client.go | 2 +- util/log/README.md | 15 -- util/log/log.go | 206 -------------------- web/service.go | 14 +- 46 files changed, 159 insertions(+), 486 deletions(-) delete mode 100644 util/log/README.md delete mode 100644 util/log/log.go diff --git a/agent/input/discord/conn.go b/agent/input/discord/conn.go index c7ca09ba..8d5d70d1 100644 --- a/agent/input/discord/conn.go +++ b/agent/input/discord/conn.go @@ -7,7 +7,7 @@ import ( "github.com/bwmarrin/discordgo" "github.com/micro/go-micro/v2/agent/input" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) type discordConn struct { @@ -74,7 +74,7 @@ func (dc *discordConn) Send(e *input.Event) error { fields := strings.Split(e.To, ":") _, err := dc.master.session.ChannelMessageSend(fields[0], string(e.Data)) if err != nil { - log.Log("[bot][loop][send]", err) + log.Error("[bot][loop][send]", err) } return nil } diff --git a/agent/input/telegram/conn.go b/agent/input/telegram/conn.go index 9dd594c8..41ecae72 100644 --- a/agent/input/telegram/conn.go +++ b/agent/input/telegram/conn.go @@ -7,7 +7,7 @@ import ( "github.com/forestgiant/sliceutil" "github.com/micro/go-micro/v2/agent/input" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" tgbotapi "gopkg.in/telegram-bot-api.v4" ) @@ -104,7 +104,7 @@ func (tc *telegramConn) Send(event *input.Event) error { if err != nil { // probably it could be because of nested HTML tags -- telegram doesn't allow nested tags - log.Log("[telegram][Send] error:", err) + log.Error("[telegram][Send] error:", err) msgConfig.Text = "This bot couldn't send the response (Internal error)" tc.input.api.Send(msgConfig) } diff --git a/api/handler/broker/broker.go b/api/handler/broker/broker.go index 29d78a02..6a503e37 100644 --- a/api/handler/broker/broker.go +++ b/api/handler/broker/broker.go @@ -14,7 +14,7 @@ import ( "github.com/gorilla/websocket" "github.com/micro/go-micro/v2/api/handler" "github.com/micro/go-micro/v2/broker" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) const ( @@ -136,7 +136,7 @@ func (c *conn) writeLoop() { }() if err != nil { - log.Log(err.Error()) + log.Error(err.Error()) return } @@ -214,7 +214,7 @@ func (b *brokerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ws, err := b.u.Upgrade(w, r, nil) if err != nil { - log.Log(err.Error()) + log.Error(err.Error()) return } diff --git a/api/server/acme/certmagic/certmagic_test.go b/api/server/acme/certmagic/certmagic_test.go index f91566b9..30d698c8 100644 --- a/api/server/acme/certmagic/certmagic_test.go +++ b/api/server/acme/certmagic/certmagic_test.go @@ -1,6 +1,7 @@ package certmagic import ( + "net" "net/http" "os" "reflect" @@ -21,6 +22,9 @@ func TestCertMagic(t *testing.T) { } l, err := NewProvider().Listen() if err != nil { + if _, ok := err.(*net.OpError); ok { + t.Skip("Run under non privileged user") + } t.Fatal(err.Error()) } l.Close() diff --git a/api/server/http/http.go b/api/server/http/http.go index f5240cda..f5fe5d24 100644 --- a/api/server/http/http.go +++ b/api/server/http/http.go @@ -10,7 +10,7 @@ import ( "github.com/gorilla/handlers" "github.com/micro/go-micro/v2/api/server" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) type httpServer struct { @@ -65,7 +65,7 @@ func (s *httpServer) Start() error { return err } - log.Logf("HTTP API Listening on %s", l.Addr().String()) + log.Infof("HTTP API Listening on %s", l.Addr().String()) s.mtx.Lock() s.address = l.Addr().String() diff --git a/broker/default.go b/broker/default.go index a71a5d17..b8b8ffe1 100644 --- a/broker/default.go +++ b/broker/default.go @@ -11,9 +11,9 @@ import ( "time" "github.com/micro/go-micro/v2/codec/json" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/util/addr" - "github.com/micro/go-micro/v2/util/log" "github.com/nats-io/nats-server/v2/server" nats "github.com/nats-io/nats.go" ) @@ -167,7 +167,7 @@ func (n *natsBroker) serve(exit chan bool) error { for _, node := range service.Nodes { u, err := url.Parse("nats://" + node.Address) if err != nil { - log.Log(err) + log.Info(err) continue } // append to the cluster routes @@ -242,7 +242,7 @@ func (n *natsBroker) serve(exit chan bool) error { select { case err := <-n.closeCh: if err != nil { - log.Log(err) + log.Info(err) } case <-exit: // deregister on exit diff --git a/broker/nats/nats.go b/broker/nats/nats.go index 2cce84e4..89bfd1e7 100644 --- a/broker/nats/nats.go +++ b/broker/nats/nats.go @@ -13,9 +13,9 @@ import ( "github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/codec/json" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/util/addr" - "github.com/micro/go-micro/v2/util/log" "github.com/nats-io/nats-server/v2/server" nats "github.com/nats-io/nats.go" ) @@ -164,7 +164,7 @@ func (n *natsBroker) serve(exit chan bool) error { for _, node := range service.Nodes { u, err := url.Parse("nats://" + node.Address) if err != nil { - log.Log(err) + log.Error(err) continue } // append to the cluster routes diff --git a/broker/service/service.go b/broker/service/service.go index 81c1fb28..a52400ba 100644 --- a/broker/service/service.go +++ b/broker/service/service.go @@ -8,7 +8,7 @@ import ( "github.com/micro/go-micro/v2/broker" pb "github.com/micro/go-micro/v2/broker/service/proto" "github.com/micro/go-micro/v2/client" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) type serviceBroker struct { diff --git a/broker/service/subscriber.go b/broker/service/subscriber.go index 36de7275..2e92ad43 100644 --- a/broker/service/subscriber.go +++ b/broker/service/subscriber.go @@ -3,7 +3,7 @@ package service import ( "github.com/micro/go-micro/v2/broker" pb "github.com/micro/go-micro/v2/broker/service/proto" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) type serviceSub struct { diff --git a/config/cmd/cmd.go b/config/cmd/cmd.go index 8aea177b..3c561146 100644 --- a/config/cmd/cmd.go +++ b/config/cmd/cmd.go @@ -12,12 +12,12 @@ import ( "github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/debug/trace" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/runtime" "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/transport" - "github.com/micro/go-micro/v2/util/log" // clients cgrpc "github.com/micro/go-micro/v2/client/grpc" diff --git a/config/source/service/service.go b/config/source/service/service.go index 567db30a..5e71ab17 100644 --- a/config/source/service/service.go +++ b/config/source/service/service.go @@ -6,7 +6,7 @@ import ( "github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/config/source" proto "github.com/micro/go-micro/v2/config/source/service/proto" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) var ( diff --git a/debug/log/os.go b/debug/log/os.go index 3073eacf..022481c2 100644 --- a/debug/log/os.go +++ b/debug/log/os.go @@ -1,13 +1,7 @@ package log import ( - "bufio" - "encoding/json" - "io" - "os" - "strings" "sync" - "time" "github.com/google/uuid" "github.com/micro/go-micro/v2/util/ring" @@ -25,106 +19,10 @@ type osLog struct { type osStream struct { stream chan Record - stop chan bool -} - -// watch io stream -func (o *osLog) run() { - // save outputs - stdout := *os.Stdout - stderr := *os.Stderr - - // new os pipe - r, w := io.Pipe() - - // create new iopipes - r1, w1, _ := os.Pipe() - r2, w2, _ := os.Pipe() - - // create tea readers - tee1 := io.TeeReader(r1, &stdout) - tee2 := io.TeeReader(r2, &stderr) - - // start copying - go io.Copy(w, tee1) - go io.Copy(w, tee2) - - // set default go log output - //log.SetOutput(w2) - - // replace os stdout and os stderr - *os.Stdout = *w1 - *os.Stderr = *w2 - - // this should short circuit everything - defer func() { - // reset stdout and stderr - *os.Stdout = stdout - *os.Stderr = stderr - //log.SetOutput(stderr) - - // close all the outputs - r.Close() - r1.Close() - r2.Close() - w.Close() - w1.Close() - w2.Close() - }() - - // read from standard error - scanner := bufio.NewReader(r) - - for { - // read the line - line, err := scanner.ReadString('\n') - if err != nil { - return - } - // check if the line exists - if len(line) == 0 { - continue - } - // parse the record - var r Record - if line[0] == '{' { - json.Unmarshal([]byte(line), &r) - } else { - r = Record{ - Timestamp: time.Now(), - Message: strings.TrimSuffix(line, "\n"), - Metadata: make(map[string]string), - } - } - - o.Lock() - - // write to the buffer - o.buffer.Put(r) - - // check subs and send to stream - for id, sub := range o.subs { - // send to stream - select { - case <-sub.stop: - delete(o.subs, id) - case sub.stream <- r: - // send to stream - default: - // do not block - } - } - - o.Unlock() - } } // Read reads log entries from the logger func (o *osLog) Read(...ReadOption) ([]Record, error) { - o.once.Do(func() { - go o.run() - }) - var records []Record // read the last 100 records @@ -137,29 +35,18 @@ func (o *osLog) Read(...ReadOption) ([]Record, error) { // Write writes records to log func (o *osLog) Write(r Record) error { - o.once.Do(func() { - go o.run() - }) - - // generate output - out := o.format(r) + "\n" - _, err := os.Stderr.Write([]byte(out)) - return err + o.buffer.Put(r) + return nil } // Stream log records func (o *osLog) Stream() (Stream, error) { - o.once.Do(func() { - go o.run() - }) - o.Lock() defer o.Unlock() // create stream st := &osStream{ stream: make(chan Record, 128), - stop: make(chan bool), } // save stream @@ -173,12 +60,6 @@ func (o *osStream) Chan() <-chan Record { } func (o *osStream) Stop() error { - select { - case <-o.stop: - return nil - default: - close(o.stop) - } return nil } diff --git a/logger/default.go b/logger/default.go index ec099c6c..3369f64e 100644 --- a/logger/default.go +++ b/logger/default.go @@ -2,10 +2,11 @@ package logger import ( "context" - "encoding/json" "fmt" - "log" "os" + "time" + + dlog "github.com/micro/go-micro/v2/debug/log" ) type defaultLogger struct { @@ -36,44 +37,57 @@ func (l *defaultLogger) Error(err error) Logger { } func (l *defaultLogger) Log(level Level, v ...interface{}) { + // TODO decide does we need to write message if log level not used? if !l.opts.Level.Enabled(level) { return } - msg := fmt.Sprint(v...) fields := l.opts.Fields fields["level"] = level.String() - fields["message"] = msg if l.err != nil { fields["error"] = l.err.Error() } - enc := json.NewEncoder(l.opts.Out) - - if err := enc.Encode(fields); err != nil { - log.Fatal(err) + rec := dlog.Record{ + Timestamp: time.Now(), + Message: fmt.Sprint(v...), + Metadata: make(map[string]string), } + for k, v := range fields { + rec.Metadata[k] = fmt.Sprintf("%v", v) + } + + dlog.DefaultLog.Write(rec) + + t := rec.Timestamp.Format("2006-01-02 15:04:05") + fmt.Printf("%s %v\n", t, rec.Message) } func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) { + // TODO decide does we need to write message if log level not used? if level < l.opts.Level { return } - msg := fmt.Sprintf(format, v...) fields := l.opts.Fields fields["level"] = level.String() - fields["message"] = msg if l.err != nil { fields["error"] = l.err.Error() } - enc := json.NewEncoder(l.opts.Out) - - if err := enc.Encode(fields); err != nil { - log.Fatal(err) + rec := dlog.Record{ + Timestamp: time.Now(), + Message: fmt.Sprintf(format, v...), + Metadata: make(map[string]string), + } + for k, v := range fields { + rec.Metadata[k] = fmt.Sprintf("%v", v) } + dlog.DefaultLog.Write(rec) + + t := rec.Timestamp.Format("2006-01-02 15:04:05") + fmt.Printf("%s %v\n", t, rec.Message) } func (n *defaultLogger) Options() Options { diff --git a/network/default.go b/network/default.go index 6fb347ae..5e116b85 100644 --- a/network/default.go +++ b/network/default.go @@ -15,6 +15,7 @@ import ( "github.com/micro/go-micro/v2/client" cmucp "github.com/micro/go-micro/v2/client/mucp" rtr "github.com/micro/go-micro/v2/client/selector/router" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/network/resolver/dns" pbNet "github.com/micro/go-micro/v2/network/service/proto" "github.com/micro/go-micro/v2/proxy" @@ -27,7 +28,6 @@ import ( bun "github.com/micro/go-micro/v2/tunnel/broker" tun "github.com/micro/go-micro/v2/tunnel/transport" "github.com/micro/go-micro/v2/util/backoff" - "github.com/micro/go-micro/v2/util/log" pbUtil "github.com/micro/go-micro/v2/util/proto" ) diff --git a/proxy/mucp/mucp.go b/proxy/mucp/mucp.go index 4cba3270..d9edf14a 100644 --- a/proxy/mucp/mucp.go +++ b/proxy/mucp/mucp.go @@ -15,11 +15,11 @@ import ( "github.com/micro/go-micro/v2/codec" "github.com/micro/go-micro/v2/codec/bytes" "github.com/micro/go-micro/v2/errors" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/proxy" "github.com/micro/go-micro/v2/router" "github.com/micro/go-micro/v2/server" - "github.com/micro/go-micro/v2/util/log" ) // Proxy will transparently proxy requests to an endpoint. diff --git a/registry/cache/cache.go b/registry/cache/cache.go index 51c3aac0..689c6e35 100644 --- a/registry/cache/cache.go +++ b/registry/cache/cache.go @@ -7,8 +7,8 @@ import ( "sync" "time" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" - log "github.com/micro/go-micro/v2/util/log" ) // Cache is the registry cache interface @@ -339,7 +339,7 @@ func (c *cache) run() { c.setStatus(err) if a > 3 { - log.Log("rcache: ", err, " backing off ", d) + log.Info("rcache: ", err, " backing off ", d) a = 0 } @@ -362,7 +362,7 @@ func (c *cache) run() { c.setStatus(err) if b > 3 { - log.Log("rcache: ", err, " backing off ", d) + log.Info("rcache: ", err, " backing off ", d) b = 0 } diff --git a/registry/etcd/etcd.go b/registry/etcd/etcd.go index b51a4010..40ba3b03 100644 --- a/registry/etcd/etcd.go +++ b/registry/etcd/etcd.go @@ -15,8 +15,8 @@ import ( "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" - "github.com/micro/go-micro/v2/util/log" hash "github.com/mitchellh/hashstructure" "go.uber.org/zap" ) diff --git a/registry/kubernetes/watcher.go b/registry/kubernetes/watcher.go index 0d848c4a..5c54a1b6 100644 --- a/registry/kubernetes/watcher.go +++ b/registry/kubernetes/watcher.go @@ -6,9 +6,9 @@ import ( "strings" "sync" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/util/kubernetes/client" - "github.com/micro/go-micro/v2/util/log" ) type k8sWatcher struct { @@ -132,7 +132,7 @@ func (k *k8sWatcher) buildPodResults(pod *client.Pod, cache *client.Pod) []*regi func (k *k8sWatcher) handleEvent(event client.Event) { var pod client.Pod if err := json.Unmarshal([]byte(event.Object), &pod); err != nil { - log.Log("K8s Watcher: Couldnt unmarshal event object from pod") + log.Info("K8s Watcher: Couldnt unmarshal event object from pod") return } diff --git a/registry/memory/memory.go b/registry/memory/memory.go index 2586d5e4..3dc96f46 100644 --- a/registry/memory/memory.go +++ b/registry/memory/memory.go @@ -7,8 +7,8 @@ import ( "time" "github.com/google/uuid" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" - "github.com/micro/go-micro/v2/util/log" ) var ( diff --git a/router/default.go b/router/default.go index 2187609a..d89ce13a 100644 --- a/router/default.go +++ b/router/default.go @@ -10,8 +10,8 @@ import ( "time" "github.com/google/uuid" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" - "github.com/micro/go-micro/v2/util/log" ) var ( @@ -357,14 +357,14 @@ func (r *router) advertiseEvents() error { // routing table watcher w, err = r.Watch() if err != nil { - log.Logf("Error creating watcher: %v", err) + log.Errorf("Error creating watcher: %v", err) time.Sleep(time.Second) continue } } if err := r.watchTable(w); err != nil { - log.Logf("Error watching table: %v", err) + log.Errorf("Error watching table: %v", err) time.Sleep(time.Second) } @@ -538,14 +538,14 @@ func (r *router) Start() error { if w == nil { w, err = r.options.Registry.Watch() if err != nil { - log.Logf("failed creating registry watcher: %v", err) + log.Errorf("failed creating registry watcher: %v", err) time.Sleep(time.Second) continue } } if err := r.watchRegistry(w); err != nil { - log.Logf("Error watching the registry: %v", err) + log.Errorf("Error watching the registry: %v", err) time.Sleep(time.Second) } @@ -602,7 +602,7 @@ func (r *router) Advertise() (<-chan *Advert, error) { return default: if err := r.advertiseEvents(); err != nil { - log.Logf("Error adveritising events: %v", err) + log.Errorf("Error adveritising events: %v", err) } } }() diff --git a/router/default_test.go b/router/default_test.go index c94ac9b4..42a79595 100644 --- a/router/default_test.go +++ b/router/default_test.go @@ -6,8 +6,8 @@ import ( "testing" "time" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry/memory" - "github.com/micro/go-micro/v2/util/log" ) func routerTestSetup() Router { diff --git a/router/table.go b/router/table.go index df17ee3e..649cea65 100644 --- a/router/table.go +++ b/router/table.go @@ -6,7 +6,7 @@ import ( "time" "github.com/google/uuid" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) var ( diff --git a/runtime/default.go b/runtime/default.go index 18247d89..fdd1ed25 100644 --- a/runtime/default.go +++ b/runtime/default.go @@ -5,7 +5,7 @@ import ( "sync" "time" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) type runtime struct { diff --git a/runtime/kubernetes/kubernetes.go b/runtime/kubernetes/kubernetes.go index aafa2f92..eabdb4b8 100644 --- a/runtime/kubernetes/kubernetes.go +++ b/runtime/kubernetes/kubernetes.go @@ -7,9 +7,9 @@ import ( "sync" "time" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/runtime" "github.com/micro/go-micro/v2/util/kubernetes/client" - "github.com/micro/go-micro/v2/util/log" ) // action to take on runtime service diff --git a/runtime/kubernetes/service.go b/runtime/kubernetes/service.go index 0fafd962..60fb31a5 100644 --- a/runtime/kubernetes/service.go +++ b/runtime/kubernetes/service.go @@ -5,10 +5,10 @@ import ( "strings" "time" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/runtime" "github.com/micro/go-micro/v2/util/kubernetes/api" "github.com/micro/go-micro/v2/util/kubernetes/client" - "github.com/micro/go-micro/v2/util/log" ) type service struct { diff --git a/runtime/local/build/docker/docker.go b/runtime/local/build/docker/docker.go index bb953a71..01bc18a4 100644 --- a/runtime/local/build/docker/docker.go +++ b/runtime/local/build/docker/docker.go @@ -9,8 +9,8 @@ import ( "path/filepath" docker "github.com/fsouza/go-dockerclient" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/runtime/local/build" - "github.com/micro/go-micro/v2/util/log" ) type Builder struct { diff --git a/runtime/service.go b/runtime/service.go index 8ddd1bcc..c93bec73 100644 --- a/runtime/service.go +++ b/runtime/service.go @@ -6,10 +6,10 @@ import ( "sync" "time" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/runtime/local/build" "github.com/micro/go-micro/v2/runtime/local/process" proc "github.com/micro/go-micro/v2/runtime/local/process/os" - "github.com/micro/go-micro/v2/util/log" ) type service struct { diff --git a/server/grpc/grpc.go b/server/grpc/grpc.go index 735b69ff..5b434200 100644 --- a/server/grpc/grpc.go +++ b/server/grpc/grpc.go @@ -17,12 +17,12 @@ import ( "github.com/golang/protobuf/proto" "github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/errors" + log "github.com/micro/go-micro/v2/logger" meta "github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/util/addr" mgrpc "github.com/micro/go-micro/v2/util/grpc" - "github.com/micro/go-micro/v2/util/log" mnet "github.com/micro/go-micro/v2/util/net" "google.golang.org/grpc" @@ -356,8 +356,8 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service, fn := func(ctx context.Context, req server.Request, rsp interface{}) (err error) { defer func() { if r := recover(); r != nil { - log.Log("panic recovered: ", r) - log.Logf(string(debug.Stack())) + log.Error("panic recovered: ", r) + log.Error(string(debug.Stack())) err = errors.InternalServerError("go.micro.server", "panic recovered: %v", r) } }() @@ -656,7 +656,7 @@ func (g *grpcServer) Register() error { g.Unlock() if !registered { - log.Logf("Registry [%s] Registering node: %s", config.Registry.String(), node.Id) + log.Infof("Registry [%s] Registering node: %s", config.Registry.String(), node.Id) } // create registry options @@ -691,7 +691,7 @@ func (g *grpcServer) Register() error { opts = append(opts, broker.DisableAutoAck()) } - log.Logf("Subscribing to topic: %s", sb.Topic()) + log.Infof("Subscribing to topic: %s", sb.Topic()) sub, err := config.Broker.Subscribe(sb.Topic(), handler, opts...) if err != nil { return err @@ -743,7 +743,7 @@ func (g *grpcServer) Deregister() error { Nodes: []*registry.Node{node}, } - log.Logf("Deregistering node: %s", node.Id) + log.Infof("Deregistering node: %s", node.Id) if err := config.Registry.Deregister(service); err != nil { return err } @@ -759,7 +759,7 @@ func (g *grpcServer) Deregister() error { for sb, subs := range g.subscribers { for _, sub := range subs { - log.Logf("Unsubscribing from topic: %s", sub.Topic()) + log.Infof("Unsubscribing from topic: %s", sub.Topic()) sub.Unsubscribe() } g.subscribers[sb] = nil @@ -799,7 +799,7 @@ func (g *grpcServer) Start() error { } } - log.Logf("Server [grpc] Listening on %s", ts.Addr().String()) + log.Infof("Server [grpc] Listening on %s", ts.Addr().String()) g.Lock() g.opts.Address = ts.Addr().String() g.Unlock() @@ -811,18 +811,18 @@ func (g *grpcServer) Start() error { return err } - log.Logf("Broker [%s] Connected to %s", config.Broker.String(), config.Broker.Address()) + log.Infof("Broker [%s] Connected to %s", config.Broker.String(), config.Broker.Address()) } // announce self to the world if err := g.Register(); err != nil { - log.Log("Server register error: ", err) + log.Errorf("Server register error: ", err) } // micro: go ts.Accept(s.accept) go func() { if err := g.srv.Serve(ts); err != nil { - log.Log("gRPC Server start error: ", err) + log.Errorf("gRPC Server start error: ", err) } }() @@ -844,7 +844,7 @@ func (g *grpcServer) Start() error { // register self on interval case <-t.C: if err := g.Register(); err != nil { - log.Log("Server register error: ", err) + log.Error("Server register error: ", err) } // wait for exit case ch = <-g.exit: @@ -854,7 +854,7 @@ func (g *grpcServer) Start() error { // deregister self if err := g.Deregister(); err != nil { - log.Log("Server deregister error: ", err) + log.Error("Server deregister error: ", err) } // wait for waitgroup @@ -879,7 +879,7 @@ func (g *grpcServer) Start() error { // close transport ch <- nil - log.Logf("Broker [%s] Disconnected from %s", config.Broker.String(), config.Broker.Address()) + log.Infof("Broker [%s] Disconnected from %s", config.Broker.String(), config.Broker.Address()) // disconnect broker config.Broker.Disconnect() }() diff --git a/server/grpc/server.go b/server/grpc/server.go index 6d43ba17..81bf9f7c 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -14,8 +14,8 @@ import ( "unicode" "unicode/utf8" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/server" - "github.com/micro/go-micro/v2/util/log" ) var ( @@ -86,7 +86,7 @@ func prepareEndpoint(method reflect.Method) *methodType { replyType = mtype.In(3) contextType = mtype.In(1) default: - log.Log("method", mname, "of", mtype, "has wrong number of ins:", mtype.NumIn()) + log.Error("method", mname, "of", mtype, "has wrong number of ins:", mtype.NumIn()) return nil } @@ -94,7 +94,7 @@ func prepareEndpoint(method reflect.Method) *methodType { // check stream type streamType := reflect.TypeOf((*server.Stream)(nil)).Elem() if !argType.Implements(streamType) { - log.Log(mname, "argument does not implement Streamer interface:", argType) + log.Error(mname, "argument does not implement Streamer interface:", argType) return nil } } else { @@ -102,30 +102,30 @@ func prepareEndpoint(method reflect.Method) *methodType { // First arg need not be a pointer. if !isExportedOrBuiltinType(argType) { - log.Log(mname, "argument type not exported:", argType) + log.Error(mname, "argument type not exported:", argType) return nil } if replyType.Kind() != reflect.Ptr { - log.Log("method", mname, "reply type not a pointer:", replyType) + log.Error("method", mname, "reply type not a pointer:", replyType) return nil } // Reply type must be exported. if !isExportedOrBuiltinType(replyType) { - log.Log("method", mname, "reply type not exported:", replyType) + log.Error("method", mname, "reply type not exported:", replyType) return nil } } // Endpoint() needs one out. if mtype.NumOut() != 1 { - log.Log("method", mname, "has wrong number of outs:", mtype.NumOut()) + log.Error("method", mname, "has wrong number of outs:", mtype.NumOut()) return nil } // The return type of the method must be error. if returnType := mtype.Out(0); returnType != typeOfError { - log.Log("method", mname, "returns", returnType.String(), "not error") + log.Error("method", mname, "returns", returnType.String(), "not error") return nil } return &methodType{method: method, ArgType: argType, ReplyType: replyType, ContextType: contextType, stream: stream} @@ -146,7 +146,7 @@ func (server *rServer) register(rcvr interface{}) error { } if !isExported(sname) { s := "rpc Register: type " + sname + " is not exported" - log.Log(s) + log.Error(s) return errors.New(s) } if _, present := server.serviceMap[sname]; present { @@ -165,7 +165,7 @@ func (server *rServer) register(rcvr interface{}) error { if len(s.method) == 0 { s := "rpc Register: type " + sname + " has no exported methods of suitable type" - log.Log(s) + log.Error(s) return errors.New(s) } server.serviceMap[s.name] = s diff --git a/server/grpc/subscriber.go b/server/grpc/subscriber.go index c628a6e0..7bd39a5f 100644 --- a/server/grpc/subscriber.go +++ b/server/grpc/subscriber.go @@ -9,10 +9,10 @@ import ( "github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/errors" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/server" - "github.com/micro/go-micro/v2/util/log" ) const ( @@ -171,8 +171,8 @@ func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broke defer func() { if r := recover(); r != nil { - log.Log("panic recovered: ", r) - log.Logf(string(debug.Stack())) + log.Error("panic recovered: ", r) + log.Error(string(debug.Stack())) err = errors.InternalServerError("go.micro.server", "panic recovered: %v", r) } }() diff --git a/server/rpc_router.go b/server/rpc_router.go index 8d36f1f0..3716617d 100644 --- a/server/rpc_router.go +++ b/server/rpc_router.go @@ -20,7 +20,7 @@ import ( "github.com/micro/go-micro/v2/codec" merrors "github.com/micro/go-micro/v2/errors" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) var ( @@ -141,7 +141,7 @@ func prepareMethod(method reflect.Method) *methodType { replyType = mtype.In(3) contextType = mtype.In(1) default: - log.Log("method", mname, "of", mtype, "has wrong number of ins:", mtype.NumIn()) + log.Error("method", mname, "of", mtype, "has wrong number of ins:", mtype.NumIn()) return nil } @@ -149,7 +149,7 @@ func prepareMethod(method reflect.Method) *methodType { // check stream type streamType := reflect.TypeOf((*Stream)(nil)).Elem() if !argType.Implements(streamType) { - log.Log(mname, "argument does not implement Stream interface:", argType) + log.Error(mname, "argument does not implement Stream interface:", argType) return nil } } else { @@ -157,30 +157,30 @@ func prepareMethod(method reflect.Method) *methodType { // First arg need not be a pointer. if !isExportedOrBuiltinType(argType) { - log.Log(mname, "argument type not exported:", argType) + log.Error(mname, "argument type not exported:", argType) return nil } if replyType.Kind() != reflect.Ptr { - log.Log("method", mname, "reply type not a pointer:", replyType) + log.Error("method", mname, "reply type not a pointer:", replyType) return nil } // Reply type must be exported. if !isExportedOrBuiltinType(replyType) { - log.Log("method", mname, "reply type not exported:", replyType) + log.Error("method", mname, "reply type not exported:", replyType) return nil } } // Method needs one out. if mtype.NumOut() != 1 { - log.Log("method", mname, "has wrong number of outs:", mtype.NumOut()) + log.Error("method", mname, "has wrong number of outs:", mtype.NumOut()) return nil } // The return type of the method must be error. if returnType := mtype.Out(0); returnType != typeOfError { - log.Log("method", mname, "returns", returnType.String(), "not error") + log.Error("method", mname, "returns", returnType.String(), "not error") return nil } return &methodType{method: method, ArgType: argType, ReplyType: replyType, ContextType: contextType, stream: stream} @@ -509,8 +509,8 @@ func (router *router) ProcessMessage(ctx context.Context, msg Message) (err erro defer func() { // recover any panics if r := recover(); r != nil { - log.Log("panic recovered: ", r) - log.Log(string(debug.Stack())) + log.Error("panic recovered: ", r) + log.Error(string(debug.Stack())) err = merrors.InternalServerError("go.micro.server", "panic recovered: %v", r) } }() diff --git a/server/rpc_server.go b/server/rpc_server.go index 6c0c793d..a692573a 100644 --- a/server/rpc_server.go +++ b/server/rpc_server.go @@ -15,11 +15,11 @@ import ( "github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/codec" raw "github.com/micro/go-micro/v2/codec/bytes" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/transport" "github.com/micro/go-micro/v2/util/addr" - log "github.com/micro/go-micro/v2/util/log" mnet "github.com/micro/go-micro/v2/util/net" "github.com/micro/go-micro/v2/util/socket" ) @@ -158,8 +158,8 @@ func (s *rpcServer) ServeConn(sock transport.Socket) { // recover any panics if r := recover(); r != nil { - log.Log("panic recovered: ", r) - log.Log(string(debug.Stack())) + log.Error("panic recovered: ", r) + log.Error(string(debug.Stack())) } }() @@ -377,8 +377,8 @@ func (s *rpcServer) ServeConn(sock transport.Socket) { // recover any panics for outbound process if r := recover(); r != nil { - log.Log("panic recovered: ", r) - log.Log(string(debug.Stack())) + log.Error("panic recovered: ", r) + log.Error(string(debug.Stack())) } }() @@ -409,8 +409,8 @@ func (s *rpcServer) ServeConn(sock transport.Socket) { // recover any panics for call handler if r := recover(); r != nil { - log.Log("panic recovered: ", r) - log.Log(string(debug.Stack())) + log.Error("panic recovered: ", r) + log.Error(string(debug.Stack())) } }() @@ -608,7 +608,7 @@ func (s *rpcServer) Register() error { s.RUnlock() if !registered { - log.Logf("Registry [%s] Registering node: %s", config.Registry.String(), node.Id) + log.Infof("Registry [%s] Registering node: %s", config.Registry.String(), node.Id) } // create registry options @@ -654,7 +654,7 @@ func (s *rpcServer) Register() error { opts = append(opts, broker.DisableAutoAck()) } - log.Logf("Subscribing to topic: %s", sub.Topic()) + log.Infof("Subscribing to topic: %s", sub.Topic()) sub, err := config.Broker.Subscribe(sb.Topic(), s.HandleEvent, opts...) if err != nil { return err @@ -712,7 +712,7 @@ func (s *rpcServer) Deregister() error { Nodes: []*registry.Node{node}, } - log.Logf("Registry [%s] Deregistering node: %s", config.Registry.String(), node.Id) + log.Infof("Registry [%s] Deregistering node: %s", config.Registry.String(), node.Id) if err := config.Registry.Deregister(service); err != nil { return err } @@ -734,7 +734,7 @@ func (s *rpcServer) Deregister() error { for sb, subs := range s.subscribers { for _, sub := range subs { - log.Logf("Unsubscribing %s from topic: %s", node.Id, sub.Topic()) + log.Infof("Unsubscribing %s from topic: %s", node.Id, sub.Topic()) sub.Unsubscribe() } s.subscribers[sb] = nil @@ -760,7 +760,7 @@ func (s *rpcServer) Start() error { return err } - log.Logf("Transport [%s] Listening on %s", config.Transport.String(), ts.Addr()) + log.Infof("Transport [%s] Listening on %s", config.Transport.String(), ts.Addr()) // swap address s.Lock() @@ -775,15 +775,15 @@ func (s *rpcServer) Start() error { bname := config.Broker.String() - log.Logf("Broker [%s] Connected to %s", bname, config.Broker.Address()) + log.Infof("Broker [%s] Connected to %s", bname, config.Broker.Address()) // use RegisterCheck func before register if err = s.opts.RegisterCheck(s.opts.Context); err != nil { - log.Logf("Server %s-%s register check error: %s", config.Name, config.Id, err) + log.Errorf("Server %s-%s register check error: %s", config.Name, config.Id, err) } else { // announce self to the world if err = s.Register(); err != nil { - log.Logf("Server %s-%s register error: %s", config.Name, config.Id, err) + log.Errorf("Server %s-%s register error: %s", config.Name, config.Id, err) } } @@ -804,7 +804,7 @@ func (s *rpcServer) Start() error { // check the error and backoff default: if err != nil { - log.Logf("Accept error: %v", err) + log.Errorf("Accept error: %v", err) time.Sleep(time.Second) continue } @@ -837,17 +837,17 @@ func (s *rpcServer) Start() error { s.RUnlock() rerr := s.opts.RegisterCheck(s.opts.Context) if rerr != nil && registered { - log.Logf("Server %s-%s register check error: %s, deregister it", config.Name, config.Id, err) + log.Errorf("Server %s-%s register check error: %s, deregister it", config.Name, config.Id, err) // deregister self in case of error if err := s.Deregister(); err != nil { - log.Logf("Server %s-%s deregister error: %s", config.Name, config.Id, err) + log.Errorf("Server %s-%s deregister error: %s", config.Name, config.Id, err) } } else if rerr != nil && !registered { - log.Logf("Server %s-%s register check error: %s", config.Name, config.Id, err) + log.Errorf("Server %s-%s register check error: %s", config.Name, config.Id, err) continue } if err := s.Register(); err != nil { - log.Logf("Server %s-%s register error: %s", config.Name, config.Id, err) + log.Errorf("Server %s-%s register error: %s", config.Name, config.Id, err) } // wait for exit case ch = <-s.exit: @@ -863,7 +863,7 @@ func (s *rpcServer) Start() error { if registered { // deregister self if err := s.Deregister(); err != nil { - log.Logf("Server %s-%s deregister error: %s", config.Name, config.Id, err) + log.Errorf("Server %s-%s deregister error: %s", config.Name, config.Id, err) } } @@ -879,7 +879,7 @@ func (s *rpcServer) Start() error { // close transport listener ch <- ts.Close() - log.Logf("Broker [%s] Disconnected from %s", bname, config.Broker.Address()) + log.Infof("Broker [%s] Disconnected from %s", bname, config.Broker.Address()) // disconnect the broker config.Broker.Disconnect() diff --git a/server/server.go b/server/server.go index a45dd8d0..e11bc14d 100644 --- a/server/server.go +++ b/server/server.go @@ -10,8 +10,8 @@ import ( "github.com/google/uuid" "github.com/micro/go-micro/v2/codec" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" - log "github.com/micro/go-micro/v2/util/log" ) // Server is a simple micro server abstraction @@ -200,7 +200,7 @@ func Run() error { ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT) - log.Logf("Received signal %s", <-ch) + log.Infof("Received signal %s", <-ch) return Stop() } @@ -208,13 +208,13 @@ func Run() error { // Start starts the default server func Start() error { config := DefaultServer.Options() - log.Logf("Starting server %s id %s", config.Name, config.Id) + log.Infof("Starting server %s id %s", config.Name, config.Id) return DefaultServer.Start() } // Stop stops the default server func Stop() error { - log.Logf("Stopping server") + log.Infof("Stopping server") return DefaultServer.Stop() } diff --git a/service.go b/service.go index 678925d9..90fdd8ca 100644 --- a/service.go +++ b/service.go @@ -17,9 +17,9 @@ import ( "github.com/micro/go-micro/v2/debug/service/handler" "github.com/micro/go-micro/v2/debug/stats" "github.com/micro/go-micro/v2/debug/trace" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/plugin" "github.com/micro/go-micro/v2/server" - "github.com/micro/go-micro/v2/util/log" "github.com/micro/go-micro/v2/util/wrapper" ) @@ -202,7 +202,7 @@ func (s *service) Run() error { defer profiler.Stop() } - log.Logf("Starting [service] %s", s.Name()) + log.Infof("Starting [service] %s", s.Name()) if err := s.Start(); err != nil { return err diff --git a/service_test.go b/service_test.go index 3c97c6a0..0c3d1829 100644 --- a/service_test.go +++ b/service_test.go @@ -7,10 +7,8 @@ import ( "testing" "github.com/micro/go-micro/v2/client" - "github.com/micro/go-micro/v2/debug/log/noop" proto "github.com/micro/go-micro/v2/debug/service/proto" "github.com/micro/go-micro/v2/registry/memory" - "github.com/micro/go-micro/v2/util/log" "github.com/micro/go-micro/v2/util/test" ) @@ -24,9 +22,6 @@ func testShutdown(wg *sync.WaitGroup, cancel func()) { } func testService(ctx context.Context, wg *sync.WaitGroup, name string) Service { - // set no op logger - log.SetLogger(noop.NewLog()) - // add self wg.Add(1) diff --git a/store/cockroach/cockroach.go b/store/cockroach/cockroach.go index faaf9e2a..0e0dc38e 100644 --- a/store/cockroach/cockroach.go +++ b/store/cockroach/cockroach.go @@ -10,8 +10,8 @@ import ( "unicode" "github.com/lib/pq" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/store" - "github.com/micro/go-micro/v2/util/log" "github.com/pkg/errors" ) diff --git a/sync/cron.go b/sync/cron.go index bc3133b6..caf51e71 100644 --- a/sync/cron.go +++ b/sync/cron.go @@ -5,10 +5,10 @@ import ( "math" "time" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/sync/leader/etcd" "github.com/micro/go-micro/v2/sync/task" "github.com/micro/go-micro/v2/sync/task/local" - "github.com/micro/go-micro/v2/util/log" ) type syncCron struct { @@ -35,7 +35,7 @@ func (c *syncCron) Schedule(s task.Schedule, t task.Command) error { // leader election e, err := c.opts.Leader.Elect(id) if err != nil { - log.Logf("[cron] leader election error: %v", err) + log.Errorf("[cron] leader election error: %v", err) time.Sleep(backoff(i)) i++ continue @@ -55,9 +55,9 @@ func (c *syncCron) Schedule(s task.Schedule, t task.Command) error { break Tick } - log.Logf("[cron] executing command %s", t.Name) + log.Infof("[cron] executing command %s", t.Name) if err := c.opts.Task.Run(t); err != nil { - log.Logf("[cron] error executing command %s: %v", t.Name, err) + log.Errorf("[cron] error executing command %s: %v", t.Name, err) } // leader revoked case <-r: diff --git a/transport/grpc/handler.go b/transport/grpc/handler.go index ce3690f3..d5a626a0 100644 --- a/transport/grpc/handler.go +++ b/transport/grpc/handler.go @@ -4,9 +4,9 @@ import ( "runtime/debug" "github.com/micro/go-micro/v2/errors" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/transport" pb "github.com/micro/go-micro/v2/transport/grpc/proto" - "github.com/micro/go-micro/v2/util/log" "google.golang.org/grpc/peer" ) @@ -30,7 +30,7 @@ func (m *microTransport) Stream(ts pb.Transport_StreamServer) (err error) { defer func() { if r := recover(); r != nil { - log.Log(r, string(debug.Stack())) + log.Error(r, string(debug.Stack())) sock.Close() err = errors.InternalServerError("go.micro.transport", "panic recovered: %v", r) } diff --git a/tunnel/default.go b/tunnel/default.go index 0bde3bde..523d1978 100644 --- a/tunnel/default.go +++ b/tunnel/default.go @@ -8,8 +8,8 @@ import ( "time" "github.com/google/uuid" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/transport" - "github.com/micro/go-micro/v2/util/log" ) var ( @@ -949,7 +949,7 @@ func (t *tun) connect() error { // still connected but the tunnel died if err != nil && t.connected { - log.Logf("Tunnel listener died: %v", err) + log.Errorf("Tunnel listener died: %v", err) } }() diff --git a/tunnel/link.go b/tunnel/link.go index 3df54cc5..e698a280 100644 --- a/tunnel/link.go +++ b/tunnel/link.go @@ -8,8 +8,8 @@ import ( "time" "github.com/google/uuid" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/transport" - "github.com/micro/go-micro/v2/util/log" ) type link struct { diff --git a/tunnel/listener.go b/tunnel/listener.go index ac5ba3dc..3aff7b85 100644 --- a/tunnel/listener.go +++ b/tunnel/listener.go @@ -4,7 +4,7 @@ import ( "io" "sync" - "github.com/micro/go-micro/v2/util/log" + log "github.com/micro/go-micro/v2/logger" ) type tunListener struct { diff --git a/tunnel/session.go b/tunnel/session.go index f662b1df..fc5b9be9 100644 --- a/tunnel/session.go +++ b/tunnel/session.go @@ -5,8 +5,8 @@ import ( "io" "time" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/transport" - "github.com/micro/go-micro/v2/util/log" ) // session is our pseudo session for transport.Socket diff --git a/util/kubernetes/client/client.go b/util/kubernetes/client/client.go index 01bf48cc..1e702a32 100644 --- a/util/kubernetes/client/client.go +++ b/util/kubernetes/client/client.go @@ -12,8 +12,8 @@ import ( "path" "strings" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/util/kubernetes/api" - "github.com/micro/go-micro/v2/util/log" ) var ( diff --git a/util/log/README.md b/util/log/README.md deleted file mode 100644 index b5b9c5df..00000000 --- a/util/log/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# Log - -This is the global logger for all micro based libraries. - -## Set Logger - -Set the logger for micro libraries - -```go -// import go-micro/util/log -import "github.com/micro/go-micro/util/log" - -// SetLogger expects github.com/micro/go-micro/debug/log.Log interface -log.SetLogger(mylogger) -``` diff --git a/util/log/log.go b/util/log/log.go deleted file mode 100644 index 67c3b75c..00000000 --- a/util/log/log.go +++ /dev/null @@ -1,206 +0,0 @@ -// Package log is a global internal logger -package log - -import ( - "fmt" - "os" - "sync/atomic" - "time" - - "github.com/micro/go-micro/v2/debug/log" -) - -// level is a log level -type Level int32 - -const ( - LevelFatal Level = iota - LevelError - LevelWarn - LevelInfo - LevelDebug - LevelTrace -) - -var ( - // the local logger - logger log.Log = log.DefaultLog - - // default log level is info - level = LevelInfo - - // prefix for all messages - prefix string -) - -func init() { - switch os.Getenv("MICRO_LOG_LEVEL") { - case "trace": - level = LevelTrace - case "debug": - level = LevelDebug - case "warn": - level = LevelWarn - case "info": - level = LevelInfo - case "error": - level = LevelError - case "fatal": - level = LevelFatal - } -} - -func (l Level) String() string { - switch l { - case LevelTrace: - return "trace" - case LevelDebug: - return "debug" - case LevelWarn: - return "warn" - case LevelInfo: - return "info" - case LevelError: - return "error" - case LevelFatal: - return "fatal" - default: - return "unknown" - } -} - -// Log makes use of github.com/micro/debug/log -func Log(v ...interface{}) { - if len(prefix) > 0 { - v = append([]interface{}{prefix, " "}, v...) - } - logger.Write(log.Record{ - Timestamp: time.Now(), - Message: fmt.Sprint(v...), - Metadata: map[string]string{ - "level": level.String(), - }, - }) -} - -// Logf makes use of github.com/micro/debug/log -func Logf(format string, v ...interface{}) { - if len(prefix) > 0 { - format = prefix + " " + format - } - logger.Write(log.Record{ - Timestamp: time.Now(), - Message: fmt.Sprintf(format, v...), - Metadata: map[string]string{ - "level": level.String(), - }, - }) -} - -// WithLevel logs with the level specified -func WithLevel(l Level, v ...interface{}) { - if l > level { - return - } - Log(v...) -} - -// WithLevel logs with the level specified -func WithLevelf(l Level, format string, v ...interface{}) { - if l > level { - return - } - Logf(format, v...) -} - -// Trace provides trace level logging -func Trace(v ...interface{}) { - WithLevel(LevelTrace, v...) -} - -// Tracef provides trace level logging -func Tracef(format string, v ...interface{}) { - WithLevelf(LevelTrace, format, v...) -} - -// Debug provides debug level logging -func Debug(v ...interface{}) { - WithLevel(LevelDebug, v...) -} - -// Debugf provides debug level logging -func Debugf(format string, v ...interface{}) { - WithLevelf(LevelDebug, format, v...) -} - -// Warn provides warn level logging -func Warn(v ...interface{}) { - WithLevel(LevelWarn, v...) -} - -// Warnf provides warn level logging -func Warnf(format string, v ...interface{}) { - WithLevelf(LevelWarn, format, v...) -} - -// Info provides info level logging -func Info(v ...interface{}) { - WithLevel(LevelInfo, v...) -} - -// Infof provides info level logging -func Infof(format string, v ...interface{}) { - WithLevelf(LevelInfo, format, v...) -} - -// Error provides warn level logging -func Error(v ...interface{}) { - WithLevel(LevelError, v...) -} - -// Errorf provides warn level logging -func Errorf(format string, v ...interface{}) { - WithLevelf(LevelError, format, v...) -} - -// Fatal logs with Log and then exits with os.Exit(1) -func Fatal(v ...interface{}) { - WithLevel(LevelFatal, v...) - os.Exit(1) -} - -// Fatalf logs with Logf and then exits with os.Exit(1) -func Fatalf(format string, v ...interface{}) { - WithLevelf(LevelFatal, format, v...) - os.Exit(1) -} - -// SetLogger sets the local logger -func SetLogger(l log.Log) { - logger = l -} - -// GetLogger returns the local logger -func GetLogger() log.Log { - return logger -} - -// SetLevel sets the log level -func SetLevel(l Level) { - atomic.StoreInt32((*int32)(&level), int32(l)) -} - -// GetLevel returns the current level -func GetLevel() Level { - return level -} - -// Set a prefix for the logger -func SetPrefix(p string) { - prefix = p -} - -// Set service name -func Name(name string) { - prefix = fmt.Sprintf("[%s]", name) -} diff --git a/web/service.go b/web/service.go index aa351236..e9837892 100644 --- a/web/service.go +++ b/web/service.go @@ -16,10 +16,10 @@ import ( "github.com/micro/cli/v2" "github.com/micro/go-micro/v2" + log "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" maddr "github.com/micro/go-micro/v2/util/addr" mhttp "github.com/micro/go-micro/v2/util/http" - "github.com/micro/go-micro/v2/util/log" mnet "github.com/micro/go-micro/v2/util/net" mls "github.com/micro/go-micro/v2/util/tls" ) @@ -126,7 +126,7 @@ func (s *service) register() error { // use RegisterCheck func before register if err := s.opts.RegisterCheck(s.opts.Context); err != nil { - log.Logf("Server %s-%s register check error: %s", s.opts.Name, s.opts.Id, err) + log.Errorf("Server %s-%s register check error: %s", s.opts.Name, s.opts.Id, err) return err } @@ -185,7 +185,7 @@ func (s *service) start() error { if s.static { _, err := os.Stat(static) if err == nil { - log.Logf("Enabling static file serving from %s", static) + log.Infof("Enabling static file serving from %s", static) s.mux.Handle("/", http.FileServer(http.Dir(static))) } } @@ -223,7 +223,7 @@ func (s *service) start() error { ch <- l.Close() }() - log.Logf("Listening on %v", l.Addr().String()) + log.Infof("Listening on %v", l.Addr().String()) return nil } @@ -245,7 +245,7 @@ func (s *service) stop() error { s.exit <- ch s.running = false - log.Log("Stopping") + log.Info("Stopping") for _, fn := range s.opts.AfterStop { if err := fn(); err != nil { @@ -392,10 +392,10 @@ func (s *service) Run() error { select { // wait on kill signal case sig := <-ch: - log.Logf("Received signal %s", sig) + log.Infof("Received signal %s", sig) // wait on context cancel case <-s.opts.Context.Done(): - log.Logf("Received context shutdown") + log.Info("Received context shutdown") } // exit reg loop