Skip to content

Commit 546ffde

Browse files
authored
feat: use klog as the default logging library (#1008)
1 parent 299258e commit 546ffde

File tree

28 files changed

+144
-113
lines changed

28 files changed

+144
-113
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ scan:
213213
./
214214

215215
.PHONY: lint
216-
lint:
216+
lint: fmt vet
217217
golangci-lint run --new -c .golangci.yaml ${BUILDPATHS}
218218

219219
.PHONY: lint-and-fix
220-
lint-and-fix:
220+
lint-and-fix: fmt vet
221221
golangci-lint run --new --fix -c .golangci.yaml ${BUILDPATHS}

cmd/analyze/cli/root.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"os"
55
"strings"
66

7-
"github.com/go-logr/logr"
87
"github.com/replicatedhq/troubleshoot/cmd/util"
98
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
109
"github.com/replicatedhq/troubleshoot/pkg/logger"
@@ -24,13 +23,10 @@ func RootCmd() *cobra.Command {
2423
v := viper.GetViper()
2524
v.BindPFlags(cmd.Flags())
2625

27-
if !v.GetBool("debug") {
28-
klog.SetLogger(logr.Discard())
29-
}
30-
logger.SetQuiet(v.GetBool("quiet"))
26+
logger.SetupLogger(v)
3127

3228
if err := util.StartProfiling(); err != nil {
33-
logger.Printf("Failed to start profiling: %v", err)
29+
klog.Errorf("Failed to start profiling: %v", err)
3430
}
3531
},
3632
RunE: func(cmd *cobra.Command, args []string) error {
@@ -40,7 +36,7 @@ func RootCmd() *cobra.Command {
4036
},
4137
PostRun: func(cmd *cobra.Command, args []string) {
4238
if err := util.StopProfiling(); err != nil {
43-
logger.Printf("Failed to stop profiling: %v", err)
39+
klog.Errorf("Failed to stop profiling: %v", err)
4440
}
4541
},
4642
}
@@ -54,6 +50,9 @@ func RootCmd() *cobra.Command {
5450

5551
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
5652

53+
// Initialize klog flags
54+
logger.InitKlogFlags(cmd)
55+
5756
k8sutil.AddFlags(cmd.Flags())
5857

5958
// CPU and memory profiling flags

cmd/collect/cli/root.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"os"
55
"strings"
66

7-
"github.com/go-logr/logr"
87
"github.com/replicatedhq/troubleshoot/cmd/util"
98
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
109
"github.com/replicatedhq/troubleshoot/pkg/logger"
@@ -24,23 +23,20 @@ func RootCmd() *cobra.Command {
2423
v := viper.GetViper()
2524
v.BindPFlags(cmd.Flags())
2625

27-
if !v.GetBool("debug") {
28-
klog.SetLogger(logr.Discard())
29-
}
26+
logger.SetupLogger(v)
3027

3128
if err := util.StartProfiling(); err != nil {
32-
logger.Printf("Failed to start profiling: %v", err)
29+
klog.Errorf("Failed to start profiling: %v", err)
3330
}
3431
},
3532
RunE: func(cmd *cobra.Command, args []string) error {
3633
v := viper.GetViper()
3734

38-
logger.SetQuiet(v.GetBool("quiet"))
3935
return runCollect(v, args[0])
4036
},
4137
PostRun: func(cmd *cobra.Command, args []string) {
4238
if err := util.StopProfiling(); err != nil {
43-
logger.Printf("Failed to stop profiling: %v", err)
39+
klog.Errorf("Failed to stop profiling: %v", err)
4440
}
4541
},
4642
}
@@ -68,6 +64,9 @@ func RootCmd() *cobra.Command {
6864

6965
k8sutil.AddFlags(cmd.Flags())
7066

67+
// Initialize klog flags
68+
logger.InitKlogFlags(cmd)
69+
7170
// CPU and memory profiling flags
7271
util.AddProfilingFlags(cmd)
7372

cmd/preflight/cli/root.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/go-logr/logr"
98
"github.com/replicatedhq/troubleshoot/cmd/util"
109
"github.com/replicatedhq/troubleshoot/internal/traces"
1110
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
@@ -29,33 +28,31 @@ that a cluster meets the requirements to run an application.`,
2928
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
3029
v.BindPFlags(cmd.Flags())
3130

32-
if !v.GetBool("debug") {
33-
klog.SetLogger(logr.Discard())
34-
}
31+
logger.SetupLogger(v)
3532

3633
if err := util.StartProfiling(); err != nil {
37-
logger.Printf("Failed to start profiling: %v", err)
34+
klog.Errorf("Failed to start profiling: %v", err)
3835
}
3936
},
4037
RunE: func(cmd *cobra.Command, args []string) error {
4138
v := viper.GetViper()
4239
closer, err := traces.ConfigureTracing("preflight")
4340
if err != nil {
4441
// Do not fail running preflights if tracing fails
45-
logger.Printf("Failed to initialize open tracing provider: %v", err)
42+
klog.Errorf("Failed to initialize open tracing provider: %v", err)
4643
} else {
4744
defer closer()
4845
}
4946

5047
err = preflight.RunPreflights(v.GetBool("interactive"), v.GetString("output"), v.GetString("format"), args)
51-
if v.GetBool("debug") {
48+
if v.GetBool("debug") || v.IsSet("v") {
5249
fmt.Printf("\n%s", traces.GetExporterInstance().GetSummary())
5350
}
5451
return err
5552
},
5653
PostRun: func(cmd *cobra.Command, args []string) {
5754
if err := util.StopProfiling(); err != nil {
58-
logger.Printf("Failed to stop profiling: %v", err)
55+
klog.Errorf("Failed to stop profiling: %v", err)
5956
}
6057
},
6158
}
@@ -67,6 +64,9 @@ that a cluster meets the requirements to run an application.`,
6764

6865
k8sutil.AddFlags(cmd.Flags())
6966

67+
// Initialize klog flags
68+
logger.InitKlogFlags(cmd)
69+
7070
// CPU and memory profiling flags
7171
util.AddProfilingFlags(cmd)
7272

cmd/troubleshoot/cli/analyze.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/replicatedhq/troubleshoot/cmd/util"
1111
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
1212
"github.com/replicatedhq/troubleshoot/pkg/convert"
13-
"github.com/replicatedhq/troubleshoot/pkg/logger"
1413
"github.com/spf13/cobra"
1514
"github.com/spf13/viper"
1615
"gopkg.in/yaml.v2"
@@ -28,8 +27,6 @@ func Analyze() *cobra.Command {
2827
RunE: func(cmd *cobra.Command, args []string) error {
2928
v := viper.GetViper()
3029

31-
logger.SetQuiet(v.GetBool("quiet"))
32-
3330
specPath := args[0]
3431
analyzerSpec, err := downloadAnalyzerSpec(specPath)
3532
if err != nil {

cmd/troubleshoot/cli/redact.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/pkg/errors"
99
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
1010
"github.com/replicatedhq/troubleshoot/pkg/collect"
11-
"github.com/replicatedhq/troubleshoot/pkg/logger"
1211
"github.com/replicatedhq/troubleshoot/pkg/supportbundle"
1312
"github.com/spf13/cobra"
1413
"github.com/spf13/viper"
@@ -35,8 +34,6 @@ For more information on redactors visit https://troubleshoot.sh/docs/redact/
3534
RunE: func(cmd *cobra.Command, args []string) error {
3635
v := viper.GetViper()
3736

38-
logger.SetQuiet(v.GetBool("quiet"))
39-
4037
// 1. Decode redactors from provided URLs
4138
redactors, err := supportbundle.GetRedactorsFromURIs(args)
4239
if err != nil {

cmd/troubleshoot/cli/root.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/go-logr/logr"
98
"github.com/replicatedhq/troubleshoot/cmd/util"
109
"github.com/replicatedhq/troubleshoot/internal/traces"
1110
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
@@ -28,38 +27,33 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
2827
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
2928
v.BindPFlags(cmd.Flags())
3029

30+
logger.SetupLogger(v)
31+
3132
if err := util.StartProfiling(); err != nil {
32-
logger.Printf("Failed to start profiling: %v", err)
33+
klog.Errorf("Failed to start profiling: %v", err)
3334
}
3435
},
35-
PreRun: func(cmd *cobra.Command, args []string) {
36-
v := viper.GetViper()
37-
if !v.GetBool("debug") {
38-
klog.SetLogger(logr.Discard())
39-
}
40-
logger.SetQuiet(!v.GetBool("debug"))
41-
},
4236
RunE: func(cmd *cobra.Command, args []string) error {
4337
v := viper.GetViper()
4438

4539
closer, err := traces.ConfigureTracing("support-bundle")
4640
if err != nil {
4741
// Do not fail running support-bundle if tracing fails
48-
logger.Printf("Failed to initialize open tracing provider: %v", err)
42+
klog.Errorf("Failed to initialize open tracing provider: %v", err)
4943
} else {
5044
defer closer()
5145
}
5246

5347
err = runTroubleshoot(v, args)
54-
if v.GetBool("debug") {
48+
if v.GetBool("debug") || v.IsSet("v") {
5549
fmt.Printf("\n%s", traces.GetExporterInstance().GetSummary())
5650
}
5751

5852
return err
5953
},
6054
PersistentPostRun: func(cmd *cobra.Command, args []string) {
6155
if err := util.StopProfiling(); err != nil {
62-
logger.Printf("Failed to stop profiling: %v", err)
56+
klog.Errorf("Failed to stop profiling: %v", err)
6357
}
6458
},
6559
}
@@ -79,7 +73,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
7973
cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)")
8074
cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")
8175
cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle")
82-
cmd.Flags().Bool("debug", false, "enable debug logging")
76+
cmd.Flags().Bool("debug", false, "enable debug logging. This is equivalent to --v=0")
8377

8478
// hidden in favor of the `insecure-skip-tls-verify` flag
8579
cmd.Flags().Bool("allow-insecure-connections", false, "when set, do not verify TLS certs when retrieving spec and reporting results")
@@ -91,6 +85,9 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
9185

9286
k8sutil.AddFlags(cmd.Flags())
9387

88+
// Initialize klog flags
89+
logger.InitKlogFlags(cmd)
90+
9491
// CPU and memory profiling flags
9592
util.AddProfilingFlags(cmd)
9693

cmd/troubleshoot/cli/run.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323
"github.com/replicatedhq/troubleshoot/pkg/convert"
2424
"github.com/replicatedhq/troubleshoot/pkg/httputil"
2525
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
26-
"github.com/replicatedhq/troubleshoot/pkg/logger"
2726
"github.com/replicatedhq/troubleshoot/pkg/specs"
2827
"github.com/replicatedhq/troubleshoot/pkg/supportbundle"
2928
"github.com/spf13/viper"
3029
spin "github.com/tj/go-spin"
3130
"k8s.io/apimachinery/pkg/labels"
3231
"k8s.io/client-go/kubernetes"
3332
"k8s.io/client-go/rest"
33+
"k8s.io/klog/v2"
3434
)
3535

3636
func runTroubleshoot(v *viper.Viper, arg []string) error {
@@ -134,21 +134,21 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
134134
// Search cluster for Troubleshoot objects in cluster
135135
bundlesFromSecrets, err := specs.LoadFromSecretMatchingLabel(client, parsedSelector.String(), namespace, specs.SupportBundleKey)
136136
if err != nil {
137-
logger.Printf("failed to load support bundle spec from secrets: %s", err)
137+
klog.Errorf("failed to load support bundle spec from secrets: %s", err)
138138
}
139139
bundlesFromCluster = append(bundlesFromCluster, bundlesFromSecrets...)
140140

141141
bundlesFromConfigMaps, err := specs.LoadFromConfigMapMatchingLabel(client, parsedSelector.String(), namespace, specs.SupportBundleKey)
142142
if err != nil {
143-
logger.Printf("failed to load support bundle spec from secrets: %s", err)
143+
klog.Errorf("failed to load support bundle spec from secrets: %s", err)
144144
}
145145
bundlesFromCluster = append(bundlesFromCluster, bundlesFromConfigMaps...)
146146

147147
for _, bundle := range bundlesFromCluster {
148148
multidocs := strings.Split(string(bundle), "\n---\n")
149149
parsedBundleFromSecret, err := supportbundle.ParseSupportBundleFromDoc([]byte(multidocs[0]))
150150
if err != nil {
151-
logger.Printf("failed to parse support bundle spec: %s", err)
151+
klog.Errorf("failed to parse support bundle spec: %s", err)
152152
continue
153153
}
154154

@@ -160,7 +160,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
160160

161161
parsedRedactors, err := supportbundle.ParseRedactorsFromDocs(multidocs)
162162
if err != nil {
163-
logger.Printf("failed to parse redactors from doc: %s", err)
163+
klog.Errorf("failed to parse redactors from doc: %s", err)
164164
continue
165165
}
166166

@@ -172,21 +172,21 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
172172
// Search cluster for Troubleshoot objects in ConfigMaps
173173
redactorsFromSecrets, err := specs.LoadFromSecretMatchingLabel(client, parsedSelector.String(), namespace, specs.RedactorKey)
174174
if err != nil {
175-
logger.Printf("failed to load redactor specs from config maps: %s", err)
175+
klog.Errorf("failed to load redactor specs from config maps: %s", err)
176176
}
177177
redactorsFromCluster = append(redactorsFromCluster, redactorsFromSecrets...)
178178

179179
redactorsFromConfigMaps, err := specs.LoadFromConfigMapMatchingLabel(client, parsedSelector.String(), namespace, specs.RedactorKey)
180180
if err != nil {
181-
logger.Printf("failed to load redactor specs from config maps: %s", err)
181+
klog.Errorf("failed to load redactor specs from config maps: %s", err)
182182
}
183183
redactorsFromCluster = append(redactorsFromCluster, redactorsFromConfigMaps...)
184184

185185
for _, redactor := range redactorsFromCluster {
186186
multidocs := strings.Split(string(redactor), "\n---\n")
187187
parsedRedactors, err := supportbundle.ParseRedactorsFromDocs(multidocs)
188188
if err != nil {
189-
logger.Printf("failed to parse redactors from doc: %s", err)
189+
klog.Errorf("failed to parse redactors from doc: %s", err)
190190
}
191191

192192
additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, parsedRedactors...)
@@ -229,7 +229,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
229229
go func() {
230230
defer wg.Done()
231231
for msg := range progressChan {
232-
logger.Printf("Collecting support bundle: %v", msg)
232+
klog.Infof("Collecting support bundle: %v", msg)
233233
}
234234
}()
235235
} else {

cmd/util/util.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"net/url"
55
"os"
66
"strings"
7+
8+
"golang.org/x/text/cases"
9+
"golang.org/x/text/language"
710
)
811

912
func HomeDir() string {
@@ -23,7 +26,7 @@ func IsURL(str string) bool {
2326
}
2427

2528
func AppName(name string) string {
26-
words := strings.Split(strings.Title(strings.Replace(name, "-", " ", -1)), " ")
29+
words := strings.Split(cases.Title(language.English).String(strings.ReplaceAll(name, "-", " ")), " ")
2730
casedWords := []string{}
2831
for i, word := range words {
2932
if strings.ToLower(word) == "ai" {

internal/traces/otel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package traces
33
import (
44
"context"
55

6-
"github.com/replicatedhq/troubleshoot/pkg/logger"
76
"github.com/replicatedhq/troubleshoot/pkg/version"
87
"go.opentelemetry.io/otel"
98
"go.opentelemetry.io/otel/attribute"
109
"go.opentelemetry.io/otel/sdk/resource"
1110
"go.opentelemetry.io/otel/sdk/trace"
1211
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
12+
"k8s.io/klog/v2"
1313
)
1414

1515
// ConfigureTracing configures the OpenTelemetry trace provider for CLI
@@ -54,7 +54,7 @@ func ConfigureTracing(processName string) (func(), error) {
5454

5555
return func() {
5656
if err := tp.Shutdown(context.Background()); err != nil {
57-
logger.Printf("Failed to shutdown trace provider: %v", err)
57+
klog.Errorf("Failed to shutdown trace provider: %v", err)
5858
}
5959
}, nil
6060
}

0 commit comments

Comments
 (0)