github.com/annwntech/go-micro/v2@v2.9.5/debug/log/os.go (about) 1 package log 2 3 import ( 4 "sync" 5 6 "github.com/google/uuid" 7 "github.com/annwntech/go-micro/v2/util/ring" 8 ) 9 10 // Should stream from OS 11 type osLog struct { 12 format FormatFunc 13 once sync.Once 14 15 sync.RWMutex 16 buffer *ring.Buffer 17 subs map[string]*osStream 18 } 19 20 type osStream struct { 21 stream chan Record 22 } 23 24 // Read reads log entries from the logger 25 func (o *osLog) Read(...ReadOption) ([]Record, error) { 26 var records []Record 27 28 // read the last 100 records 29 for _, v := range o.buffer.Get(100) { 30 records = append(records, v.Value.(Record)) 31 } 32 33 return records, nil 34 } 35 36 // Write writes records to log 37 func (o *osLog) Write(r Record) error { 38 o.buffer.Put(r) 39 return nil 40 } 41 42 // Stream log records 43 func (o *osLog) Stream() (Stream, error) { 44 o.Lock() 45 defer o.Unlock() 46 47 // create stream 48 st := &osStream{ 49 stream: make(chan Record, 128), 50 } 51 52 // save stream 53 o.subs[uuid.New().String()] = st 54 55 return st, nil 56 } 57 58 func (o *osStream) Chan() <-chan Record { 59 return o.stream 60 } 61 62 func (o *osStream) Stop() error { 63 return nil 64 } 65 66 func NewLog(opts ...Option) Log { 67 options := Options{ 68 Format: DefaultFormat, 69 } 70 for _, o := range opts { 71 o(&options) 72 } 73 74 l := &osLog{ 75 format: options.Format, 76 buffer: ring.New(1024), 77 subs: make(map[string]*osStream), 78 } 79 80 return l 81 }