diff --git a/clock.go b/clock.go deleted file mode 100644 index 380d9f341..000000000 --- a/clock.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2020 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 ( - "time" - - "go.uber.org/zap/zapcore" -) - -// Clock is a source of time for logged entries. -type Clock = zapcore.Clock - -// systemClock implements default Clock that uses system time. -type systemClock struct{} - -var _systemClock Clock = systemClock{} - -func (systemClock) Now() time.Time { - return time.Now() -} - -func (systemClock) NewTicker(duration time.Duration) *time.Ticker { - return time.NewTicker(duration) -} diff --git a/clock_test.go b/clock_test.go index c46b62c0a..5b6b9dd8e 100644 --- a/clock_test.go +++ b/clock_test.go @@ -45,16 +45,3 @@ func TestWithClock(t *testing.T) { assert.Equal(t, date, logs.All()[0].Entry.Time, "Unexpected entry time.") }) } - -func TestSystemClockNewTicker(t *testing.T) { - want := 3 - - var n int - timer := _systemClock.NewTicker(time.Millisecond) - for range timer.C { - n++ - if n == want { - return - } - } -} diff --git a/logger.go b/logger.go index b8525966a..c6ab4b0ef 100644 --- a/logger.go +++ b/logger.go @@ -51,7 +51,7 @@ type Logger struct { callerSkip int - clock Clock + clock zapcore.Clock } // New constructs a new Logger from the provided zapcore.Core and Options. If @@ -72,7 +72,7 @@ func New(core zapcore.Core, options ...Option) *Logger { core: core, errorOutput: zapcore.Lock(os.Stderr), addStack: zapcore.FatalLevel + 1, - clock: _systemClock, + clock: zapcore.DefaultClock, } return log.WithOptions(options...) } diff --git a/options.go b/options.go index 336dbf804..e9e66161f 100644 --- a/options.go +++ b/options.go @@ -141,7 +141,7 @@ func OnFatal(action zapcore.CheckWriteAction) Option { // WithClock specifies the clock used by the logger to determine the current // time for logged entries. Defaults to the system clock with time.Now. -func WithClock(clock Clock) Option { +func WithClock(clock zapcore.Clock) Option { return optionFunc(func(log *Logger) { log.clock = clock }) diff --git a/zapcore/clock.go b/zapcore/clock.go index 78129061a..d2ea95b39 100644 --- a/zapcore/clock.go +++ b/zapcore/clock.go @@ -24,11 +24,27 @@ import ( "time" ) +// DefaultClock is the default clock used by Zap in operations that require +// time. This clock uses the system clock for all operations. +var DefaultClock = systemClock{} + // Clock is a source of time for logged entries. type Clock interface { // Now returns the current local time. Now() time.Time + // NewTicker returns *time.Ticker that holds a channel // that delivers "ticks" of a clock. NewTicker(time.Duration) *time.Ticker } + +// systemClock implements default Clock that uses system time. +type systemClock struct{} + +func (systemClock) Now() time.Time { + return time.Now() +} + +func (systemClock) NewTicker(duration time.Duration) *time.Ticker { + return time.NewTicker(duration) +} diff --git a/zapcore/clock_test.go b/zapcore/clock_test.go index 67ed38ab9..aab682fec 100644 --- a/zapcore/clock_test.go +++ b/zapcore/clock_test.go @@ -29,16 +29,18 @@ import ( "go.uber.org/atomic" ) +// controlledClock provides control over the time via a mock clock. type controlledClock struct{ *clock.Mock } func newControlledClock() *controlledClock { return &controlledClock{clock.NewMock()} } + func (c *controlledClock) NewTicker(d time.Duration) *time.Ticker { return &time.Ticker{C: c.Ticker(d).C} } -func TestMockClock(t *testing.T) { +func TestControlledClock_NewTicker(t *testing.T) { var n atomic.Int32 ctrlMock := newControlledClock() @@ -65,3 +67,16 @@ func TestMockClock(t *testing.T) { assert.Equal(t, int32(2), n.Load()) close(quit) } + +func TestSystemClock_NewTicker(t *testing.T) { + want := 3 + + var n int + timer := DefaultClock.NewTicker(time.Millisecond) + for range timer.C { + n++ + if n == want { + return + } + } +}