github.com/annwntech/go-micro/v2@v2.9.5/debug/log/log.go (about)

     1  // Package log provides debug logging
     2  package log
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  	"time"
     8  )
     9  
    10  var (
    11  	// Default buffer size if any
    12  	DefaultSize = 1024
    13  	// DefaultLog logger
    14  	DefaultLog = NewLog()
    15  	// Default formatter
    16  	DefaultFormat = TextFormat
    17  )
    18  
    19  // Log is debug log interface for reading and writing logs
    20  type Log interface {
    21  	// Read reads log entries from the logger
    22  	Read(...ReadOption) ([]Record, error)
    23  	// Write writes records to log
    24  	Write(Record) error
    25  	// Stream log records
    26  	Stream() (Stream, error)
    27  }
    28  
    29  // Record is log record entry
    30  type Record struct {
    31  	// Timestamp of logged event
    32  	Timestamp time.Time `json:"timestamp"`
    33  	// Metadata to enrich log record
    34  	Metadata map[string]string `json:"metadata"`
    35  	// Value contains log entry
    36  	Message interface{} `json:"message"`
    37  }
    38  
    39  // Stream returns a log stream
    40  type Stream interface {
    41  	Chan() <-chan Record
    42  	Stop() error
    43  }
    44  
    45  // Format is a function which formats the output
    46  type FormatFunc func(Record) string
    47  
    48  // TextFormat returns text format
    49  func TextFormat(r Record) string {
    50  	t := r.Timestamp.Format("2006-01-02 15:04:05")
    51  	return fmt.Sprintf("%s %v", t, r.Message)
    52  }
    53  
    54  // JSONFormat is a json Format func
    55  func JSONFormat(r Record) string {
    56  	b, _ := json.Marshal(r)
    57  	return string(b)
    58  }