...

Source file src/github.com/sirupsen/logrus/logrus.go

Documentation: github.com/sirupsen/logrus

     1  package logrus
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  )
     8  
     9  // Fields type, used to pass to `WithFields`.
    10  type Fields map[string]interface{}
    11  
    12  // Level type
    13  type Level uint32
    14  
    15  // Convert the Level to a string. E.g. PanicLevel becomes "panic".
    16  func (level Level) String() string {
    17  	if b, err := level.MarshalText(); err == nil {
    18  		return string(b)
    19  	} else {
    20  		return "unknown"
    21  	}
    22  }
    23  
    24  // ParseLevel takes a string level and returns the Logrus log level constant.
    25  func ParseLevel(lvl string) (Level, error) {
    26  	switch strings.ToLower(lvl) {
    27  	case "panic":
    28  		return PanicLevel, nil
    29  	case "fatal":
    30  		return FatalLevel, nil
    31  	case "error":
    32  		return ErrorLevel, nil
    33  	case "warn", "warning":
    34  		return WarnLevel, nil
    35  	case "info":
    36  		return InfoLevel, nil
    37  	case "debug":
    38  		return DebugLevel, nil
    39  	case "trace":
    40  		return TraceLevel, nil
    41  	}
    42  
    43  	var l Level
    44  	return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
    45  }
    46  
    47  // UnmarshalText implements encoding.TextUnmarshaler.
    48  func (level *Level) UnmarshalText(text []byte) error {
    49  	l, err := ParseLevel(string(text))
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	*level = Level(l)
    55  
    56  	return nil
    57  }
    58  
    59  func (level Level) MarshalText() ([]byte, error) {
    60  	switch level {
    61  	case TraceLevel:
    62  		return []byte("trace"), nil
    63  	case DebugLevel:
    64  		return []byte("debug"), nil
    65  	case InfoLevel:
    66  		return []byte("info"), nil
    67  	case WarnLevel:
    68  		return []byte("warning"), nil
    69  	case ErrorLevel:
    70  		return []byte("error"), nil
    71  	case FatalLevel:
    72  		return []byte("fatal"), nil
    73  	case PanicLevel:
    74  		return []byte("panic"), nil
    75  	}
    76  
    77  	return nil, fmt.Errorf("not a valid lorus level %q", level)
    78  }
    79  
    80  // A constant exposing all logging levels
    81  var AllLevels = []Level{
    82  	PanicLevel,
    83  	FatalLevel,
    84  	ErrorLevel,
    85  	WarnLevel,
    86  	InfoLevel,
    87  	DebugLevel,
    88  	TraceLevel,
    89  }
    90  
    91  // These are the different logging levels. You can set the logging level to log
    92  // on your instance of logger, obtained with `logrus.New()`.
    93  const (
    94  	// PanicLevel level, highest level of severity. Logs and then calls panic with the
    95  	// message passed to Debug, Info, ...
    96  	PanicLevel Level = iota
    97  	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
    98  	// logging level is set to Panic.
    99  	FatalLevel
   100  	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
   101  	// Commonly used for hooks to send errors to an error tracking service.
   102  	ErrorLevel
   103  	// WarnLevel level. Non-critical entries that deserve eyes.
   104  	WarnLevel
   105  	// InfoLevel level. General operational entries about what's going on inside the
   106  	// application.
   107  	InfoLevel
   108  	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
   109  	DebugLevel
   110  	// TraceLevel level. Designates finer-grained informational events than the Debug.
   111  	TraceLevel
   112  )
   113  
   114  // Won't compile if StdLogger can't be realized by a log.Logger
   115  var (
   116  	_ StdLogger = &log.Logger{}
   117  	_ StdLogger = &Entry{}
   118  	_ StdLogger = &Logger{}
   119  )
   120  
   121  // StdLogger is what your logrus-enabled library should take, that way
   122  // it'll accept a stdlib logger and a logrus logger. There's no standard
   123  // interface, this is the closest we get, unfortunately.
   124  type StdLogger interface {
   125  	Print(...interface{})
   126  	Printf(string, ...interface{})
   127  	Println(...interface{})
   128  
   129  	Fatal(...interface{})
   130  	Fatalf(string, ...interface{})
   131  	Fatalln(...interface{})
   132  
   133  	Panic(...interface{})
   134  	Panicf(string, ...interface{})
   135  	Panicln(...interface{})
   136  }
   137  
   138  // The FieldLogger interface generalizes the Entry and Logger types
   139  type FieldLogger interface {
   140  	WithField(key string, value interface{}) *Entry
   141  	WithFields(fields Fields) *Entry
   142  	WithError(err error) *Entry
   143  
   144  	Debugf(format string, args ...interface{})
   145  	Infof(format string, args ...interface{})
   146  	Printf(format string, args ...interface{})
   147  	Warnf(format string, args ...interface{})
   148  	Warningf(format string, args ...interface{})
   149  	Errorf(format string, args ...interface{})
   150  	Fatalf(format string, args ...interface{})
   151  	Panicf(format string, args ...interface{})
   152  
   153  	Debug(args ...interface{})
   154  	Info(args ...interface{})
   155  	Print(args ...interface{})
   156  	Warn(args ...interface{})
   157  	Warning(args ...interface{})
   158  	Error(args ...interface{})
   159  	Fatal(args ...interface{})
   160  	Panic(args ...interface{})
   161  
   162  	Debugln(args ...interface{})
   163  	Infoln(args ...interface{})
   164  	Println(args ...interface{})
   165  	Warnln(args ...interface{})
   166  	Warningln(args ...interface{})
   167  	Errorln(args ...interface{})
   168  	Fatalln(args ...interface{})
   169  	Panicln(args ...interface{})
   170  
   171  	// IsDebugEnabled() bool
   172  	// IsInfoEnabled() bool
   173  	// IsWarnEnabled() bool
   174  	// IsErrorEnabled() bool
   175  	// IsFatalEnabled() bool
   176  	// IsPanicEnabled() bool
   177  }
   178  
   179  // Ext1FieldLogger (the first extension to FieldLogger) is superfluous, it is
   180  // here for consistancy. Do not use. Use Logger or Entry instead.
   181  type Ext1FieldLogger interface {
   182  	FieldLogger
   183  	Tracef(format string, args ...interface{})
   184  	Trace(args ...interface{})
   185  	Traceln(args ...interface{})
   186  }
   187  

View as plain text