Move logger
This commit is contained in:
parent
812fe9e640
commit
c2d59c1f4d
@ -10,6 +10,8 @@ var (
|
|||||||
DefaultLog = NewLog()
|
DefaultLog = NewLog()
|
||||||
// DefaultLevel is default log level
|
// DefaultLevel is default log level
|
||||||
DefaultLevel = LevelInfo
|
DefaultLevel = LevelInfo
|
||||||
|
// Default buffer size if any
|
||||||
|
DefaultSize = 1024
|
||||||
// prefix for all messages
|
// prefix for all messages
|
||||||
prefix string
|
prefix string
|
||||||
)
|
)
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package log
|
// Package memory provides an in memory log buffer
|
||||||
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
golog "log"
|
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/debug/log"
|
||||||
"github.com/micro/go-micro/util/ring"
|
"github.com/micro/go-micro/util/ring"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,36 +13,35 @@ var (
|
|||||||
DefaultSize = 1024
|
DefaultSize = 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
// defaultLog is default micro log
|
// memoryLog is default micro log
|
||||||
type defaultLog struct {
|
type memoryLog struct {
|
||||||
*ring.Buffer
|
*ring.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLog returns default Logger with
|
// NewLog returns default Logger with
|
||||||
func NewLog(opts ...Option) Log {
|
func NewLog(opts ...log.Option) log.Log {
|
||||||
// get default options
|
// get default options
|
||||||
options := DefaultOptions()
|
options := log.DefaultOptions()
|
||||||
|
|
||||||
// apply requested options
|
// apply requested options
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &defaultLog{
|
return &memoryLog{
|
||||||
Buffer: ring.New(options.Size),
|
Buffer: ring.New(options.Size),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes logs into logger
|
// Write writes logs into logger
|
||||||
func (l *defaultLog) Write(r Record) error {
|
func (l *memoryLog) Write(r log.Record) error {
|
||||||
golog.Print(r.Value)
|
|
||||||
l.Buffer.Put(fmt.Sprint(r.Value))
|
l.Buffer.Put(fmt.Sprint(r.Value))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read reads logs and returns them
|
// Read reads logs and returns them
|
||||||
func (l *defaultLog) Read(opts ...ReadOption) ([]Record, error) {
|
func (l *memoryLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
|
||||||
options := ReadOptions{}
|
options := log.ReadOptions{}
|
||||||
// initialize the read options
|
// initialize the read options
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
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 {
|
for _, entry := range entries {
|
||||||
record := Record{
|
record := log.Record{
|
||||||
Timestamp: entry.Timestamp,
|
Timestamp: entry.Timestamp,
|
||||||
Value: entry.Value,
|
Value: entry.Value,
|
||||||
}
|
}
|
||||||
@ -84,11 +84,11 @@ func (l *defaultLog) Read(opts ...ReadOption) ([]Record, error) {
|
|||||||
|
|
||||||
// Stream returns channel for reading log records
|
// Stream returns channel for reading log records
|
||||||
// along with a stop channel, close it when done
|
// 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
|
// get stream channel from ring buffer
|
||||||
stream, stop := l.Buffer.Stream()
|
stream, stop := l.Buffer.Stream()
|
||||||
// make a buffered channel
|
// make a buffered channel
|
||||||
records := make(chan Record, 128)
|
records := make(chan log.Record, 128)
|
||||||
// get last 10 records
|
// get last 10 records
|
||||||
last10 := l.Buffer.Get(10)
|
last10 := l.Buffer.Get(10)
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ func (l *defaultLog) Stream() (Stream, error) {
|
|||||||
go func() {
|
go func() {
|
||||||
// first send last 10 records
|
// first send last 10 records
|
||||||
for _, entry := range last10 {
|
for _, entry := range last10 {
|
||||||
records <- Record{
|
records <- log.Record{
|
||||||
Timestamp: entry.Timestamp,
|
Timestamp: entry.Timestamp,
|
||||||
Value: entry.Value,
|
Value: entry.Value,
|
||||||
Metadata: make(map[string]string),
|
Metadata: make(map[string]string),
|
||||||
@ -104,7 +104,7 @@ func (l *defaultLog) Stream() (Stream, error) {
|
|||||||
}
|
}
|
||||||
// now stream continuously
|
// now stream continuously
|
||||||
for entry := range stream {
|
for entry := range stream {
|
||||||
records <- Record{
|
records <- log.Record{
|
||||||
Timestamp: entry.Timestamp,
|
Timestamp: entry.Timestamp,
|
||||||
Value: entry.Value,
|
Value: entry.Value,
|
||||||
Metadata: make(map[string]string),
|
Metadata: make(map[string]string),
|
@ -1,29 +1,29 @@
|
|||||||
package log
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/debug/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogger(t *testing.T) {
|
func TestLogger(t *testing.T) {
|
||||||
// set size to some value
|
// set size to some value
|
||||||
size := 100
|
size := 100
|
||||||
// override the global logger
|
// 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
|
// make sure we have the right size of the logger ring buffer
|
||||||
if DefaultLog.(*defaultLog).Size() != size {
|
if lg.(*memoryLog).Size() != size {
|
||||||
t.Errorf("expected buffer size: %d, got: %d", size, DefaultLog.(*defaultLog).Size())
|
t.Errorf("expected buffer size: %d, got: %d", size, lg.(*memoryLog).Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log some cruft
|
// Log some cruft
|
||||||
Info("foobar")
|
lg.Write(log.Record{Value: "foobar"})
|
||||||
// increase the log level
|
lg.Write(log.Record{Value: "foo bar"})
|
||||||
DefaultLevel = LevelDebug
|
|
||||||
Debugf("foo %s", "bar")
|
|
||||||
|
|
||||||
// Check if the logs are stored in the logger ring buffer
|
// Check if the logs are stored in the logger ring buffer
|
||||||
expected := []string{"foobar", "foo bar"}
|
expected := []string{"foobar", "foo bar"}
|
||||||
entries, _ := DefaultLog.Read(Count(len(expected)))
|
entries, _ := lg.Read(log.Count(len(expected)))
|
||||||
for i, entry := range entries {
|
for i, entry := range entries {
|
||||||
if !reflect.DeepEqual(entry.Value, expected[i]) {
|
if !reflect.DeepEqual(entry.Value, expected[i]) {
|
||||||
t.Errorf("expected %s, got %s", expected[i], entry.Value)
|
t.Errorf("expected %s, got %s", expected[i], entry.Value)
|
@ -1,11 +1,15 @@
|
|||||||
package log
|
package memory
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/micro/go-micro/debug/log"
|
||||||
|
)
|
||||||
|
|
||||||
type logStream struct {
|
type logStream struct {
|
||||||
stream <-chan Record
|
stream <-chan log.Record
|
||||||
stop chan bool
|
stop chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logStream) Chan() <-chan Record {
|
func (l *logStream) Chan() <-chan log.Record {
|
||||||
return l.stream
|
return l.stream
|
||||||
}
|
}
|
||||||
|
|
97
debug/log/os.go
Normal file
97
debug/log/os.go
Normal 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{}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user