Skip to content

Commit

Permalink
Merge branch 'master' into prashantv/zap-win-abs
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav committed Sep 9, 2022
2 parents a711d83 + 7681a0a commit 9802e6a
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 21 deletions.
8 changes: 4 additions & 4 deletions array_go118.go
Expand Up @@ -76,9 +76,9 @@ func (os objects[T]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
return nil
}

// objectMarshalerPtr is a constraint that specifies that the given type
// ObjectMarshalerPtr is a constraint that specifies that the given type
// implements zapcore.ObjectMarshaler on a pointer receiver.
type objectMarshalerPtr[T any] interface {
type ObjectMarshalerPtr[T any] interface {
*T
zapcore.ObjectMarshaler
}
Expand All @@ -105,11 +105,11 @@ type objectMarshalerPtr[T any] interface {
//
// var requests []*Request = ...
// logger.Info("sending requests", zap.Objects("requests", requests))
func ObjectValues[T any, P objectMarshalerPtr[T]](key string, values []T) Field {
func ObjectValues[T any, P ObjectMarshalerPtr[T]](key string, values []T) Field {
return Array(key, objectValues[T, P](values))
}

type objectValues[T any, P objectMarshalerPtr[T]] []T
type objectValues[T any, P ObjectMarshalerPtr[T]] []T

func (os objectValues[T, P]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
for i := range os {
Expand Down
3 changes: 1 addition & 2 deletions config_test.go
Expand Up @@ -138,8 +138,7 @@ func TestConfigWithMissingAttributes(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
cfg := tt.cfg
_, err := cfg.Build()
require.Error(t, err)
assert.Equal(t, tt.expectErr, err.Error())
assert.EqualError(t, err, tt.expectErr)
})
}
}
Expand Down
6 changes: 2 additions & 4 deletions global_test.go
Expand Up @@ -143,8 +143,7 @@ func TestNewStdLogAtFatal(t *testing.T) {

func TestNewStdLogAtInvalid(t *testing.T) {
_, err := NewStdLogAt(NewNop(), zapcore.Level(99))
assert.Error(t, err, "Expected to get error.")
assert.Contains(t, err.Error(), "99", "Expected level code in error message")
assert.ErrorContains(t, err, "99", "Expected level code in error message")
}

func TestRedirectStdLog(t *testing.T) {
Expand Down Expand Up @@ -262,8 +261,7 @@ func TestRedirectStdLogAtInvalid(t *testing.T) {
restore()
}
}()
require.Error(t, err, "Expected to get error.")
assert.Contains(t, err.Error(), "99", "Expected level code in error message")
assert.ErrorContains(t, err, "99", "Expected level code in error message")
}

func checkStdLogMessage(t *testing.T, msg string, logs *observer.ObservedLogs) {
Expand Down
6 changes: 3 additions & 3 deletions level_test.go
Expand Up @@ -27,6 +27,7 @@ import (
"go.uber.org/zap/zapcore"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLevelEnablerFunc(t *testing.T) {
Expand Down Expand Up @@ -71,11 +72,10 @@ func TestParseAtomicLevel(t *testing.T) {
for _, tt := range tests {
parsedAtomicLevel, err := ParseAtomicLevel(tt.text)
if len(tt.err) == 0 {
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tt.level, parsedAtomicLevel)
} else {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.err)
assert.ErrorContains(t, err, tt.err)
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions logger.go
Expand Up @@ -183,6 +183,13 @@ func (log *Logger) With(fields ...Field) *Logger {
return l
}

// Level reports the minimum enabled level for this logger.
//
// For NopLoggers, this is [zapcore.InvalidLevel].
func (log *Logger) Level() zapcore.Level {
return zapcore.LevelOf(log.core)
}

// Check returns a CheckedEntry if logging a message at the specified level
// is enabled. It's a completely optional optimization; in high-performance
// applications, Check can help avoid allocating a slice to hold fields.
Expand Down
27 changes: 27 additions & 0 deletions logger_test.go
Expand Up @@ -83,6 +83,33 @@ func TestLoggerAtomicLevel(t *testing.T) {
})
}

func TestLoggerLevel(t *testing.T) {
levels := []zapcore.Level{
DebugLevel,
InfoLevel,
WarnLevel,
ErrorLevel,
DPanicLevel,
PanicLevel,
FatalLevel,
}

for _, lvl := range levels {
lvl := lvl
t.Run(lvl.String(), func(t *testing.T) {
t.Parallel()

core, _ := observer.New(lvl)
log := New(core)
assert.Equal(t, lvl, log.Level())
})
}

t.Run("Nop", func(t *testing.T) {
assert.Equal(t, zapcore.InvalidLevel, NewNop().Level())
})
}

func TestLoggerInitialFields(t *testing.T) {
fieldOpts := opts(Fields(Int("foo", 42), String("bar", "baz")))
withLogger(t, DebugLevel, fieldOpts, func(logger *Logger, logs *observer.ObservedLogs) {
Expand Down
2 changes: 1 addition & 1 deletion sink_test.go
Expand Up @@ -103,7 +103,7 @@ func TestRegisterSinkErrors(t *testing.T) {
t.Run("scheme-"+tt.scheme, func(t *testing.T) {
err := r.RegisterSink(tt.scheme, nopFactory)
if assert.Error(t, err, "expected error") {
assert.Contains(t, err.Error(), tt.err, "unexpected error")
assert.ErrorContains(t, err, tt.err, "unexpected error")
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion stacktrace.go
Expand Up @@ -154,7 +154,7 @@ func newStackFormatter(b *buffer.Buffer) stackFormatter {
// the final runtime.main/runtime.goexit frame.
func (sf *stackFormatter) FormatStack(stack *stacktrace) {
// Note: On the last iteration, frames.Next() returns false, with a valid
// frame, but we ignore this frame. The last frame is a a runtime frame which
// frame, but we ignore this frame. The last frame is a runtime frame which
// adds noise, since it's only either runtime.main or runtime.goexit.
for frame, more := stack.Next(); more; frame, more = stack.Next() {
sf.FormatFrame(frame)
Expand Down
7 changes: 7 additions & 0 deletions sugar.go
Expand Up @@ -114,6 +114,13 @@ func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger {
return &SugaredLogger{base: s.base.With(s.sweetenFields(args)...)}
}

// Level reports the minimum enabled level for this logger.
//
// For NopLoggers, this is [zapcore.InvalidLevel].
func (s *SugaredLogger) Level() zapcore.Level {
return zapcore.LevelOf(s.base.core)
}

// Debug uses fmt.Sprint to construct and log a message.
func (s *SugaredLogger) Debug(args ...interface{}) {
s.log(DebugLevel, "", args, nil)
Expand Down
29 changes: 29 additions & 0 deletions sugar_test.go
Expand Up @@ -139,6 +139,35 @@ func TestSugarWith(t *testing.T) {
}
}

func TestSugaredLoggerLevel(t *testing.T) {
levels := []zapcore.Level{
DebugLevel,
InfoLevel,
WarnLevel,
ErrorLevel,
DPanicLevel,
PanicLevel,
FatalLevel,
}

for _, lvl := range levels {
lvl := lvl
t.Run(lvl.String(), func(t *testing.T) {
t.Parallel()

core, _ := observer.New(lvl)
log := New(core).Sugar()
assert.Equal(t, lvl, log.Level())
})
}

t.Run("Nop", func(t *testing.T) {
t.Parallel()

assert.Equal(t, zapcore.InvalidLevel, NewNop().Sugar().Level())
})
}

func TestSugarFieldsInvalidPairs(t *testing.T) {
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
logger.With(42, "foo", []string{"bar"}, "baz").Info("")
Expand Down
2 changes: 1 addition & 1 deletion writer_test.go
Expand Up @@ -249,7 +249,7 @@ func TestOpenWithErroringSinkFactory(t *testing.T) {

assert.NoError(t, RegisterSink("test", factory), "Failed to register sink factory.")
_, _, err := Open("test://some/path")
assert.Contains(t, err.Error(), msg, "Unexpected error.")
assert.ErrorContains(t, err, msg)
}

func TestCombineWriteSyncers(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions zapcore/increase_level_test.go
Expand Up @@ -87,8 +87,7 @@ func TestIncreaseLevel(t *testing.T) {

filteredLogger, err := NewIncreaseLevelCore(logger, tt.increaseLevel)
if tt.wantErr {
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid increase level")
assert.ErrorContains(t, err, "invalid increase level")
return
}

Expand Down
5 changes: 2 additions & 3 deletions zapcore/level_test.go
Expand Up @@ -93,8 +93,7 @@ func TestParseLevel(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, tt.level, parsedLevel)
} else {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.err)
assert.ErrorContains(t, err, tt.err)
}
}
}
Expand Down Expand Up @@ -170,7 +169,7 @@ func TestLevelNils(t *testing.T) {
func TestLevelUnmarshalUnknownText(t *testing.T) {
var l Level
err := l.UnmarshalText([]byte("foo"))
assert.Contains(t, err.Error(), "unrecognized level", "Expected unmarshaling arbitrary text to fail.")
assert.ErrorContains(t, err, "unrecognized level", "Expected unmarshaling arbitrary text to fail.")
}

func TestLevelAsFlagValue(t *testing.T) {
Expand Down

0 comments on commit 9802e6a

Please sign in to comment.