chore: import upstream snapshot with attribution
govulncheck / govulncheck (push) Waiting to run
Harness (E2E) / Harnesses (mock LLM) (push) Waiting to run
Harness (E2E) / Provider harnesses (live LLM conformance) (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Run Tests / Unit Tests (push) Waiting to run
Run Tests / Etcd Integration Tests (push) Waiting to run
govulncheck / govulncheck (push) Waiting to run
Harness (E2E) / Harnesses (mock LLM) (push) Waiting to run
Harness (E2E) / Provider harnesses (live LLM conformance) (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Run Tests / Unit Tests (push) Waiting to run
Run Tests / Etcd Integration Tests (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// Package log provides debug logging
|
||||
package log
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
// Default buffer size if any.
|
||||
DefaultSize = 1024
|
||||
// DefaultLog logger.
|
||||
DefaultLog = NewLog()
|
||||
// Default formatter.
|
||||
DefaultFormat = TextFormat
|
||||
)
|
||||
|
||||
// Log is debug log interface for reading and writing logs.
|
||||
type Log interface {
|
||||
// Read reads log entries from the logger
|
||||
Read(...ReadOption) ([]Record, error)
|
||||
// Write writes records to log
|
||||
Write(Record) error
|
||||
// Stream log records
|
||||
Stream() (Stream, error)
|
||||
}
|
||||
|
||||
// Record is log record entry.
|
||||
type Record struct {
|
||||
// Timestamp of logged event
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
// Metadata to enrich log record
|
||||
Metadata map[string]string `json:"metadata"`
|
||||
// Value contains log entry
|
||||
Message interface{} `json:"message"`
|
||||
}
|
||||
|
||||
// Stream returns a log stream.
|
||||
type Stream interface {
|
||||
Chan() <-chan Record
|
||||
Stop() error
|
||||
}
|
||||
|
||||
// Format is a function which formats the output.
|
||||
type FormatFunc func(Record) string
|
||||
|
||||
// TextFormat returns text format.
|
||||
func TextFormat(r Record) string {
|
||||
t := r.Timestamp.Format("2006-01-02 15:04:05")
|
||||
return fmt.Sprintf("%s %v", t, r.Message)
|
||||
}
|
||||
|
||||
// JSONFormat is a json Format func.
|
||||
func JSONFormat(r Record) string {
|
||||
b, _ := json.Marshal(r)
|
||||
return string(b)
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
// Package memory provides an in memory log buffer
|
||||
package memory
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go-micro.dev/v6/debug/log"
|
||||
"go-micro.dev/v6/internal/util/ring"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultSize of the logger buffer.
|
||||
DefaultSize = 1024
|
||||
)
|
||||
|
||||
// memoryLog is default micro log.
|
||||
type memoryLog struct {
|
||||
*ring.Buffer
|
||||
}
|
||||
|
||||
// NewLog returns default Logger with.
|
||||
func NewLog(opts ...log.Option) log.Log {
|
||||
// get default options
|
||||
options := log.DefaultOptions()
|
||||
|
||||
// apply requested options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
return &memoryLog{
|
||||
Buffer: ring.New(options.Size),
|
||||
}
|
||||
}
|
||||
|
||||
// Write writes logs into logger.
|
||||
func (l *memoryLog) Write(r log.Record) error {
|
||||
l.Put(fmt.Sprint(r.Message))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read reads logs and returns them.
|
||||
func (l *memoryLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
|
||||
options := log.ReadOptions{}
|
||||
// initialize the read options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
var entries []*ring.Entry
|
||||
// if Since options ha sbeen specified we honor it
|
||||
if !options.Since.IsZero() {
|
||||
entries = l.Since(options.Since)
|
||||
}
|
||||
|
||||
// only if we specified valid count constraint
|
||||
// do we end up doing some serious if-else kung-fu
|
||||
// if since constraint has been provided
|
||||
// we return *count* number of logs since the given timestamp;
|
||||
// otherwise we return last count number of logs
|
||||
if options.Count > 0 {
|
||||
switch len(entries) > 0 {
|
||||
case true:
|
||||
// if we request fewer logs than what since constraint gives us
|
||||
if options.Count < len(entries) {
|
||||
entries = entries[0:options.Count]
|
||||
}
|
||||
default:
|
||||
entries = l.Get(options.Count)
|
||||
}
|
||||
}
|
||||
|
||||
records := make([]log.Record, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
record := log.Record{
|
||||
Timestamp: entry.Timestamp,
|
||||
Message: entry.Value,
|
||||
}
|
||||
records = append(records, record)
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// Stream returns channel for reading log records
|
||||
// along with a stop channel, close it when done.
|
||||
func (l *memoryLog) Stream() (log.Stream, error) {
|
||||
// get stream channel from ring buffer
|
||||
stream, stop := l.Buffer.Stream()
|
||||
// make a buffered channel
|
||||
records := make(chan log.Record, 128)
|
||||
// get last 10 records
|
||||
last10 := l.Get(10)
|
||||
|
||||
// stream the log records
|
||||
go func() {
|
||||
// first send last 10 records
|
||||
for _, entry := range last10 {
|
||||
records <- log.Record{
|
||||
Timestamp: entry.Timestamp,
|
||||
Message: entry.Value,
|
||||
Metadata: make(map[string]string),
|
||||
}
|
||||
}
|
||||
// now stream continuously
|
||||
for entry := range stream {
|
||||
records <- log.Record{
|
||||
Timestamp: entry.Timestamp,
|
||||
Message: entry.Value,
|
||||
Metadata: make(map[string]string),
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return &logStream{
|
||||
stream: records,
|
||||
stop: stop,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v6/debug/log"
|
||||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
// set size to some value
|
||||
size := 100
|
||||
// override the global logger
|
||||
lg := NewLog(log.Size(size))
|
||||
// make sure we have the right size of the logger ring buffer
|
||||
if lg.(*memoryLog).Size() != size {
|
||||
t.Errorf("expected buffer size: %d, got: %d", size, lg.(*memoryLog).Size())
|
||||
}
|
||||
|
||||
// Log some cruft
|
||||
lg.Write(log.Record{Message: "foobar"})
|
||||
lg.Write(log.Record{Message: "foo bar"})
|
||||
|
||||
// Check if the logs are stored in the logger ring buffer
|
||||
expected := []string{"foobar", "foo bar"}
|
||||
entries, _ := lg.Read(log.Count(len(expected)))
|
||||
for i, entry := range entries {
|
||||
if !reflect.DeepEqual(entry.Message, expected[i]) {
|
||||
t.Errorf("expected %s, got %s", expected[i], entry.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"go-micro.dev/v6/debug/log"
|
||||
)
|
||||
|
||||
type logStream struct {
|
||||
stream <-chan log.Record
|
||||
stop chan bool
|
||||
}
|
||||
|
||||
func (l *logStream) Chan() <-chan log.Record {
|
||||
return l.stream
|
||||
}
|
||||
|
||||
func (l *logStream) Stop() error {
|
||||
select {
|
||||
case <-l.stop:
|
||||
return nil
|
||||
default:
|
||||
close(l.stop)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package noop
|
||||
|
||||
import (
|
||||
"go-micro.dev/v6/debug/log"
|
||||
)
|
||||
|
||||
type noop struct{}
|
||||
|
||||
func (n *noop) Read(...log.ReadOption) ([]log.Record, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (n *noop) Write(log.Record) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noop) Stream() (log.Stream, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func NewLog(opts ...log.Option) log.Log {
|
||||
return new(noop)
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package log
|
||||
|
||||
import "time"
|
||||
|
||||
// Option used by the logger.
|
||||
type Option func(*Options)
|
||||
|
||||
// Options are logger options.
|
||||
type Options struct {
|
||||
// Format specifies the output format
|
||||
Format FormatFunc
|
||||
// Name of the log
|
||||
Name string
|
||||
// Size is the size of ring buffer
|
||||
Size int
|
||||
}
|
||||
|
||||
// Name of the log.
|
||||
func Name(n string) Option {
|
||||
return func(o *Options) {
|
||||
o.Name = n
|
||||
}
|
||||
}
|
||||
|
||||
// Size sets the size of the ring buffer.
|
||||
func Size(s int) Option {
|
||||
return func(o *Options) {
|
||||
o.Size = s
|
||||
}
|
||||
}
|
||||
|
||||
func Format(f FormatFunc) Option {
|
||||
return func(o *Options) {
|
||||
o.Format = f
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultOptions returns default options.
|
||||
func DefaultOptions() Options {
|
||||
return Options{
|
||||
Size: DefaultSize,
|
||||
}
|
||||
}
|
||||
|
||||
// ReadOptions for querying the logs.
|
||||
type ReadOptions struct {
|
||||
// Since what time in past to return the logs
|
||||
Since time.Time
|
||||
// Count specifies number of logs to return
|
||||
Count int
|
||||
// Stream requests continuous log stream
|
||||
Stream bool
|
||||
}
|
||||
|
||||
// ReadOption used for reading the logs.
|
||||
type ReadOption func(*ReadOptions)
|
||||
|
||||
// Since sets the time since which to return the log records.
|
||||
func Since(s time.Time) ReadOption {
|
||||
return func(o *ReadOptions) {
|
||||
o.Since = s
|
||||
}
|
||||
}
|
||||
|
||||
// Count sets the number of log records to return.
|
||||
func Count(c int) ReadOption {
|
||||
return func(o *ReadOptions) {
|
||||
o.Count = c
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user