github.com/annwntech/go-micro/v2@v2.9.5/debug/service/service.go (about)

     1  package service
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/annwntech/go-micro/v2/debug"
     7  	"github.com/annwntech/go-micro/v2/debug/log"
     8  )
     9  
    10  type serviceLog struct {
    11  	Client *debugClient
    12  }
    13  
    14  // Read reads log entries from the logger
    15  func (s *serviceLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
    16  	var options log.ReadOptions
    17  	for _, o := range opts {
    18  		o(&options)
    19  	}
    20  
    21  	stream, err := s.Client.Log(options.Since, options.Count, false)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	defer stream.Stop()
    26  
    27  	// stream the records until nothing is left
    28  	var records []log.Record
    29  
    30  	for record := range stream.Chan() {
    31  		records = append(records, record)
    32  	}
    33  
    34  	return records, nil
    35  }
    36  
    37  // There is no write support
    38  func (s *serviceLog) Write(r log.Record) error {
    39  	return nil
    40  }
    41  
    42  // Stream log records
    43  func (s *serviceLog) Stream() (log.Stream, error) {
    44  	return s.Client.Log(time.Time{}, 0, true)
    45  }
    46  
    47  // NewLog returns a new log interface
    48  func NewLog(opts ...log.Option) log.Log {
    49  	var options log.Options
    50  	for _, o := range opts {
    51  		o(&options)
    52  	}
    53  
    54  	name := options.Name
    55  
    56  	// set the default name
    57  	if len(name) == 0 {
    58  		name = debug.DefaultName
    59  	}
    60  
    61  	return &serviceLog{
    62  		Client: NewClient(name),
    63  	}
    64  }