From dc9bec2c223a58802aedce1ec2edfcef45f3ef6c Mon Sep 17 00:00:00 2001 From: linwenxiang <991154416@qq.com> Date: Sat, 29 Aug 2020 15:19:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0debug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debug/log/log.go | 56 +++++++++++++++++++++++++++++++++++ debug/log/options.go | 70 ++++++++++++++++++++++++++++++++++++++++++++ logger/default.go | 2 +- 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 debug/log/log.go create mode 100644 debug/log/options.go diff --git a/debug/log/log.go b/debug/log/log.go new file mode 100644 index 00000000..dabdb899 --- /dev/null +++ b/debug/log/log.go @@ -0,0 +1,56 @@ +// Package log provides debug logging +package log + +import ( + "encoding/json" + "fmt" + "time" +) + +var ( + // Default buffer size if any + DefaultSize = 256 + // Default formatter + DefaultFormat = TextFormat +) + +// Log is debug log interface for reading and writing logs +type Log interface { + // Read reads log entries from the logger + Read(...ReadOption) ([]Record, error) + // Write writes records to log + Write(Record) error + // Stream log records + Stream() (Stream, error) +} + +// Record is log record entry +type Record struct { + // Timestamp of logged event + 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 +type Stream interface { + Chan() <-chan Record + Stop() error +} + +// Format is a function which formats the output +type FormatFunc func(Record) string + +// TextFormat returns text format +func TextFormat(r Record) string { + t := r.Timestamp.Format("2006-01-02 15:04:05") + return fmt.Sprintf("%s %v", t, r.Message) +} + +// JSONFormat is a json Format func +func JSONFormat(r Record) string { + b, _ := json.Marshal(r) + return string(b) +} diff --git a/debug/log/options.go b/debug/log/options.go new file mode 100644 index 00000000..63281857 --- /dev/null +++ b/debug/log/options.go @@ -0,0 +1,70 @@ +package log + +import "time" + +// Option used by the logger +type Option func(*Options) + +// Options are logger options +type Options struct { + // Name of the log + Name string + // Size is the size of ring buffer + Size int + // Format specifies the output format + Format FormatFunc +} + +// Name of the log +func Name(n string) Option { + return func(o *Options) { + o.Name = n + } +} + +// Size sets the size of the ring buffer +func Size(s int) Option { + return func(o *Options) { + o.Size = s + } +} + +func Format(f FormatFunc) Option { + return func(o *Options) { + o.Format = f + } +} + +// DefaultOptions returns default options +func DefaultOptions() Options { + return Options{ + Size: DefaultSize, + } +} + +// ReadOptions for querying the logs +type ReadOptions struct { + // Since what time in past to return the logs + Since time.Time + // Count specifies number of logs to return + Count int + // Stream requests continuous log stream + Stream bool +} + +// ReadOption used for reading the logs +type ReadOption func(*ReadOptions) + +// Since sets the time since which to return the log records +func Since(s time.Time) ReadOption { + return func(o *ReadOptions) { + o.Since = s + } +} + +// Count sets the number of log records to return +func Count(c int) ReadOption { + return func(o *ReadOptions) { + o.Count = c + } +} diff --git a/logger/default.go b/logger/default.go index 191e7f71..93e3c5e1 100644 --- a/logger/default.go +++ b/logger/default.go @@ -10,7 +10,7 @@ import ( "sync" "time" - dlog "go-admin/tools/debug/log" + dlog "go-admin/debug/log" ) func init() {