code cleanup

This commit is contained in:
Simon Eskildsen 2014-02-23 19:53:50 -05:00
parent 6c895096e8
commit 0bd36d372c
2 changed files with 11 additions and 22 deletions

View File

@ -61,14 +61,15 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
func (entry *Entry) log(level string, msg string) { func (entry *Entry) log(level string, msg string) {
// TODO: Is the default format output from String() the one we want? // 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 entry.Data["level"] = level
// TODO: Is this the best name? // TODO: Is this the best name?
entry.Data["msg"] = msg entry.Data["msg"] = msg
reader, err := entry.Reader() reader, err := entry.Reader()
if err != nil { 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 // 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()) panic(reader.String())
} else { } else {
entry.logger.mu.Lock() entry.logger.mu.Lock()
io.Copy(entry.logger.Out, reader) defer entry.logger.mu.Unlock()
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())
}
} }
} }

View File

@ -3,10 +3,7 @@ package logrus
import ( import (
"io" "io"
"os" "os"
"strings"
"sync" "sync"
"github.com/tobi/airbrake-go"
) )
type Logger struct { type Logger struct {
@ -15,30 +12,17 @@ type Logger struct {
} }
func New() *Logger { func New() *Logger {
environment := strings.ToLower(os.Getenv("ENV"))
if environment == "" {
environment = "development"
}
if airbrake.Environment == "" {
airbrake.Environment = environment
}
return &Logger{ return &Logger{
Out: os.Stdout, // Default to stdout, change it if you want. Out: os.Stdout, // Default to stdout, change it if you want.
} }
} }
func (logger *Logger) WithField(key string, value interface{}) *Entry { func (logger *Logger) WithField(key string, value interface{}) *Entry {
entry := NewEntry(logger) return NewEntry(logger).WithField(key, value)
entry.WithField(key, value)
return entry
} }
func (logger *Logger) WithFields(fields Fields) *Entry { func (logger *Logger) WithFields(fields Fields) *Entry {
entry := NewEntry(logger) return NewEntry(logger).WithFields(fields)
entry.WithFields(fields)
return entry
} }
// Entry Print family functions // Entry Print family functions