2019-11-27 16:02:16 +00:00
|
|
|
// Package log provides debug logging
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2019-12-19 12:29:03 +00:00
|
|
|
"encoding/json"
|
2019-12-19 18:25:22 +00:00
|
|
|
"fmt"
|
2019-11-27 17:31:35 +00:00
|
|
|
"time"
|
2019-11-27 16:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-12-17 18:16:45 +00:00
|
|
|
// Default buffer size if any
|
|
|
|
DefaultSize = 1024
|
2019-12-17 18:34:21 +00:00
|
|
|
// DefaultLog logger
|
|
|
|
DefaultLog = NewLog()
|
2019-12-19 12:20:33 +00:00
|
|
|
// Default formatter
|
2019-12-19 12:29:03 +00:00
|
|
|
DefaultFormat = TextFormat
|
2019-11-27 16:02:16 +00:00
|
|
|
)
|
|
|
|
|
2019-12-17 18:34:21 +00:00
|
|
|
// Log is debug log interface for reading and writing logs
|
2019-11-27 17:31:35 +00:00
|
|
|
type Log interface {
|
|
|
|
// Read reads log entries from the logger
|
2019-12-17 16:56:55 +00:00
|
|
|
Read(...ReadOption) ([]Record, error)
|
2019-12-01 13:15:10 +00:00
|
|
|
// Write writes records to log
|
2019-12-17 16:56:55 +00:00
|
|
|
Write(Record) error
|
2019-12-01 13:15:10 +00:00
|
|
|
// Stream log records
|
2019-12-17 16:56:55 +00:00
|
|
|
Stream() (Stream, error)
|
2019-11-27 16:02:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:31:35 +00:00
|
|
|
// Record is log record entry
|
|
|
|
type Record struct {
|
|
|
|
// Timestamp of logged event
|
2019-12-18 16:02:11 +00:00
|
|
|
Timestamp time.Time `json:"timestamp"`
|
2019-11-27 17:31:35 +00:00
|
|
|
// Metadata to enrich log record
|
2019-12-17 16:09:51 +00:00
|
|
|
Metadata map[string]string `json:"metadata"`
|
2019-12-18 16:02:11 +00:00
|
|
|
// Value contains log entry
|
|
|
|
Message interface{} `json:"message"`
|
2019-11-27 17:31:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 18:34:21 +00:00
|
|
|
// Stream returns a log stream
|
2019-12-17 16:56:55 +00:00
|
|
|
type Stream interface {
|
|
|
|
Chan() <-chan Record
|
|
|
|
Stop() error
|
2019-11-27 16:02:16 +00:00
|
|
|
}
|
2019-12-19 12:20:33 +00:00
|
|
|
|
|
|
|
// Format is a function which formats the output
|
|
|
|
type FormatFunc func(Record) string
|
2019-12-19 12:29:03 +00:00
|
|
|
|
|
|
|
// TextFormat returns text format
|
|
|
|
func TextFormat(r Record) string {
|
2019-12-19 18:25:22 +00:00
|
|
|
t := r.Timestamp.Format("2006-01-02 15:04:05")
|
|
|
|
return fmt.Sprintf("%s %v", t, r.Message)
|
2019-12-19 12:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JSONFormat is a json Format func
|
|
|
|
func JSONFormat(r Record) string {
|
|
|
|
b, _ := json.Marshal(r)
|
|
|
|
return string(b)
|
|
|
|
}
|