2019-12-17 18:38:03 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2019-12-17 18:46:09 +03:00
|
|
|
"github.com/micro/go-micro/debug"
|
2019-12-17 18:38:03 +03:00
|
|
|
"github.com/micro/go-micro/debug/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type serviceLog struct {
|
|
|
|
Client *debugClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads log entries from the logger
|
|
|
|
func (s *serviceLog) Read(opts ...log.ReadOption) []log.Record {
|
|
|
|
// TODO: parse opts
|
|
|
|
stream, err := s.Client.Log(opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// stream the records until nothing is left
|
|
|
|
var records []log.Record
|
2019-12-17 18:46:09 +03:00
|
|
|
for record := range stream {
|
2019-12-17 18:38:03 +03:00
|
|
|
records = append(records, record)
|
|
|
|
}
|
|
|
|
return records
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no write support
|
|
|
|
func (s *serviceLog) Write(r log.Record) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream log records
|
2019-12-17 18:46:09 +03:00
|
|
|
func (s *serviceLog) Stream() (<-chan log.Record, chan bool) {
|
2019-12-17 18:38:03 +03:00
|
|
|
stop := make(chan bool)
|
|
|
|
stream, err := s.Client.Log(log.Stream(true))
|
|
|
|
if err != nil {
|
|
|
|
// return a closed stream
|
2019-12-17 18:46:09 +03:00
|
|
|
deadStream := make(chan log.Record)
|
|
|
|
close(deadStream)
|
|
|
|
return deadStream, stop
|
2019-12-17 18:38:03 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 18:46:09 +03:00
|
|
|
newStream := make(chan log.Record, 128)
|
|
|
|
|
2019-12-17 18:38:03 +03:00
|
|
|
go func() {
|
2019-12-17 18:46:09 +03:00
|
|
|
for {
|
2019-12-17 18:38:03 +03:00
|
|
|
select {
|
2019-12-17 18:46:09 +03:00
|
|
|
case rec := <-stream:
|
|
|
|
newStream <- rec
|
2019-12-17 18:38:03 +03:00
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-12-17 18:46:09 +03:00
|
|
|
return newStream, stop
|
2019-12-17 18:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLog returns a new log interface
|
|
|
|
func NewLog(opts ...log.Option) log.Log {
|
|
|
|
var options log.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := options.Name
|
|
|
|
|
|
|
|
// set the default name
|
|
|
|
if len(name) == 0 {
|
|
|
|
name = debug.DefaultName
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:46:09 +03:00
|
|
|
return &serviceLog{
|
2019-12-17 18:56:49 +03:00
|
|
|
Client: NewClient(name),
|
2019-12-17 18:38:03 +03:00
|
|
|
}
|
|
|
|
}
|