debug/log: remove stale files

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-13 20:01:57 +03:00
parent 1f8810599b
commit f182bba6ff
6 changed files with 0 additions and 322 deletions

View File

@ -1,58 +0,0 @@
// Package log provides debug logging
package log
import (
"encoding/json"
"fmt"
"time"
"github.com/unistack-org/micro/v3/metadata"
)
var (
// Default buffer size if any
DefaultSize = 256
// 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 metadata.Metadata `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)
}

View File

@ -1,115 +0,0 @@
// Package memory provides an in memory log buffer
package memory
import (
"fmt"
"github.com/unistack-org/micro/v3/debug/log"
"github.com/unistack-org/micro/v3/metadata"
"github.com/unistack-org/micro/v3/util/ring"
)
// 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.Buffer.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.Buffer.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.Buffer.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.Buffer.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: metadata.New(0),
}
}
// now stream continuously
for entry := range stream {
records <- log.Record{
Timestamp: entry.Timestamp,
Message: entry.Value,
Metadata: metadata.New(0),
}
}
}()
return &logStream{
stream: records,
stop: stop,
}, nil
}

View File

@ -1,32 +0,0 @@
package memory
import (
"reflect"
"testing"
"github.com/unistack-org/micro/v3/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)
}
}
}

View File

@ -1,24 +0,0 @@
package memory
import (
"github.com/unistack-org/micro/v3/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
}

View File

@ -1,23 +0,0 @@
package noop
import (
"github.com/unistack-org/micro/v3/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)
}

View File

@ -1,70 +0,0 @@
package log
import "time"
// Option used by the logger
type Option func(*Options)
// Options are logger options
type Options struct {
// Name of the log
Name string
// Size is the size of ring buffer
Size int
// Format specifies the output format
Format FormatFunc
}
// 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
}
}