Merge pull request #2 from burke/initial

This is sort of what I want the log output to look like.
This commit is contained in:
Simon Eskildsen 2014-03-06 09:40:59 -05:00
commit 494c757ca7
1 changed files with 34 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"sort"
"strings" "strings"
"time" "time"
@ -19,6 +20,16 @@ type Entry struct {
Data Fields 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 { func NewEntry(logger *Logger) *Entry {
return &Entry{ return &Entry{
logger: logger, logger: logger,
@ -40,19 +51,36 @@ func (entry *Entry) Reader() (*bytes.Buffer, error) {
} }
serialized = append(serialized, '\n') serialized = append(serialized, '\n')
} else { } else {
levelText := strings.ToUpper(entry.Data["level"].(string))
levelColor := 34
if levelText != "INFO" {
levelColor = 31
}
if ttyutils.IsTerminal(os.Stdout.Fd()) { 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: 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. // TODO: If this is a println, it'll do a newline and then closing quote.
for k, v := range entry.Data { keys := make([]string, 0)
serialized = append(serialized, []byte(fmt.Sprintf("%s='%s' ", k, v))...) 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[34m%s\x1b[0m=%v", k, v))...)
} }
if ttyutils.IsTerminal(os.Stdout.Fd()) { serialized = append(serialized, []byte(fmt.Sprintf("\x1b[%dm)\x1b[0m", levelColor))...)
serialized = append(serialized, []byte("\x1b[0m")...)
}
serialized = append(serialized, '\n') serialized = append(serialized, '\n')
} }