1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00
go-micro/debug/service/service.go

65 lines
1.1 KiB
Go
Raw Normal View History

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