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

⚠️ Create a dedicated logging package to encapsulate calls to zap #1502

Merged
merged 6 commits into from
Jan 20, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 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,13 +60,13 @@ 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)
}

// nolint
defer logger.Sync()
defer logger.Zap.Sync()

ctrl := gomock.NewController(t)
repo, err := localdir.MakeLocalDirRepo(tt.inputFolder)
Expand Down
6 changes: 3 additions & 3 deletions checks/license_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 @@ -142,13 +142,13 @@ func TestLicenseFileSubdirectory(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)
}

// nolint
defer logger.Sync()
defer logger.Zap.Sync()

ctrl := gomock.NewController(t)
repo, err := localdir.MakeLocalDirRepo(tt.inputFolder)
Expand Down
5 changes: 2 additions & 3 deletions checks/raw/security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"fmt"
"strings"

"go.uber.org/zap"

"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/checks/fileparser"
"github.com/ossf/scorecard/v4/clients/githubrepo"
sce "github.com/ossf/scorecard/v4/errors"
"github.com/ossf/scorecard/v4/log"
)

// SecurityPolicy checks for presence of security policy.
Expand Down Expand Up @@ -71,7 +70,7 @@ func SecurityPolicy(c *checker.CheckRequest) (checker.SecurityPolicyData, error)
}

// https://docs.github.com/en/github/building-a-strong-community/creating-a-default-community-health-file.
logger, err := githubrepo.NewLogger(zap.InfoLevel)
logger, err := githubrepo.NewLogger(log.InfoLevel)
if err != nil {
return checker.SecurityPolicyData{}, fmt.Errorf("%w", err)
}
Expand Down
22 changes: 11 additions & 11 deletions clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import (

"github.com/google/go-github/v38/github"
"github.com/shurcooL/githubv4"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/clients/githubrepo/roundtripper"
sce "github.com/ossf/scorecard/v4/errors"
"github.com/ossf/scorecard/v4/log"
)

var errInputRepoType = errors.New("input repo should be of type repoURL")
Expand Down Expand Up @@ -180,9 +179,9 @@ func (client *Client) Close() error {
}

// CreateGithubRepoClient returns a Client which implements RepoClient interface.
func CreateGithubRepoClient(ctx context.Context, logger *zap.Logger) clients.RepoClient {
func CreateGithubRepoClient(ctx context.Context, logger *log.Logger) clients.RepoClient {
// Use our custom roundtripper
rt := roundtripper.NewTransport(ctx, logger.Sugar())
rt := roundtripper.NewTransport(ctx, logger.Zap.Sugar())
httpClient := &http.Client{
Transport: rt,
}
Expand Down Expand Up @@ -220,20 +219,21 @@ func CreateGithubRepoClient(ctx context.Context, logger *zap.Logger) clients.Rep
}
}

// NewLogger creates an instance of *zap.Logger.
func NewLogger(logLevel zapcore.Level) (*zap.Logger, error) {
cfg := zap.NewProductionConfig()
cfg.Level.SetLevel(logLevel)
logger, err := cfg.Build()
// 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) {
logger, err := log.NewLogger(logLevel)
if err != nil {
return nil, fmt.Errorf("cfg.Build: %w", err)
return nil, fmt.Errorf("creating GitHub repo client logger: %w", err)
}

return logger, nil
}

// CreateOssFuzzRepoClient returns a RepoClient implementation
// intialized to `google/oss-fuzz` GitHub repository.
func CreateOssFuzzRepoClient(ctx context.Context, logger *zap.Logger) (clients.RepoClient, error) {
func CreateOssFuzzRepoClient(ctx context.Context, logger *log.Logger) (clients.RepoClient, error) {
ossFuzzRepo, err := MakeGithubRepo("google/oss-fuzz")
if err != nil {
return nil, fmt.Errorf("error during githubrepo.MakeGithubRepo: %w", err)
Expand Down
7 changes: 3 additions & 4 deletions clients/localdir/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ import (
"strings"
"sync"

"go.uber.org/zap"

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

var errInputRepoType = errors.New("input repo should be of type repoLocal")

//nolint:govet
type localDirClient struct {
logger *zap.Logger
logger *log.Logger
ctx context.Context
path string
once sync.Once
Expand Down Expand Up @@ -214,7 +213,7 @@ func (client *localDirClient) Close() error {
}

// CreateLocalDirClient returns a client which implements RepoClient interface.
func CreateLocalDirClient(ctx context.Context, logger *zap.Logger) clients.RepoClient {
func CreateLocalDirClient(ctx context.Context, logger *log.Logger) clients.RepoClient {
return &localDirClient{
ctx: ctx,
logger: logger,
Expand Down
6 changes: 3 additions & 3 deletions clients/localdir/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"go.uber.org/zap/zapcore"

"github.com/ossf/scorecard/v4/clients/githubrepo"
"github.com/ossf/scorecard/v4/log"
)

func TestClient_CreationAndCaching(t *testing.T) {
Expand Down Expand Up @@ -63,12 +63,12 @@ func TestClient_CreationAndCaching(t *testing.T) {
t.Parallel()

ctx := context.Background()
logger, err := githubrepo.NewLogger(zapcore.DebugLevel)
logger, err := githubrepo.NewLogger(log.DebugLevel)
if err != nil {
t.Errorf("githubrepo.NewLogger: %v", err)
}
// nolint
defer logger.Sync() // Flushes buffer, if any.
defer logger.Zap.Sync() // Flushes buffer, if any.

// Create repo.
repo, err := MakeLocalDirRepo(tt.inputFolder)
Expand Down
26 changes: 14 additions & 12 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 @@ -37,6 +35,7 @@ import (
"github.com/ossf/scorecard/v4/clients/localdir"
docs "github.com/ossf/scorecard/v4/docs/checks"
sce "github.com/ossf/scorecard/v4/errors"
sclog "github.com/ossf/scorecard/v4/log"
"github.com/ossf/scorecard/v4/pkg"
spol "github.com/ossf/scorecard/v4/policy"
)
Expand All @@ -47,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 @@ -84,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 @@ -186,12 +188,12 @@ 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)
}
// nolint: errcheck
defer logger.Sync() // Flushes buffer, if any.
defer logger.Zap.Sync() // Flushes buffer, if any.

repoURI, repoClient, ossFuzzRepoClient, ciiClient, vulnsClient, repoType, err := getRepoAccessors(ctx, uri, logger)
if err != nil {
Expand Down Expand Up @@ -249,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 Expand Up @@ -413,7 +415,7 @@ func validateFormat(format string) bool {
}
}

func getRepoAccessors(ctx context.Context, uri string, logger *zap.Logger) (
func getRepoAccessors(ctx context.Context, uri string, logger *sclog.Logger) (
repo clients.Repo,
repoClient clients.RepoClient,
ossFuzzRepoClient clients.RepoClient,
Expand Down
9 changes: 5 additions & 4 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,13 +41,13 @@ 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)
}
//nolint
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
defer logger.Zap.Sync() // flushes buffer, if any
sugar := logger.Zap.Sugar()
t, err := template.New("webpage").Parse(tpl)
if err != nil {
sugar.Panic(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
Loading