forked from uber-go/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up hooks and logger config (uber-go#102)
* Add a constructor for no-op fields Add a constructor so that users can also create no-op fields. I considered calling this `Nop`, but `zap.Nop()` looks like it should return a logger instead of a Field. * Clean up hooks and logger boilerplate Reduce boilerplate by introducing an Entry struct and pulling common logger elements into a Config struct (that can be shared between Logger implementations). Along the way, start logging timestamps as floating-point seconds since epoch. * Address CR feedback * Add tests, clarify comments
- Loading branch information
1 parent
02d453c
commit f9a34fd
Showing
18 changed files
with
395 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2016 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package zap | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
_timeNow = time.Now // for tests | ||
_entryPool = sync.Pool{New: func() interface{} { return &Entry{} }} | ||
) | ||
|
||
// An Entry represents a complete log message. The entry's structured context | ||
// is already serialized, but the log level, time, and message are available | ||
// for inspection and modification. | ||
// | ||
// Entries are pooled, so any functions that accept them must be careful not to | ||
// retain references to them. | ||
type Entry struct { | ||
Level Level | ||
Time time.Time | ||
Message string | ||
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 { | ||
return e.enc | ||
} | ||
|
||
func (e *Entry) free() { | ||
_entryPool.Put(e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package zap | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func stubNow(afterEpoch time.Duration) func() { | ||
prev := _timeNow | ||
t := time.Unix(0, int64(afterEpoch)) | ||
_timeNow = func() time.Time { return t } | ||
return func() { _timeNow = prev } | ||
} | ||
|
||
func TestNewEntry(t *testing.T) { | ||
defer stubNow(0)() | ||
e := newEntry(DebugLevel, "hello", nil) | ||
assert.Equal(t, DebugLevel, e.Level, "Unexpected log level.") | ||
assert.Equal(t, time.Unix(0, 0).UTC(), e.Time, "Unexpected time.") | ||
assert.Nil(t, e.Fields(), "Unexpected fields.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.