move to structured logging
This commit is contained in:
@@ -24,7 +24,7 @@ func TestKubernetes(t *testing.T) {
|
||||
meta := make(map[string]string)
|
||||
write := log.Record{
|
||||
Timestamp: time.Unix(0, 0).UTC(),
|
||||
Value: "Test log entry",
|
||||
Message: "Test log entry",
|
||||
Metadata: meta,
|
||||
}
|
||||
meta["foo"] = "bar"
|
||||
|
@@ -25,11 +25,11 @@ type Log interface {
|
||||
// Record is log record entry
|
||||
type Record struct {
|
||||
// Timestamp of logged event
|
||||
Timestamp time.Time `json:"time"`
|
||||
// Value contains log entry
|
||||
Value interface{} `json:"value"`
|
||||
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
|
||||
|
@@ -35,7 +35,7 @@ func NewLog(opts ...log.Option) log.Log {
|
||||
|
||||
// Write writes logs into logger
|
||||
func (l *memoryLog) Write(r log.Record) error {
|
||||
l.Buffer.Put(fmt.Sprint(r.Value))
|
||||
l.Buffer.Put(fmt.Sprint(r.Message))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ func (l *memoryLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
|
||||
for _, entry := range entries {
|
||||
record := log.Record{
|
||||
Timestamp: entry.Timestamp,
|
||||
Value: entry.Value,
|
||||
Message: entry.Value,
|
||||
}
|
||||
records = append(records, record)
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func (l *memoryLog) Stream() (log.Stream, error) {
|
||||
for _, entry := range last10 {
|
||||
records <- log.Record{
|
||||
Timestamp: entry.Timestamp,
|
||||
Value: entry.Value,
|
||||
Message: entry.Value,
|
||||
Metadata: make(map[string]string),
|
||||
}
|
||||
}
|
||||
@@ -106,7 +106,7 @@ func (l *memoryLog) Stream() (log.Stream, error) {
|
||||
for entry := range stream {
|
||||
records <- log.Record{
|
||||
Timestamp: entry.Timestamp,
|
||||
Value: entry.Value,
|
||||
Message: entry.Value,
|
||||
Metadata: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
@@ -18,15 +18,15 @@ func TestLogger(t *testing.T) {
|
||||
}
|
||||
|
||||
// Log some cruft
|
||||
lg.Write(log.Record{Value: "foobar"})
|
||||
lg.Write(log.Record{Value: "foo bar"})
|
||||
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.Value, expected[i]) {
|
||||
t.Errorf("expected %s, got %s", expected[i], entry.Value)
|
||||
if !reflect.DeepEqual(entry.Message, expected[i]) {
|
||||
t.Errorf("expected %s, got %s", expected[i], entry.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -87,7 +87,7 @@ func (o *osLog) run() {
|
||||
} else {
|
||||
r = Record{
|
||||
Timestamp: time.Now(),
|
||||
Value: strings.TrimSuffix(line, "\n"),
|
||||
Message: strings.TrimSuffix(line, "\n"),
|
||||
Metadata: make(map[string]string),
|
||||
}
|
||||
}
|
||||
@@ -125,7 +125,7 @@ func (o *osLog) Read(...ReadOption) ([]Record, error) {
|
||||
// Write writes records to log
|
||||
func (o *osLog) Write(r Record) error {
|
||||
b, _ := json.Marshal(r)
|
||||
_, err := os.Stderr.Write(b)
|
||||
_, err := os.Stderr.Write(append(b, byte('\n')))
|
||||
return err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user