Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch logging library from logrus to klog. #36

Merged
merged 1 commit into from May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/issues.go
Expand Up @@ -20,8 +20,8 @@ import (

"github.com/gocarina/gocsv"
"github.com/google/pullsheet/pkg/summary"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/google/pullsheet/pkg/client"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ func runIssues(rootOpts *rootOptions) error {
return err
}

logrus.Infof("%d bytes of issue output", len(out))
klog.Infof("%d bytes of issue output", len(out))
fmt.Print(out)

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/leaderboard.go
Expand Up @@ -20,8 +20,8 @@ import (
"strings"

"github.com/google/pullsheet/pkg/summary"
"k8s.io/klog/v2"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/google/pullsheet/pkg/client"
Expand Down Expand Up @@ -80,7 +80,7 @@ func runLeaderBoard(rootOpts *rootOptions) error {
return err
}

logrus.Infof("%d bytes of issue-comments output", len(out))
klog.Infof("%d bytes of issue-comments output", len(out))
fmt.Print(out)

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/prs.go
Expand Up @@ -20,8 +20,8 @@ import (

"github.com/gocarina/gocsv"
"github.com/google/pullsheet/pkg/summary"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/google/pullsheet/pkg/client"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ func runPRs(rootOpts *rootOptions) error {
return err
}

logrus.Infof("%d bytes of prs output", len(out))
klog.Infof("%d bytes of prs output", len(out))
fmt.Print(out)

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/review.go
Expand Up @@ -20,8 +20,8 @@ import (

"github.com/gocarina/gocsv"
"github.com/google/pullsheet/pkg/summary"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/google/pullsheet/pkg/client"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ func runReviews(rootOpts *rootOptions) error {
return err
}

logrus.Infof("%d bytes of reviews output", len(out))
klog.Infof("%d bytes of reviews output", len(out))
fmt.Print(out)

return nil
Expand Down
54 changes: 9 additions & 45 deletions cmd/root.go
Expand Up @@ -15,15 +15,14 @@
package cmd

import (
"fmt"
"strings"
"flag"
"time"

"github.com/karrick/tparse"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/klog/v2"
)

const dateForm = "2006-01-02"
Expand All @@ -46,7 +45,6 @@ type rootOptions struct {
untilParsed time.Time
title string
tokenPath string
logLevel string
branches []string
}

Expand All @@ -56,11 +54,15 @@ var rootOpts = &rootOptions{}
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
logrus.Fatal(err)
klog.Fatal(err)
}
}

func init() {
klog.InitFlags(nil)

rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)

