Skip to content

Commit

Permalink
log: Express log levels
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 60cebcc commit aa724d5
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 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,7 +142,7 @@ 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)
}
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
3 changes: 1 addition & 2 deletions clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

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

"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/clients/githubrepo/roundtripper"
Expand Down Expand Up @@ -221,7 +220,7 @@ func CreateGithubRepoClient(ctx context.Context, logger *log.Logger) clients.Rep
}

// NewLogger creates an instance of *log.Logger.
func NewLogger(logLevel zapcore.Level) (*log.Logger, error) {
func NewLogger(logLevel log.Level) (*log.Logger, error) {
return log.NewLogger(logLevel)
}

Expand Down
4 changes: 2 additions & 2 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,7 +63,7 @@ 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)
}
Expand Down
3 changes: 1 addition & 2 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"
"go.uber.org/zap/zapcore"

"github.com/ossf/scorecard/v4/checker"
Expand Down Expand Up @@ -187,7 +186,7 @@ func main() {
panic(err)
}

logger, err := githubrepo.NewLogger(zap.InfoLevel)
logger, err := githubrepo.NewLogger(log.InfoLevel)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/zap"

"github.com/ossf/scorecard/v4/clients/githubrepo"
"github.com/ossf/scorecard/v4/log"
Expand All @@ -42,7 +41,7 @@ var _ = BeforeSuite(func() {
"GITHUB_AUTH_TOKEN env variable is not set.The GITHUB_AUTH_TOKEN env variable has to be set to run e2e test.")
Expect(len(token)).ShouldNot(BeZero(), "Length of the GITHUB_AUTH_TOKEN env variable is zero.")

l, err := githubrepo.NewLogger(zap.InfoLevel)
l, err := githubrepo.NewLogger(log.InfoLevel)
Expect(err).Should(BeNil())
logger = l
})
Expand Down
18 changes: 16 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ type Logger struct {

// NewLogger creates an instance of *zap.Logger.
// Copied from clients/githubrepo/client.go
func NewLogger(logLevel zapcore.Level) (*Logger, error) {
func NewLogger(logLevel Level) (*Logger, error) {
zapCfg := zap.NewProductionConfig()
zapCfg.Level.SetLevel(logLevel)
zapLevel := parseLogLevelZap(string(logLevel))
zapCfg.Level.SetLevel(zapLevel)

zapLogger, err := zapCfg.Build()
if err != nil {
Expand All @@ -47,6 +48,19 @@ func NewLogger(logLevel zapcore.Level) (*Logger, error) {
// a parameter, in lieu of defined types in upstream logging packages.
type Level string

// Log levels
// 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"
)

// 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

0 comments on commit aa724d5

Please sign in to comment.