2019-11-27 22:32:54 +02:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
"github.com/micro/go-micro/debug"
|
2019-11-27 22:32:54 +02:00
|
|
|
"github.com/micro/go-micro/debug/log"
|
|
|
|
)
|
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
type serviceLog struct {
|
|
|
|
Client *debugClient
|
2019-11-27 22:32:54 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 19:08:38 +02: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 22:32:54 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
stream, err := s.Client.Log(options.Since, options.Count, false)
|
2019-11-27 22:32:54 +02:00
|
|
|
if err != nil {
|
2019-12-17 19:08:38 +02:00
|
|
|
return nil, err
|
2019-11-27 22:32:54 +02:00
|
|
|
}
|
2019-12-17 19:08:38 +02:00
|
|
|
defer stream.Stop()
|
|
|
|
|
|
|
|
// stream the records until nothing is left
|
|
|
|
var records []log.Record
|
2019-11-27 22:32:54 +02:00
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
for record := range stream.Chan() {
|
|
|
|
records = append(records, record)
|
2019-12-17 18:56:55 +02:00
|
|
|
}
|
2019-12-17 17:38:03 +02:00
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
return records, nil
|
|
|
|
}
|
2019-11-27 22:32:54 +02:00
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
// There is no write support
|
|
|
|
func (s *serviceLog) Write(r log.Record) error {
|
|
|
|
return nil
|
2019-11-27 22:32:54 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
// Stream log records
|
|
|
|
func (s *serviceLog) Stream() (log.Stream, error) {
|
|
|
|
return s.Client.Log(time.Time{}, 0, true)
|
|
|
|
}
|
2019-11-27 22:32:54 +02:00
|
|
|
|
2019-12-17 19:08:38 +02: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 22:32:54 +02:00
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
name := options.Name
|
2019-11-27 22:32:54 +02:00
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
// set the default name
|
|
|
|
if len(name) == 0 {
|
|
|
|
name = debug.DefaultName
|
|
|
|
}
|
2019-11-27 22:32:54 +02:00
|
|
|
|
2019-12-17 19:08:38 +02:00
|
|
|
return &serviceLog{
|
|
|
|
Client: NewClient(name),
|
2019-11-27 22:32:54 +02:00
|
|
|
}
|
|
|
|
}
|