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

chore: adding optional caller skip and an option to encode time in iso8601 for production logger #145

Open
wants to merge 3 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/crossplane/crossplane-runtime/pkg/logging"

Expand All @@ -44,12 +45,31 @@ func NewLogrLogger(l logr.Logger) Logger {
}

// NewLogger returns a new logger.
func NewLogger(debug bool) (logging.Logger, error) {
o := []zap.Option{zap.AddCallerSkip(1)}
func NewLogger(debug, timeEncodeISO8601 bool, addCallerSkip ...int) (Logger, error) {
// default value for caller skip
callerSkip := 1
if len(addCallerSkip) > 0 {
callerSkip = addCallerSkip[0]
}

o := []zap.Option{zap.AddCallerSkip(callerSkip)}
if debug {
zl, err := zap.NewDevelopment(o...)
return NewLogrLogger(zapr.NewLogger(zl)), errors.Wrap(err, "cannot create development zap logger")
}

// If timeEncodeISO8601 is true, use ISO8601TimeEncoder for production logger.
if timeEncodeISO8601 {
ec := zap.NewProductionEncoderConfig()
ec.EncodeTime = zapcore.ISO8601TimeEncoder

p := zap.NewProductionConfig()
p.EncoderConfig = ec
zl, err := p.Build(o...)
return NewLogrLogger(zapr.NewLogger(zl)), errors.Wrap(err, "cannot create production zap logger")
}

// If timeEncodeISO8601 is false, use the default EpochTimeEncoder for production logger.
zl, err := zap.NewProduction(o...)
return NewLogrLogger(zapr.NewLogger(zl)), errors.Wrap(err, "cannot create production zap logger")
}
4 changes: 2 additions & 2 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ func Serve(fn v1beta1.FunctionRunnerServiceServer, o ...ServeOption) error {
}

// NewLogger returns a new logger.
func NewLogger(debug bool) (logging.Logger, error) {
return logging.NewLogger(debug)
func NewLogger(debug, timeEncodeISO8601 bool, addCallerSkip ...int) (logging.Logger, error) {
return logging.NewLogger(debug, timeEncodeISO8601, addCallerSkip...)
}