Skip to content

Commit

Permalink
Revert "Rename to zaptest/tlogger"
Browse files Browse the repository at this point in the history
This reverts commit be2b5ea.
  • Loading branch information
abhinav authored and akshayjshah committed Apr 12, 2018
1 parent 944a4f3 commit db54f2d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
40 changes: 25 additions & 15 deletions zaptest/tlogger/tlogger.go → zaptest/test_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,49 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Package tlogger provides a zapcore.Core that is capable of writing log
// messages to a *testing.T and *testing.B. It may be used from Go tests or
// benchmarks to have log messages printed only if a test failed, or if the
// `-v` flag was passed to `go test`.
package tlogger // import "go.uber.org/zap/zaptest/tlogger"
package zaptest

import (
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// New builds a new Core that logs all messages to the given testing.TB.
// NewTestLogger builds a new Core that logs all messages to the given
// testing.TB.
//
// Use this with a *testing.T or *testing.B to get logs which get printed only
// if a test fails or if you ran go test -v.
//
// logger := zap.New(tlogger.New(t))
func New(t testing.TB) zapcore.Core {
return NewAt(t, zapcore.DebugLevel)
// logger := zap.New(NewTestLogger(t))
func NewTestLogger(t testing.TB) zapcore.Core {
return NewTestLoggerAt(t, zapcore.DebugLevel)
}

// NewAt builds a new Core that logs messages to the given testing.TB if the
// given LevelEnabler allows it.
// NewTestLoggerAt builds a new Core that logs messages to the given
// testing.TB if the given LevelEnabler allows it.
//
// Use this with a *testing.T or *testing.B to get logs which get printed only
// if a test fails or if you ran go test -v.
//
// logger := zap.New(tlogger.NewAt(t, zap.InfoLevel))
func NewAt(t testing.TB, enab zapcore.LevelEnabler) zapcore.Core {
// logger := zap.New(NewTestLoggerAt(t, zap.InfoLevel))
func NewTestLoggerAt(t testing.TB, enab zapcore.LevelEnabler) zapcore.Core {
return zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
// EncoderConfig copied from zap.NewDevelopmentEncoderConfig.
// Can't use it directly because that would cause a cyclic import.
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}),
testingWriter{t},
enab,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package tlogger
package zaptest

import (
"errors"
Expand All @@ -34,7 +34,7 @@ import (

func TestTestLoggerIncludesDebug(t *testing.T) {
ts := newTestLogSpy(t)
log := zap.New(New(ts))
log := zap.New(NewTestLogger(ts))
log.Debug("calculating")
log.Info("finished calculating", zap.Int("answer", 42))

Expand All @@ -46,7 +46,7 @@ func TestTestLoggerIncludesDebug(t *testing.T) {

func TestTestLoggerAt(t *testing.T) {
ts := newTestLogSpy(t)
log := zap.New(NewAt(ts, zap.WarnLevel))
log := zap.New(NewTestLoggerAt(ts, zap.WarnLevel))

log.Info("received work order")
log.Debug("starting work")
Expand Down

0 comments on commit db54f2d

Please sign in to comment.