rootCmd.PersistentFlags().StringSliceVar(
&rootOpts.repos,
"repos",
Expand Down Expand Up @@ -109,13 +111,6 @@ func init() {
"GitHub token path",
)

rootCmd.PersistentFlags().StringVar(
&rootOpts.logLevel,
"log-level",
"info",
fmt.Sprintf("the logging verbosity, either %s", levelNames()),
)

// Set up viper flag handling
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
panic(err)
Expand Down Expand Up @@ -149,9 +144,6 @@ func initRootOpts() error {
}

func initCommand(*cobra.Command, []string) error {
if err := setupGlobalLogger(rootOpts.logLevel); err != nil {
return err
}
if err := initRootOpts(); err != nil {
return err
}
Expand All @@ -162,7 +154,7 @@ func initCommand(*cobra.Command, []string) error {
if err == nil {
rootOpts.sinceParsed = t
} else {
logrus.Infof("%q not a duration: %v", rootOpts.since, err)
klog.Infof("%q not a duration: %v", rootOpts.since, err)
rootOpts.sinceParsed, err = time.Parse(dateForm, rootOpts.since)
if err != nil {
return errors.Wrap(err, "since time parse")
Expand All @@ -175,7 +167,7 @@ func initCommand(*cobra.Command, []string) error {
if err == nil {
rootOpts.untilParsed = t
} else {
logrus.Infof("%q not a duration: %v", rootOpts.until, err)
klog.Infof("%q not a duration: %v", rootOpts.until, err)
rootOpts.untilParsed, err = time.Parse(dateForm, rootOpts.until)
if err != nil {
return errors.Wrap(err, "until time parse")
Expand All @@ -185,31 +177,3 @@ func initCommand(*cobra.Command, []string) error {

return nil
}

// SetupGlobalLogger uses to provided log level string and applies it globally.
func setupGlobalLogger(level string) error {
logrus.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
ForceColors: true,
})

lvl, err := logrus.ParseLevel(level)
if err != nil {
return errors.Wrapf(err, "setting log level to %s", level)
}
logrus.SetLevel(lvl)
if lvl >= logrus.DebugLevel {
logrus.Debug("Setting commands globally into verbose mode")
}

logrus.Debugf("Using log level %q", lvl)
return nil
}

func levelNames() string {
levels := []string{}
for _, level := range logrus.AllLevels {
levels = append(levels, fmt.Sprintf("'%s'", level.String()))
}
return strings.Join(levels, ", ")
}
3 changes: 2 additions & 1 deletion go.mod
Expand Up @@ -10,8 +10,9 @@ require (
github.com/google/triage-party v0.0.0-20210325043323-fc6840b93022
github.com/karrick/tparse v2.4.2+incompatible
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
golang.org/x/oauth2 v0.0.0-20210323180902-22b0adad7558
k8s.io/klog v1.0.0 // indirect
k8s.io/klog/v2 v2.0.0 // indirect
)
5 changes: 3 additions & 2 deletions go.sum
Expand Up @@ -269,9 +269,8 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
Expand Down Expand Up @@ -602,6 +601,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
k8s.io/klog/v2 v2.0.0 h1:Foj74zO6RbjjP4hBEKjnYtjjAhGg4jNynUdYF6fJrok=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
Expand Down
12 changes: 6 additions & 6 deletions pkg/ghcache/ghcache.go
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/google/go-github/v33/github"
"github.com/google/triage-party/pkg/persist"
"github.com/sirupsen/logrus"
"k8s.io/klog/v2"
)

func PullRequestsGet(ctx context.Context, p persist.Cacher, c *github.Client, t time.Time, org string, project string, num int) (*github.PullRequest, error) {
Expand All @@ -33,15 +33,15 @@ func PullRequestsGet(ctx context.Context, p persist.Cacher, c *github.Client, t
}

if val == nil {
logrus.Debugf("cache miss for %v", key)
klog.Infof("cache miss for %v", key)
pr, _, err := c.PullRequests.Get(ctx, org, project, num)
if err != nil {
return nil, fmt.Errorf("get: %v", err)
}
return pr, p.Set(key, &persist.Blob{GHPullRequest: pr})
}

logrus.Debugf("cache hit: %v", key)
klog.Infof("cache hit: %v", key)
return val.GHPullRequest, nil
}

Expand All @@ -53,7 +53,7 @@ func PullRequestsListFiles(ctx context.Context, p persist.Cacher, c *github.Clie
return val.GHCommitFiles, nil
}

logrus.Debugf("cache miss for %v", key)
klog.Infof("cache miss for %v", key)

opts := &github.ListOptions{PerPage: 100}
fs := []*github.CommitFile{}
Expand Down Expand Up @@ -83,7 +83,7 @@ func PullRequestsListComments(ctx context.Context, p persist.Cacher, c *github.C
return val.GHPullRequestComments, nil
}

logrus.Debugf("cache miss for %v", key)
klog.Infof("cache miss for %v", key)

cs := []*github.PullRequestComment{}
opts := &github.PullRequestListCommentsOptions{
Expand Down Expand Up @@ -115,7 +115,7 @@ func IssuesGet(ctx context.Context, p persist.Cacher, c *github.Client, t time.T
return val.GHIssue, nil
}

logrus.Debugf("cache miss for %v", key)
klog.Infof("cache miss for %v", key)

i, _, err := c.Issues.Get(ctx, org, project, num)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions pkg/repo/files.go
Expand Up @@ -21,30 +21,30 @@ import (
"time"

"github.com/google/go-github/v33/github"
"github.com/sirupsen/logrus"
"k8s.io/klog/v2"

"github.com/google/pullsheet/pkg/client"
"github.com/google/pullsheet/pkg/ghcache"
)

// FilteredFiles returns a list of commit files that matter
func FilteredFiles(ctx context.Context, c *client.Client, t time.Time, org string, project string, num int) ([]*github.CommitFile, error) {
logrus.Infof("Fetching file list for #%d", num)
klog.Infof("Fetching file list for #%d", num)

changed, err := ghcache.PullRequestsListFiles(ctx, c.Cache, c.GitHubClient, t, org, project, num)
if err != nil {
return nil, err
}

logrus.Infof("%s/%s #%d had %d changed files", org, project, num, len(changed))
klog.Infof("%s/%s #%d had %d changed files", org, project, num, len(changed))

files := []*github.CommitFile{}
for _, cf := range changed {
if ignorePathRe.MatchString(cf.GetFilename()) {
logrus.Infof("ignoring %s", cf.GetFilename())
klog.Infof("ignoring %s", cf.GetFilename())
continue
}
logrus.Errorf("#%d changed: %s", num, cf.GetFilename())
klog.Errorf("#%d changed: %s", num, cf.GetFilename())

files = append(files, cf)
}
Expand All @@ -63,15 +63,15 @@ func prType(files []github.CommitFile) string {
if result == "" {
result = "docs"
}
logrus.Infof("%s: %s", f, result)
klog.Infof("%s: %s", f, result)
continue
}

if strings.Contains(f, "test") || strings.Contains(f, "integration") {
if result == "" {
result = "tests"
}
logrus.Infof("%s: %s", f, result)
klog.Infof("%s: %s", f, result)
continue
}

Expand All @@ -87,7 +87,7 @@ func prType(files []github.CommitFile) string {
result = "frontend"
}

logrus.Infof("%s (ext=%s): %s", f, ext, result)
klog.Infof("%s (ext=%s): %s", f, ext, result)
}

if result == "" {
Expand Down