fastpastebin/vendor/github.com/rs/zerolog/console.go

392 lines
8.5 KiB
Go
Raw Normal View History

2018-04-30 18:42:17 +05:00
package zerolog
import (
"bytes"
"encoding/json"
"fmt"
"io"
2019-03-07 07:56:50 +05:00
"os"
2018-04-30 18:42:17 +05:00
"sort"
"strconv"
"strings"
"sync"
2019-03-07 07:56:50 +05:00
"time"
2018-04-30 18:42:17 +05:00
)
const (
2019-03-07 07:56:50 +05:00
colorBlack = iota + 30
colorRed
colorGreen
colorYellow
colorBlue
colorMagenta
colorCyan
colorWhite
colorBold = 1
colorDarkGray = 90
2018-04-30 18:42:17 +05:00
)
2019-03-07 07:56:50 +05:00
var (
consoleBufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 100))
},
}
)
const (
consoleDefaultTimeFormat = time.Kitchen
)
2018-04-30 18:42:17 +05:00
2019-03-07 07:56:50 +05:00
// Formatter transforms the input into a formatted string.
type Formatter func(interface{}) string
// ConsoleWriter parses the JSON input and writes it in an
// (optionally) colorized, human-friendly format to Out.
2018-04-30 18:42:17 +05:00
type ConsoleWriter struct {
2019-03-07 07:56:50 +05:00
// Out is the output destination.
Out io.Writer
// NoColor disables the colorized output.
2018-04-30 18:42:17 +05:00
NoColor bool
2019-03-07 07:56:50 +05:00
// TimeFormat specifies the format for timestamp in output.
TimeFormat string
// PartsOrder defines the order of parts in output.
PartsOrder []string
FormatTimestamp Formatter
FormatLevel Formatter
FormatCaller Formatter
FormatMessage Formatter
FormatFieldName Formatter
FormatFieldValue Formatter
FormatErrFieldName Formatter
FormatErrFieldValue Formatter
}
// NewConsoleWriter creates and initializes a new ConsoleWriter.
func NewConsoleWriter(options ...func(w *ConsoleWriter)) ConsoleWriter {
w := ConsoleWriter{
Out: os.Stdout,
TimeFormat: consoleDefaultTimeFormat,
PartsOrder: consoleDefaultPartsOrder(),
}
for _, opt := range options {
opt(&w)
}
return w
2018-04-30 18:42:17 +05:00
}
2019-03-07 07:56:50 +05:00
// Write transforms the JSON input with formatters and appends to w.Out.
2018-04-30 18:42:17 +05:00
func (w ConsoleWriter) Write(p []byte) (n int, err error) {
2019-03-07 07:56:50 +05:00
if w.PartsOrder == nil {
w.PartsOrder = consoleDefaultPartsOrder()
}
var buf = consoleBufPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
consoleBufPool.Put(buf)
}()
var evt map[string]interface{}
2018-04-30 18:42:17 +05:00
p = decodeIfBinaryToBytes(p)
2019-03-07 07:56:50 +05:00
d := json.NewDecoder(bytes.NewReader(p))
d.UseNumber()
err = d.Decode(&evt)
if err != nil {
return n, fmt.Errorf("cannot decode event: %s", err)
}
for _, p := range w.PartsOrder {
w.writePart(buf, evt, p)
}
w.writeFields(evt, buf)
err = buf.WriteByte('\n')
2018-04-30 18:42:17 +05:00
if err != nil {
2019-03-07 07:56:50 +05:00
return n, err
}
_, err = buf.WriteTo(w.Out)
return len(p), err
}
// writeFields appends formatted key-value pairs to buf.
func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer) {
var fields = make([]string, 0, len(evt))
for field := range evt {
2018-04-30 18:42:17 +05:00
switch field {
2019-03-07 07:56:50 +05:00
case LevelFieldName, TimestampFieldName, MessageFieldName, CallerFieldName:
2018-04-30 18:42:17 +05:00
continue
}
fields = append(fields, field)
}
sort.Strings(fields)
2019-03-07 07:56:50 +05:00
if len(fields) > 0 {
buf.WriteByte(' ')
}
// Move the "error" field to the front
ei := sort.Search(len(fields), func(i int) bool { return fields[i] >= ErrorFieldName })
if ei < len(fields) && fields[ei] == ErrorFieldName {
fields[ei] = ""
fields = append([]string{ErrorFieldName}, fields...)
var xfields = make([]string, 0, len(fields))
for _, field := range fields {
if field == "" { // Skip empty fields
continue
}
xfields = append(xfields, field)
}
fields = xfields
}
for i, field := range fields {
var fn Formatter
var fv Formatter
if field == ErrorFieldName {
if w.FormatErrFieldName == nil {
fn = consoleDefaultFormatErrFieldName(w.NoColor)
} else {
fn = w.FormatErrFieldName
}
if w.FormatErrFieldValue == nil {
fv = consoleDefaultFormatErrFieldValue(w.NoColor)
} else {
fv = w.FormatErrFieldValue
}
} else {
if w.FormatFieldName == nil {
fn = consoleDefaultFormatFieldName(w.NoColor)
} else {
fn = w.FormatFieldName
}
if w.FormatFieldValue == nil {
fv = consoleDefaultFormatFieldValue
} else {
fv = w.FormatFieldValue
}
}
buf.WriteString(fn(field))
switch fValue := evt[field].(type) {
2018-04-30 18:42:17 +05:00
case string:
2019-03-07 07:56:50 +05:00
if needsQuote(fValue) {
buf.WriteString(fv(strconv.Quote(fValue)))
2018-04-30 18:42:17 +05:00
} else {
2019-03-07 07:56:50 +05:00
buf.WriteString(fv(fValue))
2018-04-30 18:42:17 +05:00
}
2019-03-07 07:56:50 +05:00
case json.Number:
buf.WriteString(fv(fValue))
2018-04-30 18:42:17 +05:00
default:
2019-03-07 07:56:50 +05:00
b, err := json.Marshal(fValue)
2018-04-30 18:42:17 +05:00
if err != nil {
2019-03-07 07:56:50 +05:00
fmt.Fprintf(buf, colorize("[error: %v]", colorRed, w.NoColor), err)
2018-04-30 18:42:17 +05:00
} else {
2019-03-07 07:56:50 +05:00
fmt.Fprint(buf, fv(b))
2018-04-30 18:42:17 +05:00
}
}
2019-03-07 07:56:50 +05:00
if i < len(fields)-1 { // Skip space for last field
buf.WriteByte(' ')
}
2018-04-30 18:42:17 +05:00
}
}
2019-03-07 07:56:50 +05:00
// writePart appends a formatted part to buf.
func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) {
var f Formatter
switch p {
case LevelFieldName:
if w.FormatLevel == nil {
f = consoleDefaultFormatLevel(w.NoColor)
} else {
f = w.FormatLevel
}
case TimestampFieldName:
if w.FormatTimestamp == nil {
f = consoleDefaultFormatTimestamp(w.TimeFormat, w.NoColor)
} else {
f = w.FormatTimestamp
}
case MessageFieldName:
if w.FormatMessage == nil {
f = consoleDefaultFormatMessage
} else {
f = w.FormatMessage
}
case CallerFieldName:
if w.FormatCaller == nil {
f = consoleDefaultFormatCaller(w.NoColor)
} else {
f = w.FormatCaller
}
2018-04-30 18:42:17 +05:00
default:
2019-03-07 07:56:50 +05:00
if w.FormatFieldValue == nil {
f = consoleDefaultFormatFieldValue
} else {
f = w.FormatFieldValue
}
}
var s = f(evt[p])
if len(s) > 0 {
buf.WriteString(s)
if p != w.PartsOrder[len(w.PartsOrder)-1] { // Skip space for last part
buf.WriteByte(' ')
}
2018-04-30 18:42:17 +05:00
}
}
2019-03-07 07:56:50 +05:00
// needsQuote returns true when the string s should be quoted in output.
2018-04-30 18:42:17 +05:00
func needsQuote(s string) bool {
for i := range s {
if s[i] < 0x20 || s[i] > 0x7e || s[i] == ' ' || s[i] == '\\' || s[i] == '"' {
return true
}
}
return false
}
2019-03-07 07:56:50 +05:00
// colorize returns the string s wrapped in ANSI code c, unless disabled is true.
func colorize(s interface{}, c int, disabled bool) string {
if disabled {
return fmt.Sprintf("%s", s)
}
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s)
}
// ----- DEFAULT FORMATTERS ---------------------------------------------------
func consoleDefaultPartsOrder() []string {
return []string{
TimestampFieldName,
LevelFieldName,
CallerFieldName,
MessageFieldName,
}
}
func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter {
if timeFormat == "" {
timeFormat = consoleDefaultTimeFormat
}
return func(i interface{}) string {
t := "<nil>"
switch tt := i.(type) {
case string:
ts, err := time.Parse(TimeFieldFormat, tt)
if err != nil {
t = tt
} else {
t = ts.Format(timeFormat)
}
case json.Number:
2019-10-13 13:55:38 +05:00
i, err := tt.Int64()
if err != nil {
t = tt.String()
} else {
var sec, nsec int64 = i, 0
if TimeFieldFormat == TimeFormatUnixMs {
nsec = int64(time.Duration(i) * time.Millisecond)
sec = 0
}
ts := time.Unix(sec, nsec).UTC()
t = ts.Format(timeFormat)
}
2019-03-07 07:56:50 +05:00
}
return colorize(t, colorDarkGray, noColor)
}
}
func consoleDefaultFormatLevel(noColor bool) Formatter {
return func(i interface{}) string {
var l string
if ll, ok := i.(string); ok {
switch ll {
case "debug":
l = colorize("DBG", colorYellow, noColor)
case "info":
l = colorize("INF", colorGreen, noColor)
case "warn":
l = colorize("WRN", colorRed, noColor)
case "error":
l = colorize(colorize("ERR", colorRed, noColor), colorBold, noColor)
case "fatal":
l = colorize(colorize("FTL", colorRed, noColor), colorBold, noColor)
case "panic":
l = colorize(colorize("PNC", colorRed, noColor), colorBold, noColor)
default:
l = colorize("???", colorBold, noColor)
}
} else {
2019-10-13 13:55:38 +05:00
if i == nil {
l = colorize("???", colorBold, noColor)
} else {
l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3]
}
2019-03-07 07:56:50 +05:00
}
return l
}
}
func consoleDefaultFormatCaller(noColor bool) Formatter {
return func(i interface{}) string {
var c string
if cc, ok := i.(string); ok {
c = cc
}
if len(c) > 0 {
cwd, err := os.Getwd()
if err == nil {
c = strings.TrimPrefix(c, cwd)
c = strings.TrimPrefix(c, "/")
}
c = colorize(c, colorBold, noColor) + colorize(" >", colorCyan, noColor)
}
return c
}
}
func consoleDefaultFormatMessage(i interface{}) string {
2019-10-13 13:55:38 +05:00
if i == nil {
return ""
}
2019-03-07 07:56:50 +05:00
return fmt.Sprintf("%s", i)
}
func consoleDefaultFormatFieldName(noColor bool) Formatter {
return func(i interface{}) string {
return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor)
}
}
func consoleDefaultFormatFieldValue(i interface{}) string {
return fmt.Sprintf("%s", i)
}
func consoleDefaultFormatErrFieldName(noColor bool) Formatter {
return func(i interface{}) string {
return colorize(fmt.Sprintf("%s=", i), colorRed, noColor)
}
}
func consoleDefaultFormatErrFieldValue(noColor bool) Formatter {
return func(i interface{}) string {
return colorize(fmt.Sprintf("%s", i), colorRed, noColor)
}
}