2019-12-17 18:38:03 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2019-12-17 19:56:55 +03:00
|
|
|
"time"
|
|
|
|
|
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
|
2019-12-17 19:56:55 +03:00
|
|
|
func (s *serviceLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
|
|
|
|
var options log.ReadOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
stream, err := s.Client.Log(options.Since, options.Count, false)
|
2019-12-17 18:38:03 +03:00
|
|
|
if err != nil {
|
2019-12-17 19:56:55 +03:00
|
|
|
return nil, err
|
2019-12-17 18:38:03 +03:00
|
|
|
}
|
2019-12-17 19:56:55 +03:00
|
|
|
defer stream.Stop()
|
|
|
|
|
2019-12-17 18:38:03 +03:00
|
|
|
// stream the records until nothing is left
|
|
|
|
var records []log.Record
|
2019-12-17 19:56:55 +03:00
|
|
|
|
|
|
|
for record := range stream.Chan() {
|
2019-12-17 18:38:03 +03:00
|
|
|
records = append(records, record)
|
|
|
|
}
|
2019-12-17 19:56:55 +03:00
|
|
|
|
|
|
|
return records, nil
|
2019-12-17 18:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// There is no write support
|
2019-12-17 19:56:55 +03:00
|
|
|
func (s *serviceLog) Write(r log.Record) error {
|
|
|
|
return nil
|
2019-12-17 18:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stream log records
|
2019-12-17 19:56:55 +03:00
|
|
|
func (s *serviceLog) Stream() (log.Stream, error) {
|
|
|
|
return s.Client.Log(time.Time{}, 0, true)
|
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
|
|
|
}
|
|
|
|
}
|