2014-02-24 04:50:42 +04:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Entry struct {
|
2014-04-23 20:54:53 +04:00
|
|
|
Logger *Logger
|
2014-02-24 04:50:42 +04:00
|
|
|
Data Fields
|
|
|
|
}
|
|
|
|
|
2014-03-04 20:38:21 +04:00
|
|
|
var baseTimestamp time.Time
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
baseTimestamp = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func miniTS() int {
|
|
|
|
return int(time.Since(baseTimestamp) / time.Second)
|
|
|
|
}
|
|
|
|
|
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
|
2014-02-24 04:50:42 +04:00
|
|
|
Data: make(Fields, 5),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) String() (string, error) {
|
|
|
|
reader, err := entry.Reader()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-03-04 02:33:35 +04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
|
|
|
entry.Data[key] = value
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) WithFields(fields Fields) *Entry {
|
|
|
|
for key, value := range fields {
|
|
|
|
entry.WithField(key, value)
|
|
|
|
}
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
2014-03-12 18:10:22 +04:00
|
|
|
func (entry *Entry) log(level string, levelInt Level, msg string) string {
|
2014-02-24 04:53:50 +04:00
|
|
|
entry.Data["time"] = time.Now().String()
|
2014-02-24 04:50:42 +04:00
|
|
|
entry.Data["level"] = level
|
|
|
|
entry.Data["msg"] = msg
|
|
|
|
|
|
|
|
reader, err := entry.Reader()
|
|
|
|
if err != nil {
|
2014-03-04 02:35:32 +04:00
|
|
|
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v", err)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-04-23 20:54:53 +04:00
|
|
|
if err := entry.Logger.Hooks.Fire(levelInt, entry); err != nil {
|
2014-03-12 18:10:22 +04:00
|
|
|
fmt.Fprintf(os.Stderr, "Failed to fire hook", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-04-23 20:54:53 +04:00
|
|
|
_, err = io.Copy(entry.Logger.Out, reader)
|
2014-02-24 15:34:12 +04:00
|
|
|
if err != nil {
|
2014-03-11 03:22:08 +04:00
|
|
|
fmt.Fprintf(os.Stderr, "Failed to write to log, %v", err)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
2014-02-24 15:34:12 +04:00
|
|
|
|
|
|
|
return reader.String()
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Debug(args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Debug {
|
2014-03-12 18:10:22 +04:00
|
|
|
entry.log("debug", Debug, fmt.Sprint(args...))
|
2014-04-23 20:54:53 +04:00
|
|
|
entry.Logger.Hooks.Fire(Debug, entry)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:59:18 +04:00
|
|
|
func (entry *Entry) Print(args ...interface{}) {
|
|
|
|
entry.Info(args...)
|
|
|
|
}
|
|
|
|
|
2014-02-24 04:50:42 +04:00
|
|
|
func (entry *Entry) Info(args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Info {
|
2014-03-12 18:10:22 +04:00
|
|
|
entry.log("info", Info, fmt.Sprint(args...))
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
func (entry *Entry) Warn(args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Warn {
|
2014-03-12 18:10:22 +04:00
|
|
|
entry.log("warning", Warn, fmt.Sprint(args...))
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Error(args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Error {
|
2014-03-12 18:10:22 +04:00
|
|
|
entry.log("error", Error, fmt.Sprint(args...))
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Fatal(args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Fatal {
|
2014-03-12 18:10:22 +04:00
|
|
|
entry.log("fatal", Fatal, fmt.Sprint(args...))
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Panic(args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Panic {
|
2014-03-12 18:10:22 +04:00
|
|
|
msg := entry.log("panic", Panic, fmt.Sprint(args...))
|
2014-02-24 15:34:12 +04:00
|
|
|
panic(msg)
|
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{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Debug {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Debug(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Infof(format string, args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Info {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Info(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Printf(format string, args ...interface{}) {
|
2014-03-26 09:51:40 +04:00
|
|
|
entry.Infof(format, args...)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Warn {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Warn(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
2014-02-24 04:50:42 +04:00
|
|
|
func (entry *Entry) Warningf(format string, args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Warnf(format, args...)
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Error {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Error(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Fatal {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Fatal(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Panicf(format string, args ...interface{}) {
|
2014-04-23 20:54:53 +04:00
|
|
|
if entry.Logger.Level >= Panic {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Panic(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Entry Println family functions
|
|
|
|
|
|
|
|
func (entry *Entry) Debugln(args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Debug(args...)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Infoln(args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Info(args...)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Println(args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Info(args...)
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Warnln(args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Warn(args...)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *Entry) Warningln(args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Warn(args...)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
func (entry *Entry) Errorln(args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Error(args...)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
func (entry *Entry) Fatalln(args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Fatal(args...)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
func (entry *Entry) Panicln(args ...interface{}) {
|
2014-03-19 20:02:00 +04:00
|
|
|
entry.Panic(args...)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|