Small refactoring og logs

* log.Write now accepts log.Record
* we stream last 10 records first
* regenerate proto because of the above
This commit is contained in:
Milos Gajdos
2019-12-01 13:15:10 +00:00
parent ecdadef633
commit 4613a820ca
7 changed files with 98 additions and 88 deletions

View File

@@ -33,9 +33,9 @@ func NewLog(opts ...Option) Log {
}
// Write writes logs into logger
func (l *defaultLog) Write(v ...interface{}) {
golog.Print(v...)
l.Buffer.Put(fmt.Sprint(v...))
func (l *defaultLog) Write(r Record) {
golog.Print(r.Value)
l.Buffer.Put(fmt.Sprint(r.Value))
}
// Read reads logs and returns them
@@ -85,12 +85,22 @@ func (l *defaultLog) Read(opts ...ReadOption) []Record {
func (l *defaultLog) Stream(stop chan bool) <-chan Record {
// get stream channel from ring buffer
stream := l.Buffer.Stream(stop)
records := make(chan Record)
fmt.Println("requested log stream")
// make a buffered channel
records := make(chan 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 <- Record{
Timestamp: entry.Timestamp,
Value: entry.Value,
Metadata: make(map[string]string),
}
}
// now stream continuously
for entry := range stream {
records <- Record{
Timestamp: entry.Timestamp,

View File

@@ -20,9 +20,9 @@ var (
type Log interface {
// Read reads log entries from the logger
Read(...ReadOption) []Record
// Write writes logs to logger
Write(...interface{})
// Stream logs
// Write writes records to log
Write(Record)
// Stream log records
Stream(chan bool) <-chan Record
}
@@ -67,17 +67,17 @@ func init() {
func log(v ...interface{}) {
if len(prefix) > 0 {
DefaultLog.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...))
DefaultLog.Write(Record{Value: fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)})
return
}
DefaultLog.Write(fmt.Sprint(v...))
DefaultLog.Write(Record{Value: fmt.Sprint(v...)})
}
func logf(format string, v ...interface{}) {
if len(prefix) > 0 {
format = prefix + " " + format
}
DefaultLog.Write(fmt.Sprintf(format, v...))
DefaultLog.Write(Record{Value: fmt.Sprintf(format, v...)})
}
// WithLevel logs with the level specified