micro--go-micro
3e885308a0
Fixes #2988. Brings 'golangci-lint run ./...' to zero issues (was ~373): - errcheck: explicitly ignore fire-and-forget calls with '_ =' (and a small errcheck.exclude-functions list for response writes — json Encoder.Encode, http ResponseWriter.Write, fmt.Fprint*); genuine cases handled. - unused: remove dead code (unexported decls and dead test helpers) and the imports they orphaned. - staticcheck: ST1005 error strings, ST1016 receiver names, S1000/S1017/S1019/ S1023 simplifications, SA4004/SA4006/SA4010 dead code, SA1021 net.IP.Equal, SA6002 (store *[]byte in sync.Pool). - govet: fix a context leak (lostcancel) in internal/util/mdns and move t.Fatal/Fatalf out of goroutines (testinggoroutine) in tests. - ineffassign, unconvert: mechanical fixes. CI: the Lint workflow now runs a blocking full-tree 'golangci-lint run' on pushes and PRs (dropped only-new-issues now that the tree is clean). Verified: go build, go vet, test compilation, and unit tests for the behaviourally-touched packages all pass. Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL Co-authored-by: Claude <noreply@anthropic.com>
81 行
1.2 KiB
Go
81 行
1.2 KiB
Go
package log
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/google/uuid"
|
|
"go-micro.dev/v6/internal/util/ring"
|
|
)
|
|
|
|
// Should stream from OS.
|
|
type osLog struct {
|
|
format FormatFunc
|
|
buffer *ring.Buffer
|
|
subs map[string]*osStream
|
|
|
|
sync.RWMutex
|
|
}
|
|
|
|
type osStream struct {
|
|
stream chan Record
|
|
}
|
|
|
|
// Read reads log entries from the logger.
|
|
func (o *osLog) Read(...ReadOption) ([]Record, error) {
|
|
var records []Record
|
|
|
|
// read the last 100 records
|
|
for _, v := range o.buffer.Get(100) {
|
|
records = append(records, v.Value.(Record))
|
|
}
|
|
|
|
return records, nil
|
|
}
|
|
|
|
// Write writes records to log.
|
|
func (o *osLog) Write(r Record) error {
|
|
o.buffer.Put(r)
|
|
return nil
|
|
}
|
|
|
|
// Stream log records.
|
|
func (o *osLog) Stream() (Stream, error) {
|
|
o.Lock()
|
|
defer o.Unlock()
|
|
|
|
// create stream
|
|
st := &osStream{
|
|
stream: make(chan Record, 128),
|
|
}
|
|
|
|
// save stream
|
|
o.subs[uuid.New().String()] = st
|
|
|
|
return st, nil
|
|
}
|
|
|
|
func (o *osStream) Chan() <-chan Record {
|
|
return o.stream
|
|
}
|
|
|
|
func (o *osStream) Stop() error {
|
|
return nil
|
|
}
|
|
|
|
func NewLog(opts ...Option) Log {
|
|
options := Options{
|
|
Format: DefaultFormat,
|
|
}
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
|
|
l := &osLog{
|
|
format: options.Format,
|
|
buffer: ring.New(1024),
|
|
subs: make(map[string]*osStream),
|
|
}
|
|
|
|
return l
|
|
}
|