diff --git a/logging/logging.go b/logging/logging.go index 6032c57..9420ca9 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -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" @@ -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") } 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...) }