2019-11-27 20:32:54 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
"github.com/micro/go-micro/debug"
|
2019-11-27 20:32:54 +00:00
|
|
|
"github.com/micro/go-micro/debug/log"
|
|
|
|
)
|
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
type serviceLog struct {
|
|
|
|
Client *debugClient
|
2019-11-27 20:32:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
// Read reads log entries from the logger
|
|
|
|
func (s *serviceLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
|
|
|
|
var options log.ReadOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2019-11-27 20:32:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
stream, err := s.Client.Log(options.Since, options.Count, false)
|
2019-11-27 20:32:54 +00:00
|
|
|
if err != nil {
|
2019-12-17 17:08:38 +00:00
|
|
|
return nil, err
|
2019-11-27 20:32:54 +00:00
|
|
|
}
|
2019-12-17 17:08:38 +00:00
|
|
|
defer stream.Stop()
|
|
|
|
|
|
|
|
// stream the records until nothing is left
|
|
|
|
var records []log.Record
|
2019-11-27 20:32:54 +00:00
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
for record := range stream.Chan() {
|
|
|
|
records = append(records, record)
|
2019-12-17 16:56:55 +00:00
|
|
|
}
|
2019-12-17 15:38:03 +00:00
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
return records, nil
|
|
|
|
}
|
2019-11-27 20:32:54 +00:00
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
// There is no write support
|
|
|
|
func (s *serviceLog) Write(r log.Record) error {
|
|
|
|
return nil
|
2019-11-27 20:32:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
// Stream log records
|
|
|
|
func (s *serviceLog) Stream() (log.Stream, error) {
|
|
|
|
return s.Client.Log(time.Time{}, 0, true)
|
|
|
|
}
|
2019-11-27 20:32:54 +00:00
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
// NewLog returns a new log interface
|
|
|
|
func NewLog(opts ...log.Option) log.Log {
|
|
|
|
var options log.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2019-11-27 20:32:54 +00:00
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
name := options.Name
|
2019-11-27 20:32:54 +00:00
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
// set the default name
|
|
|
|
if len(name) == 0 {
|
|
|
|
name = debug.DefaultName
|
|
|
|
}
|
2019-11-27 20:32:54 +00:00
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
return &serviceLog{
|
|
|
|
Client: NewClient(name),
|
2019-11-27 20:32:54 +00:00
|
|
|
}
|
|
|
|
}
|