diff --git a/go.mod b/go.mod index ebcb20b73..f04f79420 100644 --- a/go.mod +++ b/go.mod @@ -54,6 +54,7 @@ require ( require ( github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 + github.com/bombsimon/logrusr/v4 v4.1.0 github.com/prometheus/client_model v0.5.0 gopkg.in/yaml.v2 v2.4.0 k8s.io/utils v0.0.0-20230505201702-9f6742963106 diff --git a/go.sum b/go.sum index 3c0b70905..d8e790c01 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/bombsimon/logrusr/v4 v4.1.0 h1:uZNPbwusB0eUXlO8hIUwStE6Lr5bLN6IgYgG+75kuh4= +github.com/bombsimon/logrusr/v4 v4.1.0/go.mod h1:pjfHC5e59CvjTBIU3V3sGhFWFAnsnhOR03TRc6im0l8= github.com/briandowns/spinner v0.0.0-20190212173954-5cf08d0ac778 h1:Dmz6bJXocvwkw7BOz4jpyVZReGrkjs+fBDWKn5tBES4= github.com/briandowns/spinner v0.0.0-20190212173954-5cf08d0ac778/go.mod h1:hw/JEQBIE+c/BLI4aKM8UU8v+ZqrD3h7HC27kKt8JQU= github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuPGoOVeF2fE4Og9otCc70= diff --git a/pkg/flags/flags.go b/pkg/flags/flags.go index 7df09f25d..f447ac6cc 100644 --- a/pkg/flags/flags.go +++ b/pkg/flags/flags.go @@ -6,6 +6,8 @@ import ( "os" "github.com/linkerd/linkerd2/pkg/version" + + "github.com/bombsimon/logrusr/v4" log "github.com/sirupsen/logrus" "github.com/spf13/pflag" "helm.sh/helm/v3/pkg/cli/values" @@ -28,11 +30,6 @@ const ( // func calls flag.Parse(), so it should be called after all other flags have // been configured. func ConfigureAndParse(cmd *flag.FlagSet, args []string) { - klog.InitFlags(nil) - flag.Set("stderrthreshold", "FATAL") - flag.Set("logtostderr", "false") - flag.Set("log_file", "/dev/null") - flag.Set("v", "0") logLevel := cmd.String("log-level", log.InfoLevel.String(), "log level, must be one of: panic, fatal, error, warn, info, debug, trace") logFormat := cmd.String("log-format", "plain", @@ -44,9 +41,11 @@ func ConfigureAndParse(cmd *flag.FlagSet, args []string) { //nolint:errcheck cmd.Parse(args) - // set log timestamps log.SetFormatter(getFormatter(*logFormat)) + klog.InitFlags(nil) + klog.SetLogger(logrusr.New(log.StandardLogger())) + setLogLevel(*logLevel) maybePrintVersionAndExit(*printVersion) } @@ -66,16 +65,26 @@ func setLogLevel(logLevel string) { } log.SetLevel(level) - if level >= log.DebugLevel { - flag.Set("stderrthreshold", "INFO") - flag.Set("logtostderr", "true") + // Loosely based on k8s logging conventions, except for 'tracing' that we + // bump to 10 (we can see in client-go source code that level is actually + // used) and `debug` to 6 (given that at level 7 and higher auth tokens get + // logged) + // https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md + switch level { + case log.PanicLevel: + flag.Set("v", "0") + case log.FatalLevel: + flag.Set("v", "0") + case log.ErrorLevel: + flag.Set("v", "0") + case log.WarnLevel: + flag.Set("v", "0") + case log.InfoLevel: + flag.Set("v", "2") + case log.DebugLevel: flag.Set("v", "6") - // pipe klog entries to logrus - klog.SetOutput(log.StandardLogger().Writer()) - } - - if level >= log.TraceLevel { - flag.Set("v", "12") // At 7 and higher, authorization tokens get logged. + case log.TraceLevel: + flag.Set("v", "10") } }