|
@@ -4,11 +4,30 @@ import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
+ "reflect"
|
|
|
+ "runtime"
|
|
|
+ "strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-var bufferPool *sync.Pool
|
|
|
+var (
|
|
|
+ bufferPool *sync.Pool
|
|
|
+
|
|
|
+ // qualified package name, cached at first use
|
|
|
+ logrusPackage string
|
|
|
+
|
|
|
+ // Positions in the call stack when tracing to report the calling method
|
|
|
+ minimumCallerDepth int
|
|
|
+
|
|
|
+ // Used for caller information initialisation
|
|
|
+ callerInitOnce sync.Once
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ maximumCallerDepth int = 25
|
|
|
+ knownLogrusFrames int = 4
|
|
|
+)
|
|
|
|
|
|
func init() {
|
|
|
bufferPool = &sync.Pool{
|
|
@@ -16,15 +35,18 @@ func init() {
|
|
|
return new(bytes.Buffer)
|
|
|
},
|
|
|
}
|
|
|
+
|
|
|
+ // start at the bottom of the stack before the package-name cache is primed
|
|
|
+ minimumCallerDepth = 1
|
|
|
}
|
|
|
|
|
|
// Defines the key when adding errors using WithError.
|
|
|
var ErrorKey = "error"
|
|
|
|
|
|
// An entry is the final or intermediate Logrus logging entry. It contains all
|
|
|
-// the fields passed with WithField{,s}. It's finally logged when Debug, Info,
|
|
|
-// Warn, Error, Fatal or Panic is called on it. These objects can be reused and
|
|
|
-// passed around as much as you wish to avoid field duplication.
|
|
|
+// the fields passed with WithField{,s}. It's finally logged when Trace, Debug,
|
|
|
+// Info, Warn, Error, Fatal or Panic is called on it. These objects can be
|
|
|
+// reused and passed around as much as you wish to avoid field duplication.
|
|
|
type Entry struct {
|
|
|
Logger *Logger
|
|
|
|
|
@@ -34,22 +56,28 @@ type Entry struct {
|
|
|
// Time at which the log entry was created
|
|
|
Time time.Time
|
|
|
|
|
|
- // Level the log entry was logged at: Debug, Info, Warn, Error, Fatal or Panic
|
|
|
+ // Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic
|
|
|
// This field will be set on entry firing and the value will be equal to the one in Logger struct field.
|
|
|
Level Level
|
|
|
|
|
|
- // Message passed to Debug, Info, Warn, Error, Fatal or Panic
|
|
|
+ // Calling method, with package name
|
|
|
+ Caller *runtime.Frame
|
|
|
+
|
|
|
+ // Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic
|
|
|
Message string
|
|
|
|
|
|
- // When formatter is called in entry.log(), an Buffer may be set to entry
|
|
|
+ // When formatter is called in entry.log(), a Buffer may be set to entry
|
|
|
Buffer *bytes.Buffer
|
|
|
+
|
|
|
+ // err may contain a field formatting error
|
|
|
+ err string
|
|
|
}
|
|
|
|
|
|
func NewEntry(logger *Logger) *Entry {
|
|
|
return &Entry{
|
|
|
Logger: logger,
|
|
|
- // Default is five fields, give a little extra room
|
|
|
- Data: make(Fields, 5),
|
|
|
+ // Default is three fields, plus one optional. Give a little extra room.
|
|
|
+ Data: make(Fields, 6),
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -80,15 +108,85 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
|
|
|
for k, v := range entry.Data {
|
|
|
data[k] = v
|
|
|
}
|
|
|
+ fieldErr := entry.err
|
|
|
for k, v := range fields {
|
|
|
- data[k] = v
|
|
|
+ isErrField := false
|
|
|
+ if t := reflect.TypeOf(v); t != nil {
|
|
|
+ switch t.Kind() {
|
|
|
+ case reflect.Func:
|
|
|
+ isErrField = true
|
|
|
+ case reflect.Ptr:
|
|
|
+ isErrField = t.Elem().Kind() == reflect.Func
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if isErrField {
|
|
|
+ tmp := fmt.Sprintf("can not add field %q", k)
|
|
|
+ if fieldErr != "" {
|
|
|
+ fieldErr = entry.err + ", " + tmp
|
|
|
+ } else {
|
|
|
+ fieldErr = tmp
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ data[k] = v
|
|
|
+ }
|
|
|
}
|
|
|
- return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time}
|
|
|
+ return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr}
|
|
|
}
|
|
|
|
|
|
// Overrides the time of the Entry.
|
|
|
func (entry *Entry) WithTime(t time.Time) *Entry {
|
|
|
- return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t}
|
|
|
+ return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t, err: entry.err}
|
|
|
+}
|
|
|
+
|
|
|
+// getPackageName reduces a fully qualified function name to the package name
|
|
|
+// There really ought to be to be a better way...
|
|
|
+func getPackageName(f string) string {
|
|
|
+ for {
|
|
|
+ lastPeriod := strings.LastIndex(f, ".")
|
|
|
+ lastSlash := strings.LastIndex(f, "/")
|
|
|
+ if lastPeriod > lastSlash {
|
|
|
+ f = f[:lastPeriod]
|
|
|
+ } else {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return f
|
|
|
+}
|
|
|
+
|
|
|
+// getCaller retrieves the name of the first non-logrus calling function
|
|
|
+func getCaller() *runtime.Frame {
|
|
|
+ // Restrict the lookback frames to avoid runaway lookups
|
|
|
+ pcs := make([]uintptr, maximumCallerDepth)
|
|
|
+ depth := runtime.Callers(minimumCallerDepth, pcs)
|
|
|
+ frames := runtime.CallersFrames(pcs[:depth])
|
|
|
+
|
|
|
+ // cache this package's fully-qualified name
|
|
|
+ callerInitOnce.Do(func() {
|
|
|
+ logrusPackage = getPackageName(runtime.FuncForPC(pcs[0]).Name())
|
|
|
+
|
|
|
+ // now that we have the cache, we can skip a minimum count of known-logrus functions
|
|
|
+ // XXX this is dubious, the number of frames may vary store an entry in a logger interface
|
|
|
+ minimumCallerDepth = knownLogrusFrames
|
|
|
+ })
|
|
|
+
|
|
|
+ for f, again := frames.Next(); again; f, again = frames.Next() {
|
|
|
+ pkg := getPackageName(f.Function)
|
|
|
+
|
|
|
+ // If the caller isn't part of this package, we're done
|
|
|
+ if pkg != logrusPackage {
|
|
|
+ return &f
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // if we got here, we failed to find the caller's context
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (entry Entry) HasCaller() (has bool) {
|
|
|
+ return entry.Logger != nil &&
|
|
|
+ entry.Logger.ReportCaller &&
|
|
|
+ entry.Caller != nil
|
|
|
}
|
|
|
|
|
|
// This function is not declared with a pointer value because otherwise
|
|
@@ -107,6 +205,9 @@ func (entry Entry) log(level Level, msg string) {
|
|
|
|
|
|
entry.Level = level
|
|
|
entry.Message = msg
|
|
|
+ if entry.Logger.ReportCaller {
|
|
|
+ entry.Caller = getCaller()
|
|
|
+ }
|
|
|
|
|
|
entry.fireHooks()
|
|
|
|
|
@@ -137,9 +238,9 @@ func (entry *Entry) fireHooks() {
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) write() {
|
|
|
- serialized, err := entry.Logger.Formatter.Format(entry)
|
|
|
entry.Logger.mu.Lock()
|
|
|
defer entry.Logger.mu.Unlock()
|
|
|
+ serialized, err := entry.Logger.Formatter.Format(entry)
|
|
|
if err != nil {
|
|
|
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
|
|
|
} else {
|
|
@@ -150,26 +251,30 @@ func (entry *Entry) write() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (entry *Entry) Debug(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= DebugLevel {
|
|
|
- entry.log(DebugLevel, fmt.Sprint(args...))
|
|
|
+func (entry *Entry) Log(level Level, args ...interface{}) {
|
|
|
+ if entry.Logger.IsLevelEnabled(level) {
|
|
|
+ entry.log(level, fmt.Sprint(args...))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (entry *Entry) Trace(args ...interface{}) {
|
|
|
+ entry.Log(TraceLevel, args...)
|
|
|
+}
|
|
|
+
|
|
|
+func (entry *Entry) Debug(args ...interface{}) {
|
|
|
+ entry.Log(DebugLevel, args...)
|
|
|
+}
|
|
|
+
|
|
|
func (entry *Entry) Print(args ...interface{}) {
|
|
|
entry.Info(args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Info(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= InfoLevel {
|
|
|
- entry.log(InfoLevel, fmt.Sprint(args...))
|
|
|
- }
|
|
|
+ entry.Log(InfoLevel, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Warn(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= WarnLevel {
|
|
|
- entry.log(WarnLevel, fmt.Sprint(args...))
|
|
|
- }
|
|
|
+ entry.Log(WarnLevel, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Warning(args ...interface{}) {
|
|
@@ -177,37 +282,35 @@ func (entry *Entry) Warning(args ...interface{}) {
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Error(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= ErrorLevel {
|
|
|
- entry.log(ErrorLevel, fmt.Sprint(args...))
|
|
|
- }
|
|
|
+ entry.Log(ErrorLevel, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Fatal(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= FatalLevel {
|
|
|
- entry.log(FatalLevel, fmt.Sprint(args...))
|
|
|
- }
|
|
|
- Exit(1)
|
|
|
+ entry.Log(FatalLevel, args...)
|
|
|
+ entry.Logger.Exit(1)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Panic(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= PanicLevel {
|
|
|
- entry.log(PanicLevel, fmt.Sprint(args...))
|
|
|
- }
|
|
|
+ entry.Log(PanicLevel, args...)
|
|
|
panic(fmt.Sprint(args...))
|
|
|
}
|
|
|
|
|
|
// Entry Printf family functions
|
|
|
|
|
|
+func (entry *Entry) Logf(level Level, format string, args ...interface{}) {
|
|
|
+ entry.Log(level, fmt.Sprintf(format, args...))
|
|
|
+}
|
|
|
+
|
|
|
+func (entry *Entry) Tracef(format string, args ...interface{}) {
|
|
|
+ entry.Logf(TraceLevel, format, args...)
|
|
|
+}
|
|
|
+
|
|
|
func (entry *Entry) Debugf(format string, args ...interface{}) {
|
|
|
- if entry.Logger.level() >= DebugLevel {
|
|
|
- entry.Debug(fmt.Sprintf(format, args...))
|
|
|
- }
|
|
|
+ entry.Logf(DebugLevel, format, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Infof(format string, args ...interface{}) {
|
|
|
- if entry.Logger.level() >= InfoLevel {
|
|
|
- entry.Info(fmt.Sprintf(format, args...))
|
|
|
- }
|
|
|
+ entry.Logf(InfoLevel, format, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Printf(format string, args ...interface{}) {
|
|
@@ -215,9 +318,7 @@ func (entry *Entry) Printf(format string, args ...interface{}) {
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
|
|
- if entry.Logger.level() >= WarnLevel {
|
|
|
- entry.Warn(fmt.Sprintf(format, args...))
|
|
|
- }
|
|
|
+ entry.Logf(WarnLevel, format, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Warningf(format string, args ...interface{}) {
|
|
@@ -225,36 +326,36 @@ func (entry *Entry) Warningf(format string, args ...interface{}) {
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
|
|
- if entry.Logger.level() >= ErrorLevel {
|
|
|
- entry.Error(fmt.Sprintf(format, args...))
|
|
|
- }
|
|
|
+ entry.Logf(ErrorLevel, format, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
|
|
- if entry.Logger.level() >= FatalLevel {
|
|
|
- entry.Fatal(fmt.Sprintf(format, args...))
|
|
|
- }
|
|
|
- Exit(1)
|
|
|
+ entry.Logf(FatalLevel, format, args...)
|
|
|
+ entry.Logger.Exit(1)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Panicf(format string, args ...interface{}) {
|
|
|
- if entry.Logger.level() >= PanicLevel {
|
|
|
- entry.Panic(fmt.Sprintf(format, args...))
|
|
|
- }
|
|
|
+ entry.Logf(PanicLevel, format, args...)
|
|
|
}
|
|
|
|
|
|
// Entry Println family functions
|
|
|
|
|
|
-func (entry *Entry) Debugln(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= DebugLevel {
|
|
|
- entry.Debug(entry.sprintlnn(args...))
|
|
|
+func (entry *Entry) Logln(level Level, args ...interface{}) {
|
|
|
+ if entry.Logger.IsLevelEnabled(level) {
|
|
|
+ entry.Log(level, entry.sprintlnn(args...))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (entry *Entry) Traceln(args ...interface{}) {
|
|
|
+ entry.Logln(TraceLevel, args...)
|
|
|
+}
|
|
|
+
|
|
|
+func (entry *Entry) Debugln(args ...interface{}) {
|
|
|
+ entry.Logln(DebugLevel, args...)
|
|
|
+}
|
|
|
+
|
|
|
func (entry *Entry) Infoln(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= InfoLevel {
|
|
|
- entry.Info(entry.sprintlnn(args...))
|
|
|
- }
|
|
|
+ entry.Logln(InfoLevel, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Println(args ...interface{}) {
|
|
@@ -262,9 +363,7 @@ func (entry *Entry) Println(args ...interface{}) {
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Warnln(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= WarnLevel {
|
|
|
- entry.Warn(entry.sprintlnn(args...))
|
|
|
- }
|
|
|
+ entry.Logln(WarnLevel, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Warningln(args ...interface{}) {
|
|
@@ -272,22 +371,16 @@ func (entry *Entry) Warningln(args ...interface{}) {
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Errorln(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= ErrorLevel {
|
|
|
- entry.Error(entry.sprintlnn(args...))
|
|
|
- }
|
|
|
+ entry.Logln(ErrorLevel, args...)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Fatalln(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= FatalLevel {
|
|
|
- entry.Fatal(entry.sprintlnn(args...))
|
|
|
- }
|
|
|
- Exit(1)
|
|
|
+ entry.Logln(FatalLevel, args...)
|
|
|
+ entry.Logger.Exit(1)
|
|
|
}
|
|
|
|
|
|
func (entry *Entry) Panicln(args ...interface{}) {
|
|
|
- if entry.Logger.level() >= PanicLevel {
|
|
|
- entry.Panic(entry.sprintlnn(args...))
|
|
|
- }
|
|
|
+ entry.Logln(PanicLevel, args...)
|
|
|
}
|
|
|
|
|
|
// Sprintlnn => Sprint no newline. This is to get the behavior of how
|