2020-05-29 05:07:53 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/apex/log/handlers/cli"
|
|
|
|
color2 "github.com/fatih/color"
|
|
|
|
"github.com/mattn/go-colorable"
|
2020-11-28 23:57:10 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-05-29 05:07:53 +00:00
|
|
|
"io"
|
2020-12-06 22:58:35 +00:00
|
|
|
"math"
|
2020-05-29 05:07:53 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-08-04 03:35:48 +00:00
|
|
|
var Default = New(os.Stderr, true)
|
2020-05-29 05:07:53 +00:00
|
|
|
|
|
|
|
var bold = color2.New(color2.Bold)
|
2020-12-06 22:58:35 +00:00
|
|
|
var boldred = bold.Add(color2.FgRed)
|
2020-05-29 05:07:53 +00:00
|
|
|
|
|
|
|
var Strings = [...]string{
|
|
|
|
log.DebugLevel: "DEBUG",
|
|
|
|
log.InfoLevel: " INFO",
|
|
|
|
log.WarnLevel: " WARN",
|
|
|
|
log.ErrorLevel: "ERROR",
|
|
|
|
log.FatalLevel: "FATAL",
|
|
|
|
}
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
Writer io.Writer
|
|
|
|
Padding int
|
|
|
|
}
|
|
|
|
|
2020-08-04 03:35:48 +00:00
|
|
|
func New(w io.Writer, useColors bool) *Handler {
|
2020-05-29 05:07:53 +00:00
|
|
|
if f, ok := w.(*os.File); ok {
|
2020-08-04 03:35:48 +00:00
|
|
|
if useColors {
|
|
|
|
return &Handler{Writer: colorable.NewColorable(f), Padding: 2}
|
|
|
|
}
|
2020-05-29 05:07:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 03:35:48 +00:00
|
|
|
return &Handler{Writer: colorable.NewNonColorable(w), Padding: 2}
|
2020-05-29 05:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type tracer interface {
|
|
|
|
StackTrace() errors.StackTrace
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleLog implements log.Handler.
|
|
|
|
func (h *Handler) HandleLog(e *log.Entry) error {
|
|
|
|
color := cli.Colors[e.Level]
|
|
|
|
level := Strings[e.Level]
|
|
|
|
names := e.Fields.Names()
|
|
|
|
|
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
|
|
|
color.Fprintf(h.Writer, "%s: [%s] %-25s", bold.Sprintf("%*s", h.Padding+1, level), time.Now().Format(time.StampMilli), e.Message)
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
if name == "source" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintf(h.Writer, " %s=%v", color.Sprint(name), e.Fields.Get(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(h.Writer)
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
if name != "error" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err, ok := e.Fields.Get("error").(error); ok {
|
2020-12-06 22:55:35 +00:00
|
|
|
if e, ok := errors.Cause(err).(tracer); ok {
|
|
|
|
st := e.StackTrace()
|
2020-12-06 22:58:35 +00:00
|
|
|
l := math.Min(float64(len(st)), 10)
|
|
|
|
fmt.Fprintf(h.Writer, "\n%s%+v\n\n", boldred.Sprintf("Stacktrace:"), st[0:int(l)])
|
2020-12-06 22:55:35 +00:00
|
|
|
} else {
|
2020-12-06 22:58:35 +00:00
|
|
|
fmt.Fprintf(h.Writer, "\n%s\n%+v\n\n", boldred.Sprintf("Stacktrace:"), err)
|
2020-12-06 22:55:35 +00:00
|
|
|
}
|
2020-07-11 19:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 22:55:35 +00:00
|
|
|
return nil
|
2020-07-11 19:53:54 +00:00
|
|
|
}
|