-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
* Shift hook running into Meta * Firm up a convenience Meta.Encode method * Unify time.Now calling, drop test stub
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,15 +20,7 @@ | |
|
||
package zap | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
_timeNow = time.Now // for tests | ||
_entryPool = sync.Pool{New: func() interface{} { return &Entry{} }} | ||
) | ||
import "time" | ||
|
||
// An Entry represents a complete log message. The entry's structured context | ||
// is already serialized, but the log level, time, and message are available | ||
|
@@ -43,20 +35,7 @@ type Entry struct { | |
enc Encoder | ||
} | ||
|
||
func newEntry(lvl Level, msg string, enc Encoder) *Entry { | ||
e := _entryPool.Get().(*Entry) | ||
e.Level = lvl | ||
e.Message = msg | ||
e.Time = _timeNow().UTC() | ||
e.enc = enc | ||
return e | ||
} | ||
|
||
// Fields returns a mutable reference to the entry's accumulated context. | ||
func (e *Entry) Fields() KeyValue { | ||
func (e Entry) Fields() KeyValue { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jcorbin
Author
Contributor
|
||
return e.enc | ||
} | ||
|
||
func (e *Entry) free() { | ||
_entryPool.Put(e) | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,18 @@ package zap | |
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var _entryPool = sync.Pool{ | ||
New: func() interface{} { | ||
return &Entry{} | ||
}, | ||
} | ||
|
||
// Meta is implementation-agnostic state management for Loggers. Most Logger | ||
// implementations can reduce the required boilerplate by embedding a Meta. | ||
// | ||
|
@@ -83,6 +92,30 @@ func (m Meta) Check(log Logger, lvl Level, msg string) *CheckedMessage { | |
// ErrorOutput. This method should only be used to report internal logger | ||
// problems and should not be used to report user-caused problems. | ||
func (m Meta) InternalError(cause string, err error) { | ||
fmt.Fprintf(m.ErrorOutput, "%v %s error: %v\n", _timeNow().UTC(), cause, err) | ||
fmt.Fprintf(m.ErrorOutput, "%v %s error: %v\n", time.Now().UTC(), cause, err) | ||
m.ErrorOutput.Sync() | ||
} | ||
|
||
// Encode runs any Hook functions and then writes an encoded log entry to the | ||
// given io.Writer, returning any error. | ||
func (m Meta) Encode(w io.Writer, t time.Time, lvl Level, msg string, fields []Field) error { | ||
enc := m.Encoder.Clone() | ||
addFields(enc, fields) | ||
if len(m.Hooks) >= 0 { | ||
entry := _entryPool.Get().(*Entry) | ||
entry.Level = lvl | ||
entry.Message = msg | ||
entry.Time = t | ||
entry.enc = enc | ||
for _, hook := range m.Hooks { | ||
if err := hook(entry); err != nil { | ||
m.InternalError("hook", err) | ||
} | ||
} | ||
msg, enc = entry.Message, entry.enc | ||
This comment has been minimized.
Sorry, something went wrong.
prashantv
Collaborator
|
||
_entryPool.Put(entry) | ||
} | ||
err := enc.WriteEntry(w, msg, lvl, t) | ||
enc.Free() | ||
return err | ||
} |
Let's revert this change back to using the pointer.
Entry
is not a tiny struct (it's 64 bytes) so ideally we avoid copying that when callingFields()
. Guessing this was an artifact of the previous version where we tries to use values everywhere.