Merge pull request #59 from aybabtme/avoid-syscalls-at-each-logentry

text-formatter: assume that IsTerminal doesn't change during execution
This commit is contained in:
Simon Eskildsen 2014-09-16 17:24:56 -04:00
commit 89b6d460f0
2 changed files with 10 additions and 5 deletions

View File

@ -28,8 +28,6 @@ type Entry struct {
Message string Message string
} }
var baseTimestamp time.Time
func NewEntry(logger *Logger) *Entry { func NewEntry(logger *Logger) *Entry {
return &Entry{ return &Entry{
Logger: logger, Logger: logger,

View File

@ -16,8 +16,14 @@ const (
blue = 34 blue = 34
) )
var (
baseTimestamp time.Time
isTerminal bool
)
func init() { func init() {
baseTimestamp = time.Now() baseTimestamp = time.Now()
isTerminal = IsTerminal()
} }
func miniTS() int { func miniTS() int {
@ -31,11 +37,12 @@ type TextFormatter struct {
} }
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
prefixFieldClashes(entry) prefixFieldClashes(entry)
if (f.ForceColors || IsTerminal()) && !f.DisableColors { if (f.ForceColors || isTerminal) && !f.DisableColors {
levelText := strings.ToUpper(entry.Data["level"].(string))[0:4] levelText := strings.ToUpper(entry.Data["level"].(string))[0:4]
levelColor := blue levelColor := blue
@ -50,8 +57,8 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Data["msg"]) fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Data["msg"])
keys := make([]string, 0) var keys []string
for k, _ := range entry.Data { for k := range entry.Data {
if k != "level" && k != "time" && k != "msg" { if k != "level" && k != "time" && k != "msg" {
keys = append(keys, k) keys = append(keys, k)
} }