github.com/wiselike/revel-cmd@v1.2.1/logger/logger.go (about)

     1  package logger
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/revel/config"
     8  )
     9  
    10  // The LogHandler defines the interface to handle the log records.
    11  type (
    12  	// The Multilogger reduces the number of exposed defined logging variables,
    13  	// and allows the output to be easily refined.
    14  	MultiLogger interface {
    15  		// New returns a new Logger that has this logger's context plus the given context
    16  		New(ctx ...interface{}) MultiLogger
    17  
    18  		// SetHandler updates the logger to write records to the specified handler.
    19  		SetHandler(h LogHandler)
    20  
    21  		// Set the stack depth for the logger
    22  		SetStackDepth(int) MultiLogger
    23  
    24  		// Log a message at the given level with context key/value pairs
    25  		Debug(msg string, ctx ...interface{})
    26  
    27  		// Log a message at the given level formatting message with the parameters
    28  		Debugf(msg string, params ...interface{})
    29  
    30  		// Log a message at the given level with context key/value pairs
    31  		Info(msg string, ctx ...interface{})
    32  
    33  		// Log a message at the given level formatting message with the parameters
    34  		Infof(msg string, params ...interface{})
    35  
    36  		// Log a message at the given level with context key/value pairs
    37  		Warn(msg string, ctx ...interface{})
    38  
    39  		// Log a message at the given level formatting message with the parameters
    40  		Warnf(msg string, params ...interface{})
    41  
    42  		// Log a message at the given level with context key/value pairs
    43  		Error(msg string, ctx ...interface{})
    44  
    45  		// Log a message at the given level formatting message with the parameters
    46  		Errorf(msg string, params ...interface{})
    47  
    48  		// Log a message at the given level with context key/value pairs
    49  		Crit(msg string, ctx ...interface{})
    50  
    51  		// Log a message at the given level formatting message with the parameters
    52  		Critf(msg string, params ...interface{})
    53  
    54  		// Log a message at the given level with context key/value pairs and exits
    55  		Fatal(msg string, ctx ...interface{})
    56  
    57  		// Log a message at the given level formatting message with the parameters and exits
    58  		Fatalf(msg string, params ...interface{})
    59  
    60  		// Log a message at the given level with context key/value pairs and panics
    61  		Panic(msg string, ctx ...interface{})
    62  
    63  		// Log a message at the given level formatting message with the parameters and panics
    64  		Panicf(msg string, params ...interface{})
    65  	}
    66  
    67  	// The log handler interface.
    68  	LogHandler interface {
    69  		Log(*Record) error
    70  		// log15.Handler
    71  	}
    72  
    73  	// The log stack handler interface.
    74  	LogStackHandler interface {
    75  		LogHandler
    76  		GetStack() int
    77  	}
    78  
    79  	// The log handler interface which has child logs.
    80  	ParentLogHandler interface {
    81  		SetChild(handler LogHandler) LogHandler
    82  	}
    83  
    84  	// The log format interface.
    85  	LogFormat interface {
    86  		Format(r *Record) []byte
    87  	}
    88  
    89  	// The log level type.
    90  	LogLevel int
    91  
    92  	// Used for the callback to LogFunctionMap.
    93  	LogOptions struct {
    94  		Ctx                    *config.Context
    95  		ReplaceExistingHandler bool
    96  		HandlerWrap            ParentLogHandler
    97  		Levels                 []LogLevel
    98  		ExtendedOptions        map[string]interface{}
    99  	}
   100  
   101  	// The log record.
   102  	Record struct {
   103  		Message string     // The message
   104  		Time    time.Time  // The time
   105  		Level   LogLevel   // The level
   106  		Call    CallStack  // The call stack if built
   107  		Context ContextMap // The context
   108  	}
   109  
   110  	// The lazy structure to implement a function to be invoked only if needed.
   111  	Lazy struct {
   112  		Fn interface{} // the function
   113  	}
   114  
   115  	// Currently the only requirement for the callstack is to support the Formatter method
   116  	// which stack.Call does so we use that.
   117  	CallStack interface {
   118  		fmt.Formatter // Requirement
   119  	}
   120  )
   121  
   122  // FormatFunc returns a new Format object which uses
   123  // the given function to perform record formatting.
   124  func FormatFunc(f func(*Record) []byte) LogFormat {
   125  	return formatFunc(f)
   126  }
   127  
   128  type formatFunc func(*Record) []byte
   129  
   130  func (f formatFunc) Format(r *Record) []byte {
   131  	return f(r)
   132  }
   133  
   134  func NewRecord(message string, level LogLevel) *Record {
   135  	return &Record{Message: message, Context: ContextMap{}, Level: level}
   136  }
   137  
   138  const (
   139  	LvlCrit  LogLevel = iota // Critical
   140  	LvlError                 // Error
   141  	LvlWarn                  // Warning
   142  	LvlInfo                  // Information
   143  	LvlDebug                 // Debug
   144  )
   145  
   146  // LvlAllList is a list of all the log levels.
   147  var LvlAllList = []LogLevel{LvlDebug, LvlInfo, LvlWarn, LvlError, LvlCrit}
   148  
   149  // Implements the ParentLogHandler.
   150  type parentLogHandler struct {
   151  	setChild func(handler LogHandler) LogHandler
   152  }
   153  
   154  // Create a new parent log handler.
   155  func NewParentLogHandler(callBack func(child LogHandler) LogHandler) ParentLogHandler {
   156  	return &parentLogHandler{callBack}
   157  }
   158  
   159  // Sets the child of the log handler.
   160  func (p *parentLogHandler) SetChild(child LogHandler) LogHandler {
   161  	return p.setChild(child)
   162  }
   163  
   164  // Create a new log options.
   165  func NewLogOptions(cfg *config.Context, replaceHandler bool, phandler ParentLogHandler, lvl ...LogLevel) (logOptions *LogOptions) {
   166  	logOptions = &LogOptions{
   167  		Ctx:                    cfg,
   168  		ReplaceExistingHandler: replaceHandler,
   169  		HandlerWrap:            phandler,
   170  		Levels:                 lvl,
   171  		ExtendedOptions:        map[string]interface{}{},
   172  	}
   173  	return
   174  }
   175  
   176  // Assumes options will be an even number and have a string, value syntax.
   177  func (l *LogOptions) SetExtendedOptions(options ...interface{}) {
   178  	for x := 0; x < len(options); x += 2 {
   179  		l.ExtendedOptions[options[x].(string)] = options[x+1]
   180  	}
   181  }
   182  
   183  // Gets a string option with default.
   184  func (l *LogOptions) GetStringDefault(option, value string) string {
   185  	if v, found := l.ExtendedOptions[option]; found {
   186  		return v.(string)
   187  	}
   188  	return value
   189  }
   190  
   191  // Gets an int option with default.
   192  func (l *LogOptions) GetIntDefault(option string, value int) int {
   193  	if v, found := l.ExtendedOptions[option]; found {
   194  		return v.(int)
   195  	}
   196  	return value
   197  }
   198  
   199  // Gets a boolean option with default.
   200  func (l *LogOptions) GetBoolDefault(option string, value bool) bool {
   201  	if v, found := l.ExtendedOptions[option]; found {
   202  		return v.(bool)
   203  	}
   204  	return value
   205  }