entry: move hook firing to entry.log

This commit is contained in:
Simon Eskildsen 2014-03-12 10:10:22 -04:00
parent cd5805716d
commit 1e8a799618
2 changed files with 11 additions and 13 deletions

View File

@ -187,4 +187,3 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
* Default fields for an instance and inheritance * Default fields for an instance and inheritance
* Default available hooks (airbrake, statsd, dump cores) * Default available hooks (airbrake, statsd, dump cores)
* Revisit TextFormatter * Revisit TextFormatter
* Hook error logging

View File

@ -57,7 +57,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
return entry return entry
} }
func (entry *Entry) log(level string, msg string) string { func (entry *Entry) log(level string, levelInt Level, msg string) string {
entry.Data["time"] = time.Now().String() entry.Data["time"] = time.Now().String()
entry.Data["level"] = level entry.Data["level"] = level
entry.Data["msg"] = msg entry.Data["msg"] = msg
@ -67,6 +67,10 @@ func (entry *Entry) log(level string, msg string) string {
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v", err) fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v", err)
} }
if err := entry.logger.Hooks.Fire(levelInt, entry); err != nil {
fmt.Fprintf(os.Stderr, "Failed to fire hook", err)
}
entry.logger.mu.Lock() entry.logger.mu.Lock()
defer entry.logger.mu.Unlock() defer entry.logger.mu.Unlock()
@ -80,7 +84,7 @@ func (entry *Entry) log(level string, msg string) string {
func (entry *Entry) Debug(args ...interface{}) { func (entry *Entry) Debug(args ...interface{}) {
if entry.logger.Level >= Debug { if entry.logger.Level >= Debug {
entry.log("debug", fmt.Sprint(args...)) entry.log("debug", Debug, fmt.Sprint(args...))
entry.logger.Hooks.Fire(Debug, entry) entry.logger.Hooks.Fire(Debug, entry)
} }
} }
@ -91,37 +95,32 @@ func (entry *Entry) Print(args ...interface{}) {
func (entry *Entry) Info(args ...interface{}) { func (entry *Entry) Info(args ...interface{}) {
if entry.logger.Level >= Info { if entry.logger.Level >= Info {
entry.log("info", fmt.Sprint(args...)) entry.log("info", Info, fmt.Sprint(args...))
entry.logger.Hooks.Fire(Info, entry)
} }
} }
func (entry *Entry) Warn(args ...interface{}) { func (entry *Entry) Warn(args ...interface{}) {
if entry.logger.Level >= Warn { if entry.logger.Level >= Warn {
entry.log("warning", fmt.Sprint(args...)) entry.log("warning", Warn, fmt.Sprint(args...))
entry.logger.Hooks.Fire(Warn, entry)
} }
} }
func (entry *Entry) Error(args ...interface{}) { func (entry *Entry) Error(args ...interface{}) {
if entry.logger.Level >= Error { if entry.logger.Level >= Error {
entry.log("error", fmt.Sprint(args...)) entry.log("error", Error, fmt.Sprint(args...))
entry.logger.Hooks.Fire(Error, entry)
} }
} }
func (entry *Entry) Fatal(args ...interface{}) { func (entry *Entry) Fatal(args ...interface{}) {
if entry.logger.Level >= Fatal { if entry.logger.Level >= Fatal {
entry.log("fatal", fmt.Sprint(args...)) entry.log("fatal", Fatal, fmt.Sprint(args...))
entry.logger.Hooks.Fire(Fatal, entry)
} }
os.Exit(1) os.Exit(1)
} }
func (entry *Entry) Panic(args ...interface{}) { func (entry *Entry) Panic(args ...interface{}) {
if entry.logger.Level >= Panic { if entry.logger.Level >= Panic {
msg := entry.log("panic", fmt.Sprint(args...)) msg := entry.log("panic", Panic, fmt.Sprint(args...))
entry.logger.Hooks.Fire(Panic, entry)
panic(msg) panic(msg)
} }
panic(fmt.Sprint(args...)) panic(fmt.Sprint(args...))