1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/debug/log/log.go

59 lines
1.2 KiB
Go
Raw Normal View History

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