Add features from README
This commit is contained in:
@@ -2,17 +2,10 @@ package logrus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/burke/ttyutils"
|
||||
"github.com/tobi/airbrake-go"
|
||||
)
|
||||
|
||||
type Entry struct {
|
||||
@@ -33,64 +26,23 @@ func miniTS() int {
|
||||
func NewEntry(logger *Logger) *Entry {
|
||||
return &Entry{
|
||||
logger: logger,
|
||||
// Default is three fields, give a little extra room. Shouldn't hurt the
|
||||
// scale.
|
||||
// Default is three fields, give a little extra room
|
||||
Data: make(Fields, 5),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Other formats?
|
||||
func (entry *Entry) Reader() (*bytes.Buffer, error) {
|
||||
var serialized []byte
|
||||
var err error
|
||||
serialized, err := entry.logger.Formatter.Format(entry)
|
||||
return bytes.NewBuffer(serialized), err
|
||||
}
|
||||
|
||||
if Environment == "production" {
|
||||
serialized, err = json.Marshal(entry.Data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
|
||||
}
|
||||
serialized = append(serialized, '\n')
|
||||
} else {
|
||||
levelText := strings.ToUpper(entry.Data["level"].(string))[0:4]
|
||||
levelColor := 34
|
||||
|
||||
if entry.Data["level"] == "warning" {
|
||||
levelColor = 33
|
||||
} else if entry.Data["level"] == "fatal" ||
|
||||
entry.Data["level"] == "panic" {
|
||||
levelColor = 31
|
||||
}
|
||||
|
||||
if ttyutils.IsTerminal(os.Stdout.Fd()) {
|
||||
serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm%s\x1b[0m[%04d] %-45s ", levelColor, levelText, miniTS(), entry.Data["msg"]))...)
|
||||
}
|
||||
|
||||
// TODO: Pretty-print more by coloring when stdout is a tty
|
||||
// TODO: If this is a println, it'll do a newline and then closing quote.
|
||||
keys := make([]string, 0)
|
||||
for k, _ := range entry.Data {
|
||||
if k != "level" && k != "time" && k != "msg" {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(keys)
|
||||
first := true
|
||||
for _, k := range keys {
|
||||
v := entry.Data[k]
|
||||
if first {
|
||||
first = false
|
||||
} else {
|
||||
serialized = append(serialized, ' ')
|
||||
}
|
||||
serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm%s\x1b[0m=%v", levelColor, k, v))...)
|
||||
}
|
||||
|
||||
// serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm)\x1b[0m", levelColor))...)
|
||||
|
||||
serialized = append(serialized, '\n')
|
||||
func (entry *Entry) String() (string, error) {
|
||||
reader, err := entry.Reader()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return bytes.NewBuffer(serialized), nil
|
||||
return reader.String(), err
|
||||
}
|
||||
|
||||
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
||||
@@ -102,15 +54,12 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
|
||||
for key, value := range fields {
|
||||
entry.WithField(key, value)
|
||||
}
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
func (entry *Entry) log(level string, msg string) string {
|
||||
// TODO: Is the default format output from String() the one we want?
|
||||
entry.Data["time"] = time.Now().String()
|
||||
entry.Data["level"] = level
|
||||
// TODO: Is this the best name?
|
||||
entry.Data["msg"] = msg
|
||||
|
||||
reader, err := entry.Reader()
|
||||
@@ -118,25 +67,12 @@ func (entry *Entry) log(level string, msg string) string {
|
||||
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v", err)
|
||||
}
|
||||
|
||||
if Environment != "development" {
|
||||
// Send HTTP request in a goroutine in warning environment to not halt the
|
||||
// main thread. It's sent before logging due to panic.
|
||||
if level == "warning" {
|
||||
// TODO: new() should spawn an airbrake goroutine and this should send to
|
||||
// that channel. This prevent us from spawning hundreds of goroutines in a
|
||||
// hot code path generating a warning.
|
||||
go entry.airbrake(reader.String())
|
||||
} else if level == "fatal" || level == "panic" {
|
||||
entry.airbrake(reader.String())
|
||||
}
|
||||
}
|
||||
|
||||
entry.logger.mu.Lock()
|
||||
defer entry.logger.mu.Unlock()
|
||||
|
||||
_, err = io.Copy(entry.logger.Out, reader)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v", err)
|
||||
fmt.Fprintf(os.Stderr, "Failed to write to log, %v", err)
|
||||
}
|
||||
|
||||
return reader.String()
|
||||
@@ -145,30 +81,42 @@ func (entry *Entry) log(level string, msg string) string {
|
||||
func (entry *Entry) Debug(args ...interface{}) {
|
||||
if Level >= LevelDebug {
|
||||
entry.log("debug", fmt.Sprint(args...))
|
||||
entry.logger.Hooks.Fire(LevelDebug, entry)
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Info(args ...interface{}) {
|
||||
if Level >= LevelInfo {
|
||||
entry.log("info", fmt.Sprint(args...))
|
||||
entry.logger.Hooks.Fire(LevelInfo, entry)
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Print(args ...interface{}) {
|
||||
if Level >= LevelInfo {
|
||||
entry.log("info", fmt.Sprint(args...))
|
||||
entry.logger.Hooks.Fire(LevelInfo, entry)
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Warning(args ...interface{}) {
|
||||
if Level >= LevelWarning {
|
||||
func (entry *Entry) Warn(args ...interface{}) {
|
||||
if Level >= LevelWarn {
|
||||
entry.log("warning", fmt.Sprint(args...))
|
||||
entry.logger.Hooks.Fire(LevelWarn, entry)
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Error(args ...interface{}) {
|
||||
if Level >= LevelError {
|
||||
entry.log("error", fmt.Sprint(args...))
|
||||
entry.logger.Hooks.Fire(LevelError, entry)
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatal(args ...interface{}) {
|
||||
if Level >= LevelFatal {
|
||||
entry.log("fatal", fmt.Sprint(args...))
|
||||
entry.logger.Hooks.Fire(LevelFatal, entry)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -176,6 +124,7 @@ func (entry *Entry) Fatal(args ...interface{}) {
|
||||
func (entry *Entry) Panic(args ...interface{}) {
|
||||
if Level >= LevelPanic {
|
||||
msg := entry.log("panic", fmt.Sprint(args...))
|
||||
entry.logger.Hooks.Fire(LevelPanic, entry)
|
||||
panic(msg)
|
||||
}
|
||||
panic(fmt.Sprint(args...))
|
||||
@@ -195,8 +144,16 @@ func (entry *Entry) Printf(format string, args ...interface{}) {
|
||||
entry.Print(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
||||
entry.Warn(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Warningf(format string, args ...interface{}) {
|
||||
entry.Warning(fmt.Sprintf(format, args...))
|
||||
entry.Warn(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
||||
entry.Print(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
||||
@@ -210,35 +167,33 @@ func (entry *Entry) Panicf(format string, args ...interface{}) {
|
||||
// Entry Println family functions
|
||||
|
||||
func (entry *Entry) Debugln(args ...interface{}) {
|
||||
entry.Debug(fmt.Sprintln(args...))
|
||||
entry.Debug(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Infoln(args ...interface{}) {
|
||||
entry.Info(fmt.Sprintln(args...))
|
||||
entry.Info(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Println(args ...interface{}) {
|
||||
entry.Print(fmt.Sprintln(args...))
|
||||
entry.Print(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Warnln(args ...interface{}) {
|
||||
entry.Warn(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Warningln(args ...interface{}) {
|
||||
entry.Warning(fmt.Sprintln(args...))
|
||||
entry.Warn(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Errorln(args ...interface{}) {
|
||||
entry.Error(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatalln(args ...interface{}) {
|
||||
entry.Fatal(fmt.Sprintln(args...))
|
||||
entry.Fatal(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) Panicln(args ...interface{}) {
|
||||
entry.Panic(fmt.Sprintln(args...))
|
||||
}
|
||||
|
||||
func (entry *Entry) airbrake(exception string) {
|
||||
err := airbrake.Notify(errors.New(exception))
|
||||
if err != nil {
|
||||
entry.logger.WithFields(Fields{
|
||||
"source": "airbrake",
|
||||
"endpoint": airbrake.Endpoint,
|
||||
}).Infof("Failed to send exception to Airbrake")
|
||||
}
|
||||
entry.Panic(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user