|
|
@@ -94,35 +94,47 @@ func (entry Entry) log(level Level, msg string) {
|
|
|
entry.Level = level
|
|
|
entry.Message = msg
|
|
|
|
|
|
- if err := entry.Logger.Hooks.Fire(level, &entry); err != nil {
|
|
|
- entry.Logger.mu.Lock()
|
|
|
- fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
|
|
- entry.Logger.mu.Unlock()
|
|
|
- }
|
|
|
+ entry.fireHooks()
|
|
|
+
|
|
|
buffer = bufferPool.Get().(*bytes.Buffer)
|
|
|
buffer.Reset()
|
|
|
defer bufferPool.Put(buffer)
|
|
|
entry.Buffer = buffer
|
|
|
- serialized, err := entry.Logger.Formatter.Format(&entry)
|
|
|
+
|
|
|
+ entry.write()
|
|
|
+
|
|
|
entry.Buffer = nil
|
|
|
+
|
|
|
+ // To avoid Entry#log() returning a value that only would make sense for
|
|
|
+ // panic() to use in Entry#Panic(), we avoid the allocation by checking
|
|
|
+ // directly here.
|
|
|
+ if level <= PanicLevel {
|
|
|
+ panic(&entry)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// This function is not declared with a pointer value because otherwise
|
|
|
+// race conditions will occur when using multiple goroutines
|
|
|
+func (entry Entry) fireHooks() {
|
|
|
+ entry.Logger.mu.Lock()
|
|
|
+ defer entry.Logger.mu.Unlock()
|
|
|
+ err := entry.Logger.Hooks.Fire(entry.Level, &entry)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (entry *Entry) write() {
|
|
|
+ serialized, err := entry.Logger.Formatter.Format(entry)
|
|
|
+ entry.Logger.mu.Lock()
|
|
|
+ defer entry.Logger.mu.Unlock()
|
|
|
if err != nil {
|
|
|
- entry.Logger.mu.Lock()
|
|
|
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
|
|
|
- entry.Logger.mu.Unlock()
|
|
|
} else {
|
|
|
- entry.Logger.mu.Lock()
|
|
|
_, err = entry.Logger.Out.Write(serialized)
|
|
|
if err != nil {
|
|
|
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
|
|
|
}
|
|
|
- entry.Logger.mu.Unlock()
|
|
|
- }
|
|
|
-
|
|
|
- // To avoid Entry#log() returning a value that only would make sense for
|
|
|
- // panic() to use in Entry#Panic(), we avoid the allocation by checking
|
|
|
- // directly here.
|
|
|
- if level <= PanicLevel {
|
|
|
- panic(&entry)
|
|
|
}
|
|
|
}
|
|
|
|