Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add F* funcs that allows use func() []Fields as arguments #1041

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import (
// improves the navigability of this package's API documentation.
type Field = zapcore.Field

// FieldsFunc is an alias for the Fields slice
type FieldsFunc func() []Field

var (
_minTimeInt64 = time.Unix(0, math.MinInt64)
_maxTimeInt64 = time.Unix(0, math.MaxInt64)
Expand Down
77 changes: 77 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ func (log *Logger) Debug(msg string, fields ...Field) {
}
}

// FDebug works like a Debug func, but calculate fields values only for appropriate log level
func (log *Logger) FDebug(msg string, fields ...FieldsFunc) {
if ce := log.check(DebugLevel, msg); ce != nil {
var ff []Field
for _, f := range fields {
ff = append(ff, f()...)
}
ce.Write(ff...)
}
}

// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Info(msg string, fields ...Field) {
Expand All @@ -214,6 +225,17 @@ func (log *Logger) Info(msg string, fields ...Field) {
}
}

// FInfo works like an Info func, but calculate fields values only for appropriate log level
func (log *Logger) FInfo(msg string, fields ...FieldsFunc) {
if ce := log.check(InfoLevel, msg); ce != nil {
var ff []Field
for _, f := range fields {
ff = append(ff, f()...)
}
ce.Write(ff...)
}
}

// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Warn(msg string, fields ...Field) {
Expand All @@ -222,6 +244,17 @@ func (log *Logger) Warn(msg string, fields ...Field) {
}
}

// FWarn works like a Warn func, but calculate fields values only for appropriate log level
func (log *Logger) FWarn(msg string, fields ...FieldsFunc) {
if ce := log.check(WarnLevel, msg); ce != nil {
var ff []Field
for _, f := range fields {
ff = append(ff, f()...)
}
ce.Write(ff...)
}
}

// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Error(msg string, fields ...Field) {
Expand All @@ -230,6 +263,17 @@ func (log *Logger) Error(msg string, fields ...Field) {
}
}

// FError works like an Error func, but calculate fields values only for appropriate log level
func (log *Logger) FError(msg string, fields ...FieldsFunc) {
if ce := log.check(ErrorLevel, msg); ce != nil {
var ff []Field
for _, f := range fields {
ff = append(ff, f()...)
}
ce.Write(ff...)
}
}

// DPanic logs a message at DPanicLevel. The message includes any fields
// passed at the log site, as well as any fields accumulated on the logger.
//
Expand All @@ -242,6 +286,17 @@ func (log *Logger) DPanic(msg string, fields ...Field) {
}
}

// FDPanic works like a DPanic func, but calculate fields values only for appropriate log level
func (log *Logger) FDPanic(msg string, fields ...FieldsFunc) {
if ce := log.check(DPanicLevel, msg); ce != nil {
var ff []Field
for _, f := range fields {
ff = append(ff, f()...)
}
ce.Write(ff...)
}
}

// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
Expand All @@ -252,6 +307,17 @@ func (log *Logger) Panic(msg string, fields ...Field) {
}
}

// FPanic works like a Panic func, but calculate fields values only for appropriate log level
func (log *Logger) FPanic(msg string, fields ...FieldsFunc) {
if ce := log.check(PanicLevel, msg); ce != nil {
var ff []Field
for _, f := range fields {
ff = append(ff, f()...)
}
ce.Write(ff...)
}
}

// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
Expand All @@ -263,6 +329,17 @@ func (log *Logger) Fatal(msg string, fields ...Field) {
}
}

// FFatal works like a Fatal func, but calculate fields values only for appropriate log level
func (log *Logger) FFatal(msg string, fields ...FieldsFunc) {
if ce := log.check(FatalLevel, msg); ce != nil {
var ff []Field
for _, f := range fields {
ff = append(ff, f()...)
}
ce.Write(ff...)
}
}

// Sync calls the underlying Core's Sync method, flushing any buffered log
// entries. Applications should take care to call Sync before exiting.
func (log *Logger) Sync() error {
Expand Down
62 changes: 62 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,32 @@ func TestLoggerLogLevels(t *testing.T) {
})
}

func TestLoggerLeveledFuncMethods(t *testing.T) {
withLogger(t, DebugLevel, nil, func(logger *Logger, logs *observer.ObservedLogs) {
tests := []struct {
method func(string, ...FieldsFunc)
expectedLevel zapcore.Level
}{
{logger.FDebug, DebugLevel},
{logger.FInfo, InfoLevel},
{logger.FWarn, WarnLevel},
{logger.FError, ErrorLevel},
{logger.FDPanic, DPanicLevel},
}
for i, tt := range tests {
tt.method("")
output := logs.AllUntimed()
assert.Equal(t, i+1, len(output), "Unexpected number of logs.")
assert.Equal(t, 0, len(output[i].Context), "Unexpected context on first log.")
assert.Equal(
t,
zapcore.Entry{Level: tt.expectedLevel},
output[i].Entry,
"Unexpected output from %s-level logger method.", tt.expectedLevel)
}
})
}

func TestLoggerAlwaysPanics(t *testing.T) {
// Users can disable writing out panic-level logs, but calls to logger.Panic()
// should still call panic().
Expand Down Expand Up @@ -667,6 +693,42 @@ func TestMust(t *testing.T) {
})
}

func TestFieldsFuncCalls(t *testing.T) {
withLogger(t, InfoLevel, nil, func(logger *Logger, logs *observer.ObservedLogs) {
var (
debugFuncWasCalled bool
infoFuncWasCalled bool
)

debugFields := func() []Field {
debugFuncWasCalled = true
return []Field{
String("string", "debug"),
}
}

infoFields := func() []Field {
infoFuncWasCalled = true
return []Field{
String("string", "info"),
}
}

logger.FDebug("", debugFields)
logger.FInfo("", infoFields)
output := logs.AllUntimed()
require.Equal(t, 1, len(output), "Unexpected number of logs.")
assert.Equal(
t,
zapcore.Entry{Level: InfoLevel},
output[0].Entry,
"Unexpected output from %s-level logger method.", InfoLevel)

assert.False(t, debugFuncWasCalled)
assert.True(t, infoFuncWasCalled)
})
}

func infoLog(logger *Logger, msg string, fields ...Field) {
logger.Info(msg, fields...)
}
Expand Down