8000 Add --verbosity flag to set log level verbosity (#214) · stern/stern@5327626 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 5327626

Browse files
author
Takashi Kusumi
authored
Add --verbosity flag to set log level verbosity (#214)
1 parent e03646c commit 5327626

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Supported Kubernetes resources are `pod`, `replicationcontroller`, `service`, `d
9595
`--template` | | Template to use for log lines, leave empty to use --output flag.
9696
`--timestamps`, `-t` | `false` | Print timestamps.
9797
`--timezone` | `Local` | Set timestamps to specific timezone.
98+
`--verbosity` | `0` | Number of the log level verbosity
9899
`--version`, `-v` | `false` | Print the version and exit.
99100
<!-- auto generated cli flags end --->
100101

@@ -138,6 +139,13 @@ functions](https://golang.org/pkg/text/template/#hdr-Functions)):
138139
| `extjson` | `string` | Parse the object as json and output colorized json |
139140
| `ppextjson` | `string` | Parse the object as json and output pretty-print colorized json |
140141

142+
### Log level verbosity
143+
144+
You can configure the log level verbosity by the `--verbose` flag.
145+
It is useful when you want to know how stern interacts with a Kubernetes API server in troubleshooting.
146+
147+
Increasing the verbosity increases the number of logs. `--verbose 6` would be a good starting point.
148+
141149
## Examples:
142150

143151
Tail the `gateway` container running inside of the `envvars` pod on staging

cmd/cmd.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ package cmd
1717
import (
1818
"context"
1919
"encoding/json"
20+
goflag "flag"
2021
"fmt"
2122
"regexp"
23+
"strconv"
2224
"strings"
2325
"text/template"
2426
"time"
2527

28+
"k8s.io/klog/v2"
29+
2630
"github.com/fatih/color"
2731
"github.com/pkg/errors"
2832
"github.com/spf13/cobra"
@@ -63,6 +67,7 @@ type options struct {
6367
podQuery string
6468
noFollow bool
6569
resource string
70+
verbosity int
6671
}
6772

6873
func NewOptions(streams genericclioptions.IOStreams) *options {
@@ -109,6 +114,10 @@ func (o *options) Validate() error {
109114
}
110115

111116
func (o *options) Run(cmd *cobra.Command) error {
117+
if err := o.setVerbosity(); err != nil {
118+
return err
119+
}
120+
112121
config, err := o.sternConfig()
113122
if err != nil {
114123
return err
@@ -328,6 +337,19 @@ func (o *options) sternConfig() (*stern.Config, error) {
328337
}, nil
329338
}
330339

340+
// setVerbosity sets the log level verbosity
341+
func (o *options) setVerbosity() error {
342+
if o.verbosity != 0 {
343+
// klog does not have an external method to set verbosity,
344+
// so we need to set it by a flag.
345+
// See https://github.com/kubernetes/klog/issues/336 for details
346+
var fs goflag.FlagSet
347+
klog.InitFlags(&fs)
348+
return fs.Set("v", strconv.Itoa(o.verbosity))
349+
}
350+
return nil
351+
}
352+
331353
// AddFlags adds all the flags used by stern.
332354
func (o *options) AddFlags(fs *pflag.FlagSet) {
333355
fs.BoolVarP(&o.allNamespaces, "all-namespaces", "A", o.allNamespaces, "If present, tail across all namespaces. A specific namespace is ignored even if specified with --namespace.")
@@ -356,6 +378,7 @@ func (o *options) AddFlags(fs *pflag.FlagSet) {
356378
fs.StringVar(&o.template, "template", o.template, "Template to use for log lines, leave empty to use --output flag.")
357379
fs.BoolVarP(&o.timestamps, "timestamps", "t", o.timestamps, "Print timestamps.")
358380
fs.StringVar(&o.timezone, "timezone", o.timezone, "Set timestamps to specific timezone.")
381+
fs.IntVar(&o.verbosity, "verbosity", o.verbosity, "Number of the log level verbosity")
359382
fs.BoolVarP(&o.version, "version", "v", o.version, "Print the version and exit.")
360383
}
361384

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
k8s.io/apimachinery v0.26.0
1414
k8s.io/cli-runtime v0.26.0
1515
k8s.io/client-go v0.26.0
16+
k8s.io/klog/v2 v2.80.1
1617
)
1718

1819
require (
@@ -61,7 +62,6 @@ require (
6162
gopkg.in/inf.v0 v0.9.1 // indirect
6263
gopkg.in/yaml.v2 v2.4.0 // indirect
6364
gopkg.in/yaml.v3 v3.0.1 // indirect
64-
k8s.io/klog/v2 v2.80.1 // indirect
6565
k8s.io/kube-openapi v0.0.0-20230109183929-3758b55a6596 // indirect
6666
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
6767
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect

0 commit comments

Comments
 (0)
0