micro/debug/log/log.go

57 lines
1.1 KiB
Go
Raw Normal View History

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