Code review fixes

This commit is contained in:
Simon Eskildsen 2014-02-24 06:34:12 -05:00
parent b6f53cb91b
commit 11dbaff352
2 changed files with 17 additions and 16 deletions

View File

@ -27,8 +27,9 @@ func NewEntry(logger *Logger) *Entry {
} }
// TODO: Other formats? // TODO: Other formats?
func (entry *Entry) Reader() (read *bytes.Buffer, err error) { func (entry *Entry) Reader() (*bytes.Buffer, error) {
var serialized []byte var serialized []byte
var err error
if Environment == "production" { if Environment == "production" {
serialized, err = json.Marshal(entry.Data) serialized, err = json.Marshal(entry.Data)
@ -38,7 +39,7 @@ func (entry *Entry) Reader() (read *bytes.Buffer, err error) {
} }
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
} }
serialized = append(serialized, '\n') serialized = append(serialized, '\n')
@ -59,7 +60,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
return entry return entry
} }
func (entry *Entry) log(level string, msg string) { func (entry *Entry) log(level string, msg string) 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["time"] = time.Now().String() entry.Data["time"] = time.Now().String()
entry.Data["level"] = level entry.Data["level"] = level
@ -69,7 +70,7 @@ func (entry *Entry) log(level string, msg string) {
reader, err := entry.Reader() reader, err := entry.Reader()
if err != nil { if err != nil {
// TODO: Panic? // TODO: Panic?
entry.logger.Panicln("Failed to marshal JSON: ", err.Error()) entry.logger.Panicf("Failed to obtain reader, %v", err)
} }
// 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
@ -83,17 +84,16 @@ func (entry *Entry) log(level string, msg string) {
entry.airbrake(reader.String()) entry.airbrake(reader.String())
} }
if level == "panic" { entry.logger.mu.Lock()
panic(reader.String()) defer entry.logger.mu.Unlock()
} else {
entry.logger.mu.Lock() _, err = io.Copy(entry.logger.Out, reader)
defer entry.logger.mu.Unlock() // TODO: Panic?
_, err := io.Copy(entry.logger.Out, reader) if err != nil {
// TODO: Panic? entry.logger.Panicln("Failed to log message, %v", err)
if err != nil {
entry.logger.Panicln("Failed to log message: ", err.Error())
}
} }
return reader.String()
} }
func (entry *Entry) Debug(args ...interface{}) { func (entry *Entry) Debug(args ...interface{}) {
@ -129,8 +129,10 @@ func (entry *Entry) Fatal(args ...interface{}) {
func (entry *Entry) Panic(args ...interface{}) { func (entry *Entry) Panic(args ...interface{}) {
if Level >= LevelPanic { if Level >= LevelPanic {
entry.log("panic", fmt.Sprint(args...)) msg = entry.log("panic", fmt.Sprint(args...))
panic(msg)
} }
panic(fmt.Sprint(args...))
} }
// Entry Printf family functions // Entry Printf family functions

View File

@ -25,7 +25,6 @@ func (logger *Logger) WithFields(fields Fields) *Entry {
return NewEntry(logger).WithFields(fields) return NewEntry(logger).WithFields(fields)
} }
// Entry Print family functions
// Logger Printf family functions // Logger Printf family functions
func (logger *Logger) Debugf(format string, args ...interface{}) { func (logger *Logger) Debugf(format string, args ...interface{}) {