From ce9bacf7b3f0e248e2fb45d3cdeee23d6aa0ff44 Mon Sep 17 00:00:00 2001 From: Burke Libbey Date: Tue, 4 Mar 2014 11:38:21 -0500 Subject: [PATCH 1/2] quickhax --- entry.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/entry.go b/entry.go index b326889..59572d6 100644 --- a/entry.go +++ b/entry.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "sort" "strings" "time" @@ -19,6 +20,16 @@ type Entry struct { Data Fields } +var baseTimestamp time.Time + +func init() { + baseTimestamp = time.Now() +} + +func miniTS() int { + return int(time.Since(baseTimestamp) / time.Second) +} + func NewEntry(logger *Logger) *Entry { return &Entry{ logger: logger, @@ -40,19 +51,30 @@ func (entry *Entry) Reader() (*bytes.Buffer, error) { } serialized = append(serialized, '\n') } else { + levelText := strings.ToUpper(entry.Data["level"].(string)) + levelColor := 34 + if levelText != "INFO" { + levelColor = 31 + } if ttyutils.IsTerminal(os.Stdout.Fd()) { - serialized = append(serialized, []byte(fmt.Sprintf("\x1b[34m%s: ", strings.ToUpper(entry.Data["level"].(string))))...) + serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm%s\x1b[0m[%04d] %-45s \x1b[%dm(\x1b[0m", levelColor, levelText, miniTS(), entry.Data["msg"], levelColor))...) } // 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. - for k, v := range entry.Data { - serialized = append(serialized, []byte(fmt.Sprintf("%s='%s' ", k, v))...) + keys := make([]string, 0) + for k, _ := range entry.Data { + if k != "level" && k != "time" && k != "msg" { + keys = append(keys, k) + } + } + sort.Strings(keys) + for _, k := range keys { + v := entry.Data[k] + serialized = append(serialized, []byte(fmt.Sprintf("\x1b[34m%s\x1b[0m='%s' ", k, v))...) } - if ttyutils.IsTerminal(os.Stdout.Fd()) { - serialized = append(serialized, []byte("\x1b[0m")...) - } + serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm)\x1b[0m", levelColor))...) serialized = append(serialized, '\n') } From e3978aa87cdcb3c46a7e146f7c042e07f2bf9f5e Mon Sep 17 00:00:00 2001 From: Burke Libbey Date: Tue, 4 Mar 2014 11:44:09 -0500 Subject: [PATCH 2/2] morehax --- entry.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/entry.go b/entry.go index 59572d6..b9e0b21 100644 --- a/entry.go +++ b/entry.go @@ -69,9 +69,15 @@ func (entry *Entry) Reader() (*bytes.Buffer, error) { } } sort.Strings(keys) + first := true for _, k := range keys { v := entry.Data[k] - serialized = append(serialized, []byte(fmt.Sprintf("\x1b[34m%s\x1b[0m='%s' ", k, v))...) + if first { + first = false + } else { + serialized = append(serialized, ' ') + } + serialized = append(serialized, []byte(fmt.Sprintf("\x1b[34m%s\x1b[0m=%v", k, v))...) } serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm)\x1b[0m", levelColor))...)