Skip to content

Commit 0c6c063

Browse files
author
Shawn Hurley
authored
pkg/log/zap: Adding ability to get client-go and trace level logs (#1322)
**Description of the change:** Adding the ability to set the log level for klog if the zap-level is for enough verbosity. **Motivation for the change:** Allowing users to get more debug level logs with help when diagnosing problems.
1 parent 2ca5cb1 commit 0c6c063

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Changed
66

77
- When Helm operator projects are created, the SDK now generates RBAC rules in `deploy/role.yaml` based on the chart's default manifest. ([#1188](https://github.com/operator-framework/operator-sdk/pull/1188))
8+
- When debug level is 3 or higher, we will set the klog verbosity to that level. ([#1322](https://github.com/operator-framework/operator-sdk/pull/1322))
89

910
### Deprecated
1011

Gopkg.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/user/logging.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ In the above example, we add the zap flagset to the operator's command line flag
3838
By default, `zap.Logger()` will return a logger that is ready for production use. It uses a JSON encoder, logs starting at the `info` level, and has [sampling][zap_sampling] enabled. To customize the default behavior, users can use the zap flagset and specify flags on the command line. The zap flagset includes the following flags that can be used to configure the logger:
3939
* `--zap-devel` - Enables the zap development config (changes defaults to console encoder, debug log level, and disables sampling) (default: `false`)
4040
* `--zap-encoder` string - Sets the zap log encoding (`json` or `console`)
41-
* `--zap-level` string or integer - Sets the zap log level (`debug`, `info`, `error`, or an integer value greater than 0)
41+
* `--zap-level` string or integer - Sets the zap log level (`debug`, `info`, `error`, or an integer value greater than 0). If 4 or greater the verbosity of client-go will be set to this level.
4242
* `--zap-sample` - Enables zap's sampling mode. Sampling will be disabled for integer log levels greater than 1.
4343

4444

pkg/log/zap/flags.go

+12
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
package zap
1616

1717
import (
18+
"flag"
1819
"fmt"
1920
"strconv"
2021
"strings"
2122

2223
"github.com/spf13/pflag"
2324
"go.uber.org/zap"
2425
"go.uber.org/zap/zapcore"
26+
"k8s.io/klog"
2527
)
2628

2729
var (
@@ -41,6 +43,7 @@ func init() {
4143
zapFlagSet.Var(&sampleVal, "zap-sample", "Enable zap log sampling. Sampling will be disabled for integer log levels > 1")
4244
}
4345

46+
// FlagSet - The zap logging flagset.
4447
func FlagSet() *pflag.FlagSet {
4548
return zapFlagSet
4649
}
@@ -112,6 +115,15 @@ func (v *levelValue) Set(l string) error {
112115
}
113116
}
114117
v.level = zapcore.Level(int8(lvl))
118+
// If log level is greater than debug, set glog/klog level to that level.
119+
if lvl < -3 {
120+
fs := flag.NewFlagSet("", flag.ContinueOnError)
121+
klog.InitFlags(fs)
122+
err := fs.Set("v", fmt.Sprintf("%v", -1*lvl))
123+
if err != nil {
124+
return err
125+
}
126+
}
115127
return nil
116128
}
117129

0 commit comments

Comments
 (0)