micro/debug/service/service.go

65 lines
1.1 KiB
Go
Raw Normal View History

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