Skip to content

Commit

Permalink
Use ISO8601 timestamp format for zap log messages (#843)
Browse files Browse the repository at this point in the history
* Add flags to configure logging

Adds the zap flagset to configure the logging. This includes the
following flags:
zap-devel: Enable zap development mode (changes defaults to console
encoder, debug log level, disables sampling and stacktrace from warning
level).
zap-encoder: Zap log encoding (json or console).
zap-level: Zap log level (one of debug, info, error or any uint value).
zap-stacktrace-level: Set the minimum log level that triggers stacktrace
generation.

Deprecates the --v flag in favor of --zap-debug.

Signed-off-by: Christoph Stäbler <cstabler@redhat.com>

* Use ISO8601 format for time encoding in logging

Uses the ISO8601 format for encoding of log timestamps for a more
human-readable output.

Signed-off-by: Christoph Stäbler <cstabler@redhat.com>
  • Loading branch information
creydr committed Oct 12, 2021
1 parent 7953d0a commit 2683512
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ spec:
name: kubernetes-nmstate-operator
spec:
containers:
- args:
- --v=production
command:
- command:
- manager
env:
- name: WATCH_NAMESPACE
Expand Down
6 changes: 0 additions & 6 deletions deploy/handler/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ spec:
priorityClassName: system-cluster-critical
containers:
- name: nmstate-webhook
args:
- --v=production
# Replace this with the built image name
image: {{ .HandlerImage }}
imagePullPolicy: {{ .HandlerPullPolicy }}
Expand Down Expand Up @@ -115,8 +113,6 @@ spec:
priorityClassName: system-cluster-critical
containers:
- name: nmstate-cert-manager
args:
- --v=production
# Replace this with the built image name
image: {{ .HandlerImage }}
imagePullPolicy: {{ .HandlerPullPolicy }}
Expand Down Expand Up @@ -209,8 +205,6 @@ spec:
priorityClassName: system-node-critical
containers:
- name: nmstate-handler
args:
- --v=production
# Replace this with the built image name
image: {{ .HandlerImage }}
imagePullPolicy: {{ .HandlerPullPolicy }}
Expand Down
2 changes: 0 additions & 2 deletions deploy/operator/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ spec:
effect: NoSchedule
containers:
- name: nmstate-operator
args:
- --v=production
# Replace this with the built image name
image: {{ .OperatorImage }}
imagePullPolicy: {{ .OperatorPullPolicy }}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ require (
github.com/phoracek/networkmanager-go v0.1.0
github.com/pkg/errors v0.9.1
github.com/qinqon/kube-admission-webhook v0.17.0
github.com/spf13/pflag v1.0.5
github.com/tidwall/gjson v1.8.0
github.com/tidwall/sjson v1.1.7
go.uber.org/zap v1.17.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.21.1
k8s.io/apimachinery v0.21.1
Expand Down
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"time"

"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -35,12 +36,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

// +kubebuilder:scaffold:imports

"github.com/gofrs/flock"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"github.com/qinqon/kube-admission-webhook/pkg/certificate"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/wait"

nmstateapi "github.com/nmstate/kubernetes-nmstate/api/shared"
Expand Down Expand Up @@ -73,11 +76,20 @@ func init() {
}

func main() {
opt := zap.Options{}
opt.BindFlags(flag.CommandLine)
var logType string
flag.StringVar(&logType, "v", "production", "Log type (debug/production).")
flag.Parse()
pflag.StringVar(&logType, "v", "production", "Log type (debug/production).")
pflag.CommandLine.MarkDeprecated("v", "please use the --zap-devel flag for debug logging instead")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

if logType == "debug" {
// workaround until --v flag got removed
flag.CommandLine.Set("zap-devel", "true")
}

ctrl.SetLogger(zap.New(zap.UseDevMode(logType != "production")))
setupLogger(opt)

// Lock only for handler, we can run old and new version of
// webhook without problems, policy status will be updated
Expand Down Expand Up @@ -252,6 +264,15 @@ func main() {
}
}

func setupLogger(opts zap.Options) {
opts.EncoderConfigOptions = append(opts.EncoderConfigOptions, func(ec *zapcore.EncoderConfig) {
ec.EncodeTime = zapcore.ISO8601TimeEncoder
})

logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)
}

// Start profiler on given port if ENABLE_PROFILER is True
func setProfiler() {
cfg := ProfilerConfig{}
Expand Down
2 changes: 2 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ github.com/sirupsen/logrus
# github.com/spf13/cobra v1.1.3
github.com/spf13/cobra
# github.com/spf13/pflag v1.0.5
## explicit
github.com/spf13/pflag
# github.com/spiegel-im-spiegel/errs v1.0.2
github.com/spiegel-im-spiegel/errs
Expand Down Expand Up @@ -642,6 +643,7 @@ go.uber.org/atomic
# go.uber.org/multierr v1.6.0
go.uber.org/multierr
# go.uber.org/zap v1.17.0
## explicit
go.uber.org/zap
go.uber.org/zap/buffer
go.uber.org/zap/internal/bufferpool
Expand Down

0 comments on commit 2683512

Please sign in to comment.