mirror of https://github.com/sirupsen/logrus.git
Merge branch 'moriyoshi/refix-707' of git://github.com/moriyoshi/logrus into fix_firehooks
This commit is contained in:
commit
3e01752db0
6
entry.go
6
entry.go
|
@ -127,12 +127,10 @@ func (entry Entry) log(level Level, msg string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is not declared with a pointer value because otherwise
|
func (entry *Entry) fireHooks() {
|
||||||
// race conditions will occur when using multiple goroutines
|
|
||||||
func (entry Entry) fireHooks() {
|
|
||||||
entry.Logger.mu.Lock()
|
entry.Logger.mu.Lock()
|
||||||
defer entry.Logger.mu.Unlock()
|
defer entry.Logger.mu.Unlock()
|
||||||
err := entry.Logger.Hooks.Fire(entry.Level, &entry)
|
err := entry.Logger.Hooks.Fire(entry.Level, entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ type Hook struct {
|
||||||
// Entries is an array of all entries that have been received by this hook.
|
// Entries is an array of all entries that have been received by this hook.
|
||||||
// For safe access, use the AllEntries() method, rather than reading this
|
// For safe access, use the AllEntries() method, rather than reading this
|
||||||
// value directly.
|
// value directly.
|
||||||
Entries []*logrus.Entry
|
Entries []logrus.Entry
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ func NewNullLogger() (*logrus.Logger, *Hook) {
|
||||||
func (t *Hook) Fire(e *logrus.Entry) error {
|
func (t *Hook) Fire(e *logrus.Entry) error {
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
t.Entries = append(t.Entries, e)
|
t.Entries = append(t.Entries, *e)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,9 +68,7 @@ func (t *Hook) LastEntry() *logrus.Entry {
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Make a copy, for safety
|
return &t.Entries[i]
|
||||||
e := *t.Entries[i]
|
|
||||||
return &e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AllEntries returns all entries that were logged.
|
// AllEntries returns all entries that were logged.
|
||||||
|
@ -79,10 +77,9 @@ func (t *Hook) AllEntries() []*logrus.Entry {
|
||||||
defer t.mu.RUnlock()
|
defer t.mu.RUnlock()
|
||||||
// Make a copy so the returned value won't race with future log requests
|
// Make a copy so the returned value won't race with future log requests
|
||||||
entries := make([]*logrus.Entry, len(t.Entries))
|
entries := make([]*logrus.Entry, len(t.Entries))
|
||||||
for i, entry := range t.Entries {
|
for i := 0; i < len(t.Entries); i++ {
|
||||||
// Make a copy, for safety
|
// Make a copy, for safety
|
||||||
e := *entry
|
entries[i] = &t.Entries[i]
|
||||||
entries[i] = &e
|
|
||||||
}
|
}
|
||||||
return entries
|
return entries
|
||||||
}
|
}
|
||||||
|
@ -91,5 +88,5 @@ func (t *Hook) AllEntries() []*logrus.Entry {
|
||||||
func (t *Hook) Reset() {
|
func (t *Hook) Reset() {
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
t.Entries = make([]*logrus.Entry, 0)
|
t.Entries = make([]logrus.Entry, 0)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue