From 0bd36d372c159acc5f9eba775d7c1932cf8bc638 Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Sun, 23 Feb 2014 19:53:50 -0500 Subject: [PATCH] code cleanup --- entry.go | 13 +++++++++---- logger.go | 20 ++------------------ 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/entry.go b/entry.go index a492078..5e349a3 100644 --- a/entry.go +++ b/entry.go @@ -61,14 +61,15 @@ func (entry *Entry) WithFields(fields Fields) *Entry { func (entry *Entry) log(level string, msg string) { // TODO: Is the default format output from String() the one we want? - entry.Data["timestamp"] = time.Now().String() + entry.Data["time"] = time.Now().String() entry.Data["level"] = level // TODO: Is this the best name? entry.Data["msg"] = msg reader, err := entry.Reader() if err != nil { - entry.logger.Panicln("Failed to marshal JSON ", err.Error()) + // TODO: Panic? + entry.logger.Panicln("Failed to marshal JSON: ", err.Error()) } // Send HTTP request in a goroutine in warning environment to not halt the @@ -86,8 +87,12 @@ func (entry *Entry) log(level string, msg string) { panic(reader.String()) } else { entry.logger.mu.Lock() - io.Copy(entry.logger.Out, reader) - entry.logger.mu.Unlock() + defer entry.logger.mu.Unlock() + _, err := io.Copy(entry.logger.Out, reader) + // TODO: Panic? + if err != nil { + entry.logger.Panicln("Failed to log message: ", err.Error()) + } } } diff --git a/logger.go b/logger.go index cf3154d..6a9ce5c 100644 --- a/logger.go +++ b/logger.go @@ -3,10 +3,7 @@ package logrus import ( "io" "os" - "strings" "sync" - - "github.com/tobi/airbrake-go" ) type Logger struct { @@ -15,30 +12,17 @@ type Logger struct { } func New() *Logger { - environment := strings.ToLower(os.Getenv("ENV")) - if environment == "" { - environment = "development" - } - - if airbrake.Environment == "" { - airbrake.Environment = environment - } - return &Logger{ Out: os.Stdout, // Default to stdout, change it if you want. } } func (logger *Logger) WithField(key string, value interface{}) *Entry { - entry := NewEntry(logger) - entry.WithField(key, value) - return entry + return NewEntry(logger).WithField(key, value) } func (logger *Logger) WithFields(fields Fields) *Entry { - entry := NewEntry(logger) - entry.WithFields(fields) - return entry + return NewEntry(logger).WithFields(fields) } // Entry Print family functions