2016-01-21 04:19:15 +01:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-10-11 18:41:41 +02:00
|
|
|
"reflect"
|
2016-12-16 12:52:12 +01:00
|
|
|
"sync"
|
2016-01-21 04:19:15 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-12-16 12:52:12 +01:00
|
|
|
var bufferPool *sync.Pool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bufferPool = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 04:19:15 +01:00
|
|
|
// 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.
|
|
|
|
type Entry struct {
|
|
|
|
Logger *Logger
|
|
|
|
|
|
|
|
// Contains all the fields set by the user.
|
|
|
|
Data Fields
|
|
|
|
|
|
|
|
// 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
|
2017-10-05 16:20:47 +02:00
|
|
|
// This field will be set on entry firing and the value will be equal to the one in Logger struct field.
|
2016-01-21 04:19:15 +01:00
|
|
|
Level Level
|
|
|
|
|
|
|
|
// Message passed to Debug, Info, Warn, Error, Fatal or Panic
|
|
|
|
Message string
|
2016-12-16 12:52:12 +01:00
|
|
|
|
2018-10-11 18:41:41 +02:00
|
|
|
// When formatter is called in entry.log(), a Buffer may be set to entry
|
2016-12-16 12:52:12 +01:00
|
|
|
Buffer *bytes.Buffer
|
2018-10-11 18:41:41 +02:00
|
|
|
|
|
|
|
// err may contain a field formatting error
|
|
|
|
err string
|
2016-01-21 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEntry(logger *Logger) *Entry {
|
|
|
|
return &Entry{
|
|
|
|
Logger: logger,
|
2018-10-11 18:41:41 +02:00
|
|
|
// Default is five fields, give a little extra room
|
2016-01-21 04:19:15 +01:00
|
|
|
Data: make(Fields, 5),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the string representation from the reader and ultimately the
|
|
|
|
// formatter.
|
|
|
|
func (entry *Entry) String() (string, error) {
|
2016-12-16 12:52:12 +01:00
|
|
|
serialized, err := entry.Logger.Formatter.Format(entry)
|
2016-01-21 04:19:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-12-16 12:52:12 +01:00
|
|
|
str := string(serialized)
|
|
|
|
return str, nil
|
2016-01-21 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add an error as single field (using the key defined in ErrorKey) to the Entry.
|
|
|
|
func (entry *Entry) WithError(err error) *Entry {
|
|
|
|
return entry.WithField(ErrorKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a single field to the Entry.
|
|
|
|
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
|
|
|
return entry.WithFields(Fields{key: value})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a map of fields to the Entry.
|
|
|
|
func (entry *Entry) WithFields(fields Fields) *Entry {
|
2016-12-16 12:52:12 +01:00
|
|
|
data := make(Fields, len(entry.Data)+len(fields))
|
2016-01-21 04:19:15 +01:00
|
|
|
for k, v := range entry.Data {
|
|
|
|
data[k] = v
|
|
|
|
}
|
2018-10-11 18:41:41 +02:00
|
|
|
var field_err string
|
2016-01-21 04:19:15 +01:00
|
|
|
for k, v := range fields {
|
2018-10-11 18:41:41 +02:00
|
|
|
if t := reflect.TypeOf(v); t != nil && t.Kind() == reflect.Func {
|
|
|
|
field_err = fmt.Sprintf("can not add field %q", k)
|
|
|
|
if entry.err != "" {
|
|
|
|
field_err = entry.err + ", " + field_err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data[k] = v
|
|
|
|
}
|
2016-01-21 04:19:15 +01:00
|
|
|
}
|
2018-10-11 18:41:41 +02:00
|
|
|
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: field_err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overrides the time of the Entry.
|
|
|
|
func (entry *Entry) WithTime(t time.Time) *Entry {
|
|
|
|
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t}
|
2016-01-21 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is not declared with a pointer value because otherwise
|
|
|
|
// race conditions will occur when using multiple goroutines
|
|
|
|
func (entry Entry) log(level Level, msg string) {
|
2016-12-16 12:52:12 +01:00
|
|
|
var buffer *bytes.Buffer
|
2018-10-11 18:41:41 +02:00
|
|
|
|
|
|
|
// Default to now, but allow users to override if they want.
|
|
|
|
//
|
|
|
|
// We don't have to worry about polluting future calls to Entry#log()
|
|
|
|
// with this assignment because this function is declared with a
|
|
|
|
// non-pointer receiver.
|
|
|
|
if entry.Time.IsZero() {
|
|
|
|
entry.Time = time.Now()
|
|
|
|
}
|
|
|
|
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Level = level
|
|
|
|
entry.Message = msg
|
|
|
|
|
2018-10-11 18:41:41 +02:00
|
|
|
entry.fireHooks()
|
|
|
|
|
2016-12-16 12:52:12 +01:00
|
|
|
buffer = bufferPool.Get().(*bytes.Buffer)
|
|
|
|
buffer.Reset()
|
|
|
|
defer bufferPool.Put(buffer)
|
|
|
|
entry.Buffer = buffer
|
2018-10-11 18:41:41 +02:00
|
|
|
|
|
|
|
entry.write()
|
|
|
|
|
2016-12-16 12:52:12 +01:00
|
|
|
entry.Buffer = nil
|
2018-10-11 18:41:41 +02:00
|
|
|
|
|
|
|
// To avoid Entry#log() returning a value that only would make sense for
|
|
|
|
// panic() to use in Entry#Panic(), we avoid the allocation by checking
|
|
|
|
// directly here.
|
|
|
|
if level <= PanicLevel {
|
|
|
|
panic(&entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) fireHooks() {
|
|
|
|
entry.Logger.mu.Lock()
|
|
|
|
defer entry.Logger.mu.Unlock()
|
|
|
|
err := entry.Logger.Hooks.Fire(entry.Level, entry)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) write() {
|
|
|
|
entry.Logger.mu.Lock()
|
|
|
|
defer entry.Logger.mu.Unlock()
|
|
|
|
serialized, err := entry.Logger.Formatter.Format(entry)
|
2016-01-21 04:19:15 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
|
2016-12-16 12:52:12 +01:00
|
|
|
} else {
|
|
|
|
_, err = entry.Logger.Out.Write(serialized)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
|
|
|
|
}
|
2016-01-21 04:19:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Debug(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(DebugLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.log(DebugLevel, fmt.Sprint(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Print(args ...interface{}) {
|
|
|
|
entry.Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Info(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(InfoLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.log(InfoLevel, fmt.Sprint(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Warn(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(WarnLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.log(WarnLevel, fmt.Sprint(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Warning(args ...interface{}) {
|
|
|
|
entry.Warn(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Error(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(ErrorLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.log(ErrorLevel, fmt.Sprint(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Fatal(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.log(FatalLevel, fmt.Sprint(args...))
|
|
|
|
}
|
2016-12-16 12:52:12 +01:00
|
|
|
Exit(1)
|
2016-01-21 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Panic(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(PanicLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.log(PanicLevel, fmt.Sprint(args...))
|
|
|
|
}
|
|
|
|
panic(fmt.Sprint(args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry Printf family functions
|
|
|
|
|
|
|
|
func (entry *Entry) Debugf(format string, args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(DebugLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Debug(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Infof(format string, args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(InfoLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Info(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Printf(format string, args ...interface{}) {
|
|
|
|
entry.Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(WarnLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Warn(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Warningf(format string, args ...interface{}) {
|
|
|
|
entry.Warnf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(ErrorLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Error(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Fatal(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2016-12-16 12:52:12 +01:00
|
|
|
Exit(1)
|
2016-01-21 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Panicf(format string, args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(PanicLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Panic(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry Println family functions
|
|
|
|
|
|
|
|
func (entry *Entry) Debugln(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(DebugLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Debug(entry.sprintlnn(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Infoln(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(InfoLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Info(entry.sprintlnn(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Println(args ...interface{}) {
|
|
|
|
entry.Infoln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Warnln(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(WarnLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Warn(entry.sprintlnn(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Warningln(args ...interface{}) {
|
|
|
|
entry.Warnln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Errorln(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(ErrorLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Error(entry.sprintlnn(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Fatalln(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Fatal(entry.sprintlnn(args...))
|
|
|
|
}
|
2016-12-16 12:52:12 +01:00
|
|
|
Exit(1)
|
2016-01-21 04:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Panicln(args ...interface{}) {
|
2018-10-11 18:41:41 +02:00
|
|
|
if entry.Logger.IsLevelEnabled(PanicLevel) {
|
2016-01-21 04:19:15 +01:00
|
|
|
entry.Panic(entry.sprintlnn(args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sprintlnn => Sprint no newline. This is to get the behavior of how
|
|
|
|
// fmt.Sprintln where spaces are always added between operands, regardless of
|
|
|
|
// their type. Instead of vendoring the Sprintln implementation to spare a
|
|
|
|
// string allocation, we do the simplest thing.
|
|
|
|
func (entry *Entry) sprintlnn(args ...interface{}) string {
|
|
|
|
msg := fmt.Sprintln(args...)
|
|
|
|
return msg[:len(msg)-1]
|
|
|
|
}
|