logrus/entry.go

265 lines
6.4 KiB
Go
Raw Permalink Normal View History

2014-02-24 04:50:42 +04:00
package logrus
import (
"bytes"
"fmt"
"io"
"os"
"time"
)
// Defines the key when adding errors using WithError.
2015-05-13 14:35:03 +03:00
var ErrorKey = "error"
2014-10-16 16:39:13 +04:00
// An entry is the final or intermediate Logrus logging entry. It contains all
2014-07-27 05:37:06 +04:00
// 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.
2014-02-24 04:50:42 +04:00
type Entry struct {
2014-04-23 20:54:53 +04:00
Logger *Logger
2014-07-27 05:37:06 +04:00
// 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
Level Level
// Message passed to Debug, Info, Warn, Error, Fatal or Panic
Message string
2014-02-24 04:50:42 +04:00
}
func NewEntry(logger *Logger) *Entry {
return &Entry{
2014-04-23 20:54:53 +04:00
Logger: logger,
2014-03-11 03:22:08 +04:00
// Default is three fields, give a little extra room
Data: make(Fields, 5),
2014-02-24 04:50:42 +04:00
}
}
2014-07-27 05:37:06 +04:00
// Returns a reader for the entry, which is a proxy to the formatter.
2014-02-24 15:34:12 +04:00
func (entry *Entry) Reader() (*bytes.Buffer, error) {
2014-04-23 20:54:53 +04:00
serialized, err := entry.Logger.Formatter.Format(entry)
2014-03-11 03:22:08 +04:00
return bytes.NewBuffer(serialized), err
}
2014-07-27 05:37:06 +04:00
// Returns the string representation from the reader and ultimately the
// formatter.
2014-03-11 03:22:08 +04:00
func (entry *Entry) String() (string, error) {
reader, err := entry.Reader()
if err != nil {
return "", err
}
2014-02-24 04:50:42 +04:00
2014-03-11 03:22:08 +04:00
return reader.String(), err
2014-02-24 04:50:42 +04:00
}
// Add an error as single field (using the key defined in ErrorKey) to the Entry.
2015-05-13 14:35:03 +03:00
func (entry *Entry) WithError(err error) *Entry {
return entry.WithField(ErrorKey, err)
}
2014-07-27 05:37:06 +04:00
// Add a single field to the Entry.
2014-02-24 04:50:42 +04:00
func (entry *Entry) WithField(key string, value interface{}) *Entry {
2014-06-09 19:53:23 +04:00
return entry.WithFields(Fields{key: value})
2014-02-24 04:50:42 +04:00
}
2014-07-27 05:37:06 +04:00
// Add a map of fields to the Entry.
2014-02-24 04:50:42 +04:00
func (entry *Entry) WithFields(fields Fields) *Entry {
data := Fields{}
for k, v := range entry.Data {
data[k] = v
}
for k, v := range fields {
data[k] = v
2014-02-24 04:50:42 +04:00
}
return &Entry{Logger: entry.Logger, Data: data}
2014-02-24 04:50:42 +04: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) {
entry.Time = time.Now()
entry.Level = level
entry.Message = msg
2014-02-24 04:50:42 +04:00
if err := entry.Logger.Hooks.Fire(level, &entry); err != nil {
entry.Logger.mu.Lock()
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
entry.Logger.mu.Unlock()
}
2014-02-24 04:50:42 +04:00
reader, err := entry.Reader()
if err != nil {
entry.Logger.mu.Lock()
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
entry.Logger.mu.Unlock()
2014-02-24 04:50:42 +04:00
}
2014-04-23 20:54:53 +04:00
entry.Logger.mu.Lock()
defer entry.Logger.mu.Unlock()
2014-02-24 15:34:12 +04:00
_, err = io.Copy(entry.Logger.Out, reader)
2014-02-24 15:34:12 +04:00
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
2014-02-24 04:50:42 +04:00
}
2014-02-24 15:34:12 +04: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)
}
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Debug(args ...interface{}) {
if entry.Logger.Level >= DebugLevel {
2014-07-27 05:03:20 +04:00
entry.log(DebugLevel, fmt.Sprint(args...))
2014-02-24 04:50:42 +04:00
}
}
func (entry *Entry) Print(args ...interface{}) {
entry.Info(args...)
2014-03-11 03:59:18 +04:00
}
func (entry *Entry) Info(args ...interface{}) {
if entry.Logger.Level >= InfoLevel {
2014-07-27 05:03:20 +04:00
entry.log(InfoLevel, fmt.Sprint(args...))
2014-02-24 04:50:42 +04:00
}
}
func (entry *Entry) Warn(args ...interface{}) {
if entry.Logger.Level >= WarnLevel {
2014-07-27 05:03:20 +04:00
entry.log(WarnLevel, fmt.Sprint(args...))
2014-03-11 03:22:08 +04:00
}
}
func (entry *Entry) Warning(args ...interface{}) {
entry.Warn(args...)
}
func (entry *Entry) Error(args ...interface{}) {
if entry.Logger.Level >= ErrorLevel {
2014-07-27 05:03:20 +04:00
entry.log(ErrorLevel, fmt.Sprint(args...))
2014-02-24 04:50:42 +04:00
}
}
func (entry *Entry) Fatal(args ...interface{}) {
if entry.Logger.Level >= FatalLevel {
2014-07-27 05:03:20 +04:00
entry.log(FatalLevel, fmt.Sprint(args...))
2014-02-24 04:50:42 +04:00
}
os.Exit(1)
}
func (entry *Entry) Panic(args ...interface{}) {
if entry.Logger.Level >= PanicLevel {
entry.log(PanicLevel, fmt.Sprint(args...))
2014-02-24 04:50:42 +04:00
}
2014-02-24 15:34:12 +04:00
panic(fmt.Sprint(args...))
2014-02-24 04:50:42 +04:00
}
// Entry Printf family functions
func (entry *Entry) Debugf(format string, args ...interface{}) {
if entry.Logger.Level >= DebugLevel {
entry.Debug(fmt.Sprintf(format, args...))
}
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Infof(format string, args ...interface{}) {
if entry.Logger.Level >= InfoLevel {
entry.Info(fmt.Sprintf(format, args...))
}
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Printf(format string, args ...interface{}) {
entry.Infof(format, args...)
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Warnf(format string, args ...interface{}) {
if entry.Logger.Level >= WarnLevel {
entry.Warn(fmt.Sprintf(format, args...))
}
2014-03-11 03:22:08 +04:00
}
func (entry *Entry) Warningf(format string, args ...interface{}) {
entry.Warnf(format, args...)
2014-03-11 03:22:08 +04:00
}
func (entry *Entry) Errorf(format string, args ...interface{}) {
if entry.Logger.Level >= ErrorLevel {
entry.Error(fmt.Sprintf(format, args...))
}
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Fatalf(format string, args ...interface{}) {
if entry.Logger.Level >= FatalLevel {
entry.Fatal(fmt.Sprintf(format, args...))
}
os.Exit(1)
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Panicf(format string, args ...interface{}) {
if entry.Logger.Level >= PanicLevel {
entry.Panic(fmt.Sprintf(format, args...))
}
2014-02-24 04:50:42 +04:00
}
// Entry Println family functions
func (entry *Entry) Debugln(args ...interface{}) {
if entry.Logger.Level >= DebugLevel {
entry.Debug(entry.sprintlnn(args...))
}
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Infoln(args ...interface{}) {
if entry.Logger.Level >= InfoLevel {
entry.Info(entry.sprintlnn(args...))
}
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Println(args ...interface{}) {
entry.Infoln(args...)
2014-03-11 03:22:08 +04:00
}
func (entry *Entry) Warnln(args ...interface{}) {
if entry.Logger.Level >= WarnLevel {
entry.Warn(entry.sprintlnn(args...))
}
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Warningln(args ...interface{}) {
entry.Warnln(args...)
2014-02-24 04:50:42 +04:00
}
func (entry *Entry) Errorln(args ...interface{}) {
if entry.Logger.Level >= ErrorLevel {
entry.Error(entry.sprintlnn(args...))
}
2014-02-24 04:50:42 +04:00
}
2014-03-11 03:22:08 +04:00
func (entry *Entry) Fatalln(args ...interface{}) {
if entry.Logger.Level >= FatalLevel {
entry.Fatal(entry.sprintlnn(args...))
}
os.Exit(1)
2014-02-24 04:50:42 +04:00
}
2014-03-11 03:22:08 +04:00
func (entry *Entry) Panicln(args ...interface{}) {
if entry.Logger.Level >= PanicLevel {
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]
2014-02-24 04:50:42 +04:00
}