From d9c8395d12e8be2702caa56d731f225d2b2e7476 Mon Sep 17 00:00:00 2001 From: Chaitanya Kolluru Date: Tue, 4 Jun 2024 12:13:26 -0500 Subject: [PATCH 1/3] adding optional caller skip and option to encode time in iso8601 Signed-off-by: Chaitanya Kolluru --- logging/logging.go | 25 ++++++++++++++++++++++--- sdk.go | 4 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/logging/logging.go b/logging/logging.go index 6032c57..fd7d8d9 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -24,9 +24,9 @@ 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" - "github.com/crossplane/function-sdk-go/errors" ) @@ -44,12 +44,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 { + pCfg := zap.NewProductionEncoderConfig() + pCfg.EncodeTime = zapcore.ISO8601TimeEncoder + + p := zap.NewProductionConfig() + p.EncoderConfig = pCfg + 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") } diff --git a/sdk.go b/sdk.go index 5b4aadb..e1a7520 100644 --- a/sdk.go +++ b/sdk.go @@ -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...) } From 924edfbbec42109fbb9eac15fd0c995db101c58c Mon Sep 17 00:00:00 2001 From: Chaitanya Kolluru Date: Tue, 4 Jun 2024 12:32:58 -0500 Subject: [PATCH 2/3] linting Signed-off-by: Chaitanya Kolluru --- logging/logging.go | 1 + 1 file changed, 1 insertion(+) diff --git a/logging/logging.go b/logging/logging.go index fd7d8d9..54a629c 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -27,6 +27,7 @@ import ( "go.uber.org/zap/zapcore" "github.com/crossplane/crossplane-runtime/pkg/logging" + "github.com/crossplane/function-sdk-go/errors" ) From 48f8c5301fcdf1438cbd298f69155e66252f89f2 Mon Sep 17 00:00:00 2001 From: Chaitanya Kolluru Date: Tue, 4 Jun 2024 13:06:12 -0500 Subject: [PATCH 3/3] cleaning var names Signed-off-by: Chaitanya Kolluru --- logging/logging.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/logging/logging.go b/logging/logging.go index 54a629c..9420ca9 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -60,11 +60,11 @@ func NewLogger(debug, timeEncodeISO8601 bool, addCallerSkip ...int) (Logger, err // If timeEncodeISO8601 is true, use ISO8601TimeEncoder for production logger. if timeEncodeISO8601 { - pCfg := zap.NewProductionEncoderConfig() - pCfg.EncodeTime = zapcore.ISO8601TimeEncoder + ec := zap.NewProductionEncoderConfig() + ec.EncodeTime = zapcore.ISO8601TimeEncoder p := zap.NewProductionConfig() - p.EncoderConfig = pCfg + p.EncoderConfig = ec zl, err := p.Build(o...) return NewLogrLogger(zapr.NewLogger(zl)), errors.Wrap(err, "cannot create production zap logger") }