Skip to content

Commit

Permalink
log: Replace instances of zapcore.Level with log.Level
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <[email protected]>
  • Loading branch information
justaugustus committed Jan 20, 2022
1 parent aa724d5 commit 8979e12
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 72 deletions.
4 changes: 2 additions & 2 deletions checks/binary_artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"testing"

"github.com/golang/mock/gomock"
"go.uber.org/zap/zapcore"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/clients/githubrepo"
"github.com/ossf/scorecard/v4/clients/localdir"
"github.com/ossf/scorecard/v4/log"
scut "github.com/ossf/scorecard/v4/utests"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ func TestBinaryArtifacts(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

logger, err := githubrepo.NewLogger(zapcore.DebugLevel)
logger, err := githubrepo.NewLogger(log.DebugLevel)
if err != nil {
t.Errorf("githubrepo.NewLogger: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,15 @@ func CreateGithubRepoClient(ctx context.Context, logger *log.Logger) clients.Rep
}

// NewLogger creates an instance of *log.Logger.
// TODO(log): Consider removing this function, as it only serves to wrap
// `log.NewLogger` for convenience.
func NewLogger(logLevel log.Level) (*log.Logger, error) {
return log.NewLogger(logLevel)
logger, err := log.NewLogger(logLevel)
if err != nil {
return nil, fmt.Errorf("creating GitHub repo client logger: %w", err)
}

return logger, nil
}

// CreateOssFuzzRepoClient returns a RepoClient implementation
Expand Down
21 changes: 11 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd
import (
"context"
"encoding/json"
goflag "flag"
"fmt"
"log"
"net/http"
Expand All @@ -28,7 +27,6 @@ import (
"time"

"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/checks"
Expand All @@ -48,8 +46,7 @@ var (
local string
checksToRun []string
metaData []string
// This one has to use goflag instead of pflag because it's defined by zap.
logLevel = zap.LevelFlag("verbosity", zap.InfoLevel, "override the default log level")
logLevel string
format string
npm string
pypi string
Expand Down Expand Up @@ -85,10 +82,14 @@ const cliEnableSarif = "ENABLE_SARIF"

//nolint:gochecknoinits
func init() {
// Add the zap flag manually
rootCmd.PersistentFlags().AddGoFlagSet(goflag.CommandLine)
rootCmd.Flags().StringVar(&repo, "repo", "", "repository to check")
rootCmd.Flags().StringVar(&local, "local", "", "local folder to check")
rootCmd.Flags().StringVar(
&logLevel,
"verbosity",
sclog.DefaultLevel.String(),
"set the log level",
)
rootCmd.Flags().StringVar(
&npm, "npm", "",
"npm package to check, given that the npm package has a GitHub repository")
Expand Down Expand Up @@ -187,7 +188,7 @@ func scorecardCmd(cmd *cobra.Command, args []string) {
}

ctx := context.Background()
logger, err := githubrepo.NewLogger(*logLevel)
logger, err := githubrepo.NewLogger(sclog.Level(logLevel))
if err != nil {
log.Panic(err)
}
Expand Down Expand Up @@ -250,15 +251,15 @@ func scorecardCmd(cmd *cobra.Command, args []string) {

switch format {
case formatDefault:
err = repoResult.AsString(showDetails, *logLevel, checkDocs, os.Stdout)
err = repoResult.AsString(showDetails, sclog.Level(logLevel), checkDocs, os.Stdout)
case formatSarif:
// TODO: support config files and update checker.MaxResultScore.
err = repoResult.AsSARIF(showDetails, *logLevel, os.Stdout, checkDocs, policy)
err = repoResult.AsSARIF(showDetails, sclog.Level(logLevel), os.Stdout, checkDocs, policy)
case formatJSON:
if raw {
err = repoResult.AsRawJSON(os.Stdout)
} else {
err = repoResult.AsJSON2(showDetails, *logLevel, checkDocs, os.Stdout)
err = repoResult.AsJSON2(showDetails, sclog.Level(logLevel), checkDocs, os.Stdout)
}

default:
Expand Down
5 changes: 3 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ossf/scorecard/v4/checks"
"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/clients/githubrepo"
sclog "github.com/ossf/scorecard/v4/log"
"github.com/ossf/scorecard/v4/pkg"
)

Expand All @@ -40,7 +41,7 @@ var serveCmd = &cobra.Command{
Short: "Serve the scorecard program over http",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
logger, err := githubrepo.NewLogger(*logLevel)
logger, err := githubrepo.NewLogger(sclog.Level(logLevel))
if err != nil {
log.Fatalf("unable to construct logger: %v", err)
}
Expand Down Expand Up @@ -81,7 +82,7 @@ var serveCmd = &cobra.Command{
}

if r.Header.Get("Content-Type") == "application/json" {
if err := repoResult.AsJSON(showDetails, *logLevel, rw); err != nil {
if err := repoResult.AsJSON(showDetails, sclog.Level(logLevel), rw); err != nil {
sugar.Error(err)
rw.WriteHeader(http.StatusInternalServerError)
}
Expand Down
7 changes: 3 additions & 4 deletions cron/format/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import (
// nolint:gosec
_ "net/http/pprof"

"go.uber.org/zap/zapcore"

docs "github.com/ossf/scorecard/v4/docs/checks"
sce "github.com/ossf/scorecard/v4/errors"
"github.com/ossf/scorecard/v4/log"
"github.com/ossf/scorecard/v4/pkg"
)

Expand Down Expand Up @@ -87,7 +86,7 @@ type jsonScorecardResultV2 struct {
}

// AsJSON exports results as JSON for new detail format.
func AsJSON(r *pkg.ScorecardResult, showDetails bool, logLevel zapcore.Level, writer io.Writer) error {
func AsJSON(r *pkg.ScorecardResult, showDetails bool, logLevel log.Level, writer io.Writer) error {
encoder := json.NewEncoder(writer)

out := jsonScorecardResult{
Expand Down Expand Up @@ -123,7 +122,7 @@ func AsJSON(r *pkg.ScorecardResult, showDetails bool, logLevel zapcore.Level, wr

// AsJSON2 exports results as JSON for the cron job and in the new detail format.
func AsJSON2(r *pkg.ScorecardResult, showDetails bool,
logLevel zapcore.Level, checkDocs docs.Doc, writer io.Writer) error {
logLevel log.Level, checkDocs docs.Doc, writer io.Writer) error {
score, err := r.GetAggregateScore(checkDocs)
if err != nil {
//nolint:wrapcheck
Expand Down
16 changes: 8 additions & 8 deletions cron/format/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"time"

"github.com/xeipuuv/gojsonschema"
"go.uber.org/zap/zapcore"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/log"
"github.com/ossf/scorecard/v4/pkg"
)

Expand Down Expand Up @@ -84,14 +84,14 @@ func TestJSONOutput(t *testing.T) {
name string
expected string
showDetails bool
logLevel zapcore.Level
logLevel log.Level
result pkg.ScorecardResult
}{
{
name: "check-1",
showDetails: true,
expected: "./testdata/check1.json",
logLevel: zapcore.DebugLevel,
logLevel: log.DebugLevel,
result: pkg.ScorecardResult{
Repo: pkg.RepoInfo{
Name: repoName,
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestJSONOutput(t *testing.T) {
name: "check-2",
showDetails: true,
expected: "./testdata/check2.json",
logLevel: zapcore.DebugLevel,
logLevel: log.DebugLevel,
result: pkg.ScorecardResult{
Repo: pkg.RepoInfo{
Name: repoName,
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestJSONOutput(t *testing.T) {
name: "check-3",
showDetails: true,
expected: "./testdata/check3.json",
logLevel: zapcore.InfoLevel,
logLevel: log.InfoLevel,
result: pkg.ScorecardResult{
Repo: pkg.RepoInfo{
Name: repoName,
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestJSONOutput(t *testing.T) {
name: "check-4",
showDetails: true,
expected: "./testdata/check4.json",
logLevel: zapcore.DebugLevel,
logLevel: log.DebugLevel,
result: pkg.ScorecardResult{
Repo: pkg.RepoInfo{
Name: repoName,
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestJSONOutput(t *testing.T) {
name: "check-5",
showDetails: true,
expected: "./testdata/check5.json",
logLevel: zapcore.WarnLevel,
logLevel: log.WarnLevel,
result: pkg.ScorecardResult{
Repo: pkg.RepoInfo{
Name: repoName,
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestJSONOutput(t *testing.T) {
name: "check-6",
showDetails: true,
expected: "./testdata/check6.json",
logLevel: zapcore.WarnLevel,
logLevel: log.WarnLevel,
result: pkg.ScorecardResult{
Repo: pkg.RepoInfo{
Name: repoName,
Expand Down
5 changes: 2 additions & 3 deletions cron/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
_ "net/http/pprof"

"go.opencensus.io/stats/view"
"go.uber.org/zap/zapcore"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/checks"
Expand Down Expand Up @@ -107,11 +106,11 @@ func processRequest(ctx context.Context,
logger.Zap.Warn(errorMsg)
}
result.Date = batchRequest.GetJobTime().AsTime()
if err := format.AsJSON(&result, true /*showDetails*/, zapcore.InfoLevel, &buffer); err != nil {
if err := format.AsJSON(&result, true /*showDetails*/, log.InfoLevel, &buffer); err != nil {
return fmt.Errorf("error during result.AsJSON: %w", err)
}

if err := format.AsJSON2(&result, true /*showDetails*/, zapcore.InfoLevel, checkDocs, &buffer2); err != nil {
if err := format.AsJSON2(&result, true /*showDetails*/, log.InfoLevel, checkDocs, &buffer2); err != nil {
return fmt.Errorf("error during result.AsJSON2: %w", err)
}
}
Expand Down
19 changes: 12 additions & 7 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@ type Level string
// TODO(log): Revisit if all levels are required. The current list mimics zap
// log levels.
const (
DebugLevel Level = "debug"
InfoLevel Level = "info"
WarnLevel Level = "warn"
ErrorLevel Level = "error"
DPanicLevel Level = "dpanic"
PanicLevel Level = "panic"
FatalLevel Level = "fatal"
DefaultLevel = InfoLevel
DebugLevel Level = "debug"
InfoLevel Level = "info"
WarnLevel Level = "warn"
ErrorLevel Level = "error"
DPanicLevel Level = "dpanic"
PanicLevel Level = "panic"
FatalLevel Level = "fatal"
)

func (l Level) String() string {
return string(l)
}

// parseLogLevelZap parses a log level string and returning a zapcore.Level,
// which defaults to `zapcore.InfoLevel` when the provided string is not
// recognized.
Expand Down
11 changes: 5 additions & 6 deletions pkg/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ import (
"fmt"
"strings"

"go.uber.org/zap/zapcore"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/log"
)

func textToMarkdown(s string) string {
return strings.ReplaceAll(s, "\n", "\n\n")
}

// DetailToString turns a detail information into a string.
func DetailToString(d *checker.CheckDetail, logLevel zapcore.Level) string {
func DetailToString(d *checker.CheckDetail, logLevel log.Level) string {
// UPGRADEv3: remove switch statement.
switch d.Msg.Version {
case 3:
if d.Type == checker.DetailDebug && logLevel != zapcore.DebugLevel {
if d.Type == checker.DetailDebug && logLevel != log.DebugLevel {
return ""
}
switch {
Expand All @@ -46,14 +45,14 @@ func DetailToString(d *checker.CheckDetail, logLevel zapcore.Level) string {
return fmt.Sprintf("%s: %s", typeToString(d.Type), d.Msg.Text)
}
default:
if d.Type == checker.DetailDebug && logLevel != zapcore.DebugLevel {
if d.Type == checker.DetailDebug && logLevel != log.DebugLevel {
return ""
}
return fmt.Sprintf("%s: %s", typeToString(d.Type), d.Msg.Text)
}
}

func detailsToString(details []checker.CheckDetail, logLevel zapcore.Level) (string, bool) {
func detailsToString(details []checker.CheckDetail, logLevel log.Level) (string, bool) {
// UPGRADEv2: change to make([]string, len(details)).
var sa []string
for i := range details {
Expand Down
7 changes: 3 additions & 4 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (
"fmt"
"io"

"go.uber.org/zap/zapcore"

docs "github.com/ossf/scorecard/v4/docs/checks"
sce "github.com/ossf/scorecard/v4/errors"
"github.com/ossf/scorecard/v4/log"
)

//nolint
Expand Down Expand Up @@ -83,7 +82,7 @@ type jsonScorecardResultV2 struct {
}

// AsJSON exports results as JSON for new detail format.
func (r *ScorecardResult) AsJSON(showDetails bool, logLevel zapcore.Level, writer io.Writer) error {
func (r *ScorecardResult) AsJSON(showDetails bool, logLevel log.Level, writer io.Writer) error {
encoder := json.NewEncoder(writer)

out := jsonScorecardResult{
Expand Down Expand Up @@ -119,7 +118,7 @@ func (r *ScorecardResult) AsJSON(showDetails bool, logLevel zapcore.Level, write

// AsJSON2 exports results as JSON for new detail format.
func (r *ScorecardResult) AsJSON2(showDetails bool,
logLevel zapcore.Level, checkDocs docs.Doc, writer io.Writer) error {
logLevel log.Level, checkDocs docs.Doc, writer io.Writer) error {
score, err := r.GetAggregateScore(checkDocs)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 8979e12

Please sign in to comment.