Skip to content

Commit

Permalink
prune util/log and user logger (micro#1237)
Browse files Browse the repository at this point in the history
* prune util/log and user logger

Signed-off-by: Vasiliy Tolstov <[email protected]>

* plaintext logger

Signed-off-by: Vasiliy Tolstov <[email protected]>

* add newline

Signed-off-by: Vasiliy Tolstov <[email protected]>
  • Loading branch information
vtolstov authored Feb 23, 2020
1 parent ceed894 commit 117f56e
Show file tree
Hide file tree
Showing 46 changed files with 159 additions and 486 deletions.
4 changes: 2 additions & 2 deletions agent/input/discord/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions agent/input/telegram/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions api/handler/broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -136,7 +136,7 @@ func (c *conn) writeLoop() {
}()

if err != nil {
log.Log(err.Error())
log.Error(err.Error())
return
}

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions api/server/acme/certmagic/certmagic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package certmagic

import (
"net"
"net/http"
"os"
"reflect"
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions api/server/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions broker/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions broker/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion broker/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion broker/service/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion config/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion config/source/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
123 changes: 2 additions & 121 deletions debug/log/os.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down
Loading

0 comments on commit 117f56e

Please sign in to comment.