@@ -19,9 +19,11 @@ package cli
19
19
import (
20
20
"context"
21
21
22
+ "go.uber.org/zap"
22
23
"go.uber.org/zap/zapcore"
23
24
"knative.dev/client/pkg/output"
24
25
outlogging "knative.dev/client/pkg/output/logging"
26
+ "knative.dev/pkg/logging"
25
27
"knative.dev/pkg/signals"
26
28
)
27
29
@@ -39,17 +41,46 @@ func InitialContext() context.Context {
39
41
return initialCtx
40
42
}
41
43
44
+ // LoggingSetup is a func that sets the logging into the context.
45
+ type LoggingSetup func (ctx context.Context ) context.Context
46
+
47
+ // DefaultLoggingSetup is the default logging setup.
48
+ func DefaultLoggingSetup (logLevel zapcore.Level ) func (ctx context.Context ) context.Context {
49
+ return func (ctx context.Context ) context.Context {
50
+ ctx = outlogging .WithLogLevel (ctx , logLevel )
51
+ return outlogging .EnsureLogger (ctx )
52
+ }
53
+ }
54
+
55
+ // SimplifiedLoggingSetup is just a production logger to avoid creating
56
+ // additional log files.
57
+ //
58
+ // TODO: Remove this after simplified logging is supported in
59
+ // knative.dev/client/pkg/output/logging package.
60
+ func SimplifiedLoggingSetup (logLevel zapcore.Level ) func (ctx context.Context ) context.Context {
61
+ return func (ctx context.Context ) context.Context {
62
+ prtr := output .PrinterFrom (ctx )
63
+ errout := prtr .ErrOrStderr ()
64
+ ec := zap .NewProductionEncoderConfig ()
65
+ logger := zap .New (zapcore .NewCore (
66
+ zapcore .NewJSONEncoder (ec ),
67
+ zapcore .AddSync (errout ),
68
+ logLevel ,
69
+ ))
70
+ return logging .WithLogger (ctx , logger .Sugar ())
71
+ }
72
+ }
73
+
42
74
// SetupContext will set the context commonly for all CLIs.
43
- func SetupContext (ctxual Contextual , defaultLogLevel zapcore. Level ) {
75
+ func SetupContext (ctxual Contextual , loggingSetup LoggingSetup ) {
44
76
ctx := ctxual .Context ()
45
77
if ctx == initialCtx {
46
78
// TODO: knative.dev/pkg/signals should allow for resetting the
47
79
// context for testing purposes.
48
80
ctx = signals .NewContext ()
49
81
}
50
82
ctx = output .WithContext (ctx , ctxual )
51
- ctx = outlogging .WithLogLevel (ctx , defaultLogLevel )
52
- ctx = outlogging .EnsureLogger (ctx )
83
+ ctx = loggingSetup (ctx )
53
84
ctxual .SetContext (ctx )
54
85
}
55
86
0 commit comments