github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/spi/spi.go (about)

     1  package spi
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  	"fmt"
     7  )
     8  
     9  // MinLevel exists to minimize the overhead of Trace/Debug logging
    10  var MinLevel = LevelTrace
    11  // MinCallLevel will be half level higher than MinLevel
    12  // to minimize the xxxCall output
    13  var MinCallLevel = LevelDebugCall
    14  
    15  // LevelTraceCall is lowest logging level
    16  // enable this will print every TraceCall, which is a LOT
    17  const LevelTraceCall = 5
    18  
    19  // LevelTrace should be development environment default
    20  const LevelTrace = 10
    21  
    22  const LevelDebugCall = 15
    23  const LevelDebug = 20
    24  const LevelInfoCall = 25
    25  
    26  // LevelInfo should be the production environment default
    27  const LevelInfo = 30
    28  
    29  // LevelWarn is the level for error != nil
    30  const LevelWarn = 40
    31  
    32  // LevelError is the level for user visible error
    33  const LevelError = 50
    34  
    35  // LevelFatal is the level for panic or panic like scenario
    36  const LevelFatal = 60
    37  
    38  func LevelName(level int) string {
    39  	switch level {
    40  	case LevelTraceCall, LevelTrace:
    41  		return "TRACE"
    42  	case LevelDebugCall, LevelDebug:
    43  		return "DEBUG"
    44  	case LevelInfoCall, LevelInfo:
    45  		return "INFO"
    46  	case LevelWarn:
    47  		return "WARN"
    48  	case LevelError:
    49  		return "ERROR"
    50  	case LevelFatal:
    51  		return "FATAL"
    52  	default:
    53  		return "UNKNOWN"
    54  	}
    55  }
    56  
    57  // LogSite is the location of log in the source code
    58  type LogSite struct {
    59  	Context context.Context
    60  	Func    string
    61  	File    string
    62  	Line    int
    63  	Event   string
    64  	Agg     string
    65  	Sample  []interface{}
    66  }
    67  
    68  func (site *LogSite) LogContext() *LogContext {
    69  	ctx := site.Context
    70  	if ctx == nil {
    71  		return nil
    72  	}
    73  	return GetLogContext(ctx)
    74  }
    75  
    76  func (site *LogSite) Location() string {
    77  	return fmt.Sprintf("%s @ %s:%v", site.Func, site.File, site.Line)
    78  }
    79  
    80  type Event struct {
    81  	Level      int
    82  	Context    context.Context
    83  	Error      error
    84  	Timestamp  time.Time
    85  	Properties []interface{}
    86  }
    87  
    88  type EventSink interface {
    89  	HandlerOf(site *LogSite) EventHandler
    90  }
    91  
    92  type EventHandler interface {
    93  	Handle(event *Event)
    94  	LogSite() *LogSite
    95  }
    96  
    97  type EventHandlers []EventHandler
    98  
    99  func (handlers EventHandlers) Handle(event *Event) {
   100  	for _, handler := range handlers {
   101  		handler.Handle(event)
   102  	}
   103  }
   104  
   105  func (handlers EventHandlers) LogSite() *LogSite {
   106  	return handlers[0].LogSite()
   107  }
   108  
   109  type DummyEventHandler struct {
   110  	Site *LogSite
   111  }
   112  
   113  func (handler *DummyEventHandler) Handle(event *Event) {
   114  }
   115  
   116  func (handler *DummyEventHandler) LogSite() *LogSite {
   117  	return handler.Site
   118  }
   119  
   120  type Memo struct {
   121  	Site  *LogSite
   122  	Event *Event
   123  }