logrus/entry.go

223 lines
5.0 KiB
Go
Raw Normal View History

2014-02-24 00:50:42 +00:00
package logrus
import (
"bytes"
"fmt"
"io"
"os"
"time"
)
type Entry struct {
2014-04-23 16:54:53 +00:00
Logger *Logger
2014-02-24 00:50:42 +00:00
Data Fields
}
2014-03-04 16:38:21 +00:00
var baseTimestamp time.Time
2014-02-24 00:50:42 +00:00
func NewEntry(logger *Logger) *Entry {
return &Entry{
2014-04-23 16:54:53 +00:00
Logger: logger,
2014-03-10 23:22:08 +00:00
// Default is three fields, give a little extra room
2014-02-24 00:50:42 +00:00
Data: make(Fields, 5),
}
}
2014-02-24 11:34:12 +00:00
func (entry *Entry) Reader() (*bytes.Buffer, error) {
2014-04-23 16:54:53 +00:00
serialized, err := entry.Logger.Formatter.Format(entry)
2014-03-10 23:22:08 +00:00
return bytes.NewBuffer(serialized), err
}
func (entry *Entry) String() (string, error) {
reader, err := entry.Reader()
if err != nil {
return "", err
}
2014-02-24 00:50:42 +00:00
2014-03-10 23:22:08 +00:00
return reader.String(), err
2014-02-24 00:50:42 +00:00
}
func (entry *Entry) WithField(key string, value interface{}) *Entry {
2014-06-09 15:53:23 +00:00
return entry.WithFields(Fields{key: value})
2014-02-24 00:50:42 +00: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 00:50:42 +00:00
}
return &Entry{Logger: entry.Logger, Data: data}
2014-02-24 00:50:42 +00:00
}
2014-03-12 14:10:22 +00:00
func (entry *Entry) log(level string, levelInt Level, msg string) string {
2014-02-24 00:53:50 +00:00
entry.Data["time"] = time.Now().String()
2014-02-24 00:50:42 +00:00
entry.Data["level"] = level
entry.Data["msg"] = msg
if err := entry.Logger.Hooks.Fire(levelInt, entry); err != nil {
fmt.Fprintf(os.Stderr, "Failed to fire hook", err)
}
2014-02-24 00:50:42 +00:00
reader, err := entry.Reader()
if err != nil {
2014-03-03 22:35:32 +00:00
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v", err)
2014-02-24 00:50:42 +00:00
}
2014-04-23 16:54:53 +00:00
entry.Logger.mu.Lock()
defer entry.Logger.mu.Unlock()
2014-02-24 11:34:12 +00:00
2014-04-23 16:54:53 +00:00
_, err = io.Copy(entry.Logger.Out, reader)
2014-02-24 11:34:12 +00:00
if err != nil {
2014-03-10 23:22:08 +00:00
fmt.Fprintf(os.Stderr, "Failed to write to log, %v", err)
2014-02-24 00:50:42 +00:00
}
2014-02-24 11:34:12 +00:00
return reader.String()
2014-02-24 00:50:42 +00:00
}
func (entry *Entry) Debug(args ...interface{}) {
if entry.Logger.Level >= DebugLevel {
entry.log("debug", DebugLevel, fmt.Sprint(args...))
2014-02-24 00:50:42 +00:00
}
}
2014-03-10 23:59:18 +00:00
func (entry *Entry) Print(args ...interface{}) {
entry.Info(args...)
}
2014-02-24 00:50:42 +00:00
func (entry *Entry) Info(args ...interface{}) {
if entry.Logger.Level >= InfoLevel {
entry.log("info", InfoLevel, fmt.Sprint(args...))
2014-02-24 00:50:42 +00:00
}
}
2014-03-10 23:22:08 +00:00
func (entry *Entry) Warn(args ...interface{}) {
if entry.Logger.Level >= WarnLevel {
entry.log("warning", WarnLevel, fmt.Sprint(args...))
2014-03-10 23:22:08 +00:00
}
}
func (entry *Entry) Error(args ...interface{}) {
if entry.Logger.Level >= ErrorLevel {
entry.log("error", ErrorLevel, fmt.Sprint(args...))
2014-02-24 00:50:42 +00:00
}
}
func (entry *Entry) Fatal(args ...interface{}) {
if entry.Logger.Level >= FatalLevel {
entry.log("fatal", FatalLevel, fmt.Sprint(args...))
2014-02-24 00:50:42 +00:00
}
os.Exit(1)
}
func (entry *Entry) Panic(args ...interface{}) {
if entry.Logger.Level >= PanicLevel {
msg := entry.log("panic", PanicLevel, fmt.Sprint(args...))
2014-02-24 11:34:12 +00:00
panic(msg)
2014-02-24 00:50:42 +00:00
}
2014-02-24 11:34:12 +00:00
panic(fmt.Sprint(args...))
2014-02-24 00:50:42 +00: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 00:50:42 +00:00
}
func (entry *Entry) Infof(format string, args ...interface{}) {
if entry.Logger.Level >= InfoLevel {
entry.Info(fmt.Sprintf(format, args...))
}
2014-02-24 00:50:42 +00:00
}
func (entry *Entry) Printf(format string, args ...interface{}) {
entry.Infof(format, args...)
2014-02-24 00:50:42 +00:00
}
2014-03-10 23:22:08 +00:00
func (entry *Entry) Warnf(format string, args ...interface{}) {
if entry.Logger.Level >= WarnLevel {
entry.Warn(fmt.Sprintf(format, args...))
}
2014-03-10 23:22:08 +00:00
}
2014-02-24 00:50:42 +00:00
func (entry *Entry) Warningf(format string, args ...interface{}) {
entry.Warnf(format, args...)
2014-03-10 23:22:08 +00:00
}
func (entry *Entry) Errorf(format string, args ...interface{}) {
if entry.Logger.Level >= ErrorLevel {
entry.Error(fmt.Sprintf(format, args...))
}
2014-02-24 00:50:42 +00:00
}
func (entry *Entry) Fatalf(format string, args ...interface{}) {
if entry.Logger.Level >= FatalLevel {
entry.Fatal(fmt.Sprintf(format, args...))
}
2014-02-24 00:50:42 +00:00
}
func (entry *Entry) Panicf(format string, args ...interface{}) {
if entry.Logger.Level >= PanicLevel {
entry.Panic(fmt.Sprintf(format, args...))
}
2014-02-24 00:50:42 +00:00
}
// Entry Println family functions
func (entry *Entry) Debugln(args ...interface{}) {
if entry.Logger.Level >= DebugLevel {
entry.Debug(entry.sprintlnn(args...))
}
2014-02-24 00:50:42 +00:00
}
func (entry *Entry) Infoln(args ...interface{}) {
if entry.Logger.Level >= InfoLevel {
entry.Info(entry.sprintlnn(args...))
}
2014-02-24 00:50:42 +00:00
}
func (entry *Entry) Println(args ...interface{}) {
entry.Infoln(args...)
2014-03-10 23:22:08 +00:00
}
func (entry *Entry) Warnln(args ...interface{}) {
if entry.Logger.Level >= WarnLevel {
entry.Warn(entry.sprintlnn(args...))
}
2014-02-24 00:50:42 +00:00
}
func (entry *Entry) Warningln(args ...interface{}) {
entry.Warnln(args...)
2014-02-24 00:50:42 +00:00
}
2014-03-10 23:22:08 +00:00
func (entry *Entry) Errorln(args ...interface{}) {
if entry.Logger.Level >= ErrorLevel {
entry.Error(entry.sprintlnn(args...))
}
2014-02-24 00:50:42 +00:00
}
2014-03-10 23:22:08 +00:00
func (entry *Entry) Fatalln(args ...interface{}) {
if entry.Logger.Level >= FatalLevel {
entry.Fatal(entry.sprintlnn(args...))
}
2014-02-24 00:50:42 +00:00
}
2014-03-10 23:22:08 +00: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 00:50:42 +00:00
}