gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/debug/log/default.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	golog "log"
     6  
     7  	"gitee.com/liuxuezhan/go-micro-v1.18.0/debug/buffer"
     8  )
     9  
    10  var (
    11  	// DefaultSize of the logger buffer
    12  	DefaultSize = 1000
    13  )
    14  
    15  // defaultLog is default micro log
    16  type defaultLog struct {
    17  	*buffer.Buffer
    18  }
    19  
    20  // NewLog returns default Logger with
    21  func NewLog(opts ...Option) Log {
    22  	// get default options
    23  	options := DefaultOptions()
    24  
    25  	// apply requested options
    26  	for _, o := range opts {
    27  		o(&options)
    28  	}
    29  
    30  	return &defaultLog{
    31  		Buffer: buffer.New(options.Size),
    32  	}
    33  }
    34  
    35  // Write writes logs into logger
    36  func (l *defaultLog) Write(r Record) {
    37  	golog.Print(r.Value)
    38  	l.Buffer.Put(fmt.Sprint(r.Value))
    39  }
    40  
    41  // Read reads logs and returns them
    42  func (l *defaultLog) Read(opts ...ReadOption) []Record {
    43  	options := ReadOptions{}
    44  	// initialize the read options
    45  	for _, o := range opts {
    46  		o(&options)
    47  	}
    48  
    49  	var entries []*buffer.Entry
    50  	// if Since options ha sbeen specified we honor it
    51  	if !options.Since.IsZero() {
    52  		entries = l.Buffer.Since(options.Since)
    53  	}
    54  
    55  	// only if we specified valid count constraint
    56  	// do we end up doing some serious if-else kung-fu
    57  	// if since constraint has been provided
    58  	// we return *count* number of logs since the given timestamp;
    59  	// otherwise we return last count number of logs
    60  	if options.Count > 0 {
    61  		switch len(entries) > 0 {
    62  		case true:
    63  			// if we request fewer logs than what since constraint gives us
    64  			if options.Count < len(entries) {
    65  				entries = entries[0:options.Count]
    66  			}
    67  		default:
    68  			entries = l.Buffer.Get(options.Count)
    69  		}
    70  	}
    71  
    72  	records := make([]Record, 0, len(entries))
    73  	for _, entry := range entries {
    74  		record := Record{
    75  			Timestamp: entry.Timestamp,
    76  			Value:     entry.Value,
    77  		}
    78  		records = append(records, record)
    79  	}
    80  
    81  	return records
    82  }
    83  
    84  // Stream returns channel for reading log records
    85  func (l *defaultLog) Stream(stop chan bool) <-chan Record {
    86  	// get stream channel from ring buffer
    87  	stream := l.Buffer.Stream(stop)
    88  	// make a buffered channel
    89  	records := make(chan Record, 128)
    90  	// get last 10 records
    91  	last10 := l.Buffer.Get(10)
    92  
    93  	// stream the log records
    94  	go func() {
    95  		// first send last 10 records
    96  		for _, entry := range last10 {
    97  			records <- Record{
    98  				Timestamp: entry.Timestamp,
    99  				Value:     entry.Value,
   100  				Metadata:  make(map[string]string),
   101  			}
   102  		}
   103  		// now stream continuously
   104  		for entry := range stream {
   105  			records <- Record{
   106  				Timestamp: entry.Timestamp,
   107  				Value:     entry.Value,
   108  				Metadata:  make(map[string]string),
   109  			}
   110  		}
   111  	}()
   112  
   113  	return records
   114  }