Move logger

This commit is contained in:
Asim Aslam 2019-12-17 18:16:45 +00:00
parent 812fe9e640
commit c2d59c1f4d
5 changed files with 132 additions and 29 deletions

View File

@ -10,6 +10,8 @@ var (
DefaultLog = NewLog()
// DefaultLevel is default log level
DefaultLevel = LevelInfo
// Default buffer size if any
DefaultSize = 1024
// prefix for all messages
prefix string
)

View File

@ -1,9 +1,10 @@
package log
// Package memory provides an in memory log buffer
package memory
import (
"fmt"
golog "log"
"github.com/micro/go-micro/debug/log"
"github.com/micro/go-micro/util/ring"
)
@ -12,36 +13,35 @@ var (
DefaultSize = 1024
)
// defaultLog is default micro log
type defaultLog struct {
// memoryLog is default micro log
type memoryLog struct {
*ring.Buffer
}
// NewLog returns default Logger with
func NewLog(opts ...Option) Log {
func NewLog(opts ...log.Option) log.Log {
// get default options
options := DefaultOptions()
options := log.DefaultOptions()
// apply requested options
for _, o := range opts {
o(&options)
}
return &defaultLog{
return &memoryLog{
Buffer: ring.New(options.Size),
}
}
// Write writes logs into logger
func (l *defaultLog) Write(r Record) error {
golog.Print(r.Value)
func (l *memoryLog) Write(r log.Record) error {
l.Buffer.Put(fmt.Sprint(r.Value))
return nil
}
// Read reads logs and returns them
func (l *defaultLog) Read(opts ...ReadOption) ([]Record, error) {
options := ReadOptions{}
func (l *memoryLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
options := log.ReadOptions{}
// initialize the read options
for _, o := range opts {
o(&options)
@ -70,9 +70,9 @@ func (l *defaultLog) Read(opts ...ReadOption) ([]Record, error) {
}
}
records := make([]Record, 0, len(entries))
records := make([]log.Record, 0, len(entries))
for _, entry := range entries {
record := Record{
record := log.Record{
Timestamp: entry.Timestamp,
Value: entry.Value,
}
@ -84,11 +84,11 @@ func (l *defaultLog) Read(opts ...ReadOption) ([]Record, error) {
// Stream returns channel for reading log records
// along with a stop channel, close it when done
func (l *defaultLog) Stream() (Stream, error) {
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 Record, 128)
records := make(chan log.Record, 128)
// get last 10 records
last10 := l.Buffer.Get(10)
@ -96,7 +96,7 @@ func (l *defaultLog) Stream() (Stream, error) {
go func() {
// first send last 10 records
for _, entry := range last10 {
records <- Record{
records <- log.Record{
Timestamp: entry.Timestamp,
Value: entry.Value,
Metadata: make(map[string]string),
@ -104,7 +104,7 @@ func (l *defaultLog) Stream() (Stream, error) {
}
// now stream continuously
for entry := range stream {
records <- Record{
records <- log.Record{
Timestamp: entry.Timestamp,
Value: entry.Value,
Metadata: make(map[string]string),

View File

@ -1,29 +1,29 @@
package log
package memory
import (
"reflect"
"testing"
"github.com/micro/go-micro/debug/log"
)
func TestLogger(t *testing.T) {
// set size to some value
size := 100
// override the global logger
DefaultLog = NewLog(Size(size))
lg := NewLog(log.Size(size))
// make sure we have the right size of the logger ring buffer
if DefaultLog.(*defaultLog).Size() != size {
t.Errorf("expected buffer size: %d, got: %d", size, DefaultLog.(*defaultLog).Size())
if lg.(*memoryLog).Size() != size {
t.Errorf("expected buffer size: %d, got: %d", size, lg.(*memoryLog).Size())
}
// Log some cruft
Info("foobar")
// increase the log level
DefaultLevel = LevelDebug
Debugf("foo %s", "bar")
lg.Write(log.Record{Value: "foobar"})
lg.Write(log.Record{Value: "foo bar"})
// Check if the logs are stored in the logger ring buffer
expected := []string{"foobar", "foo bar"}
entries, _ := DefaultLog.Read(Count(len(expected)))
entries, _ := lg.Read(log.Count(len(expected)))
for i, entry := range entries {
if !reflect.DeepEqual(entry.Value, expected[i]) {
t.Errorf("expected %s, got %s", expected[i], entry.Value)

View File

@ -1,11 +1,15 @@
package log
package memory
import (
"github.com/micro/go-micro/debug/log"
)
type logStream struct {
stream <-chan Record
stream <-chan log.Record
stop chan bool
}
func (l *logStream) Chan() <-chan Record {
func (l *logStream) Chan() <-chan log.Record {
return l.stream
}

97
debug/log/os.go Normal file
View File

@ -0,0 +1,97 @@
package log
import (
"bufio"
"encoding/json"
"os"
"time"
)
// Should stream from OS
type osLog struct{}
type osStream struct {
stream chan Record
scanner *bufio.Reader
stop chan bool
}
// Read reads log entries from the logger
func (o *osLog) Read(...ReadOption) ([]Record, error) {
return []Record{}, nil
}
// Write writes records to log
func (o *osLog) Write(r Record) error {
b, _ := json.Marshal(r)
_, err := os.Stderr.Write(b)
return err
}
// Stream log records
func (o *osLog) Stream() (Stream, error) {
// read from standard error
scanner := bufio.NewReader(os.Stderr)
stream := make(chan Record, 128)
stop := make(chan bool)
go func() {
for {
select {
case <-stop:
return
default:
// read the line
line, err := scanner.ReadString('\n')
if err != nil {
return
}
// check if the line exists
if len(line) == 0 {
continue
}
// parse the record
var r Record
if line[0] == '{' {
json.Unmarshal([]byte(line), &r)
} else {
r = Record{
Timestamp: time.Now(),
Value: line,
Metadata: make(map[string]string),
}
}
// send to stream
select {
case <-stop:
return
case stream <- r:
}
}
}
}()
return &osStream{
stream: stream,
scanner: scanner,
stop: stop,
}, nil
}
func (o *osStream) Chan() <-chan Record {
return o.stream
}
func (o *osStream) Stop() error {
select {
case <-o.stop:
return nil
default:
close(o.stop)
}
return nil
}
func NewLog(opts ...Option) Log {
return &osLog{}
}