This commit is contained in:
Burke Libbey 2014-03-04 11:38:21 -05:00
parent 3ef01c6291
commit ce9bacf7b3
1 changed files with 28 additions and 6 deletions

View File

@ -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')
}