Skip to content

Commit

Permalink
introduce log/slog
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Jan 23, 2024
1 parent 553583e commit d69c2c9
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions cmd/reviewdog/main.go
Expand Up @@ -7,7 +7,7 @@ import (
"flag"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -61,6 +61,7 @@ type option struct {
tee bool
filterMode filter.Mode
failOnError bool
logLevel string
}

const (
Expand Down Expand Up @@ -192,6 +193,7 @@ const (
$ export CI_REPO_NAME="reviewdog" # repository name
`
failOnErrorDoc = `Returns 1 as exit code if any errors/warnings found in input`
logLevelDoc = `log level for reviewdog itself. (debug, info, warning, error)`
)

var opt = &option{}
Expand All @@ -213,6 +215,7 @@ func init() {
flag.BoolVar(&opt.tee, "tee", false, teeDoc)
flag.Var(&opt.filterMode, "filter-mode", filterModeDoc)
flag.BoolVar(&opt.failOnError, "fail-on-error", false, failOnErrorDoc)
flag.StringVar(&opt.logLevel, "log-level", "info", logLevelDoc)
}

func usage() {
Expand All @@ -224,6 +227,29 @@ func usage() {
os.Exit(2)
}

func configureLogger(ctx context.Context, logLevel string) {
var lv slog.Level
switch logLevel {
case "debug":
lv = slog.LevelDebug
case "info":
lv = slog.LevelInfo
case "warning":
lv = slog.LevelWarn
case "error":
lv = slog.LevelError

Check warning on line 240 in cmd/reviewdog/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/reviewdog/main.go#L233-L240

Added lines #L233 - L240 were not covered by tests
default:
slog.WarnContext(ctx, "unknown log level", "log-level", logLevel)
}

opts := &slog.HandlerOptions{
Level: lv,
}
h := slog.NewTextHandler(os.Stderr, opts)
logger := slog.New(h)
slog.SetDefault(logger)
}

func main() {
flag.Usage = usage
flag.Parse()
Expand All @@ -236,6 +262,8 @@ func main() {
func run(r io.Reader, w io.Writer, opt *option) error {
ctx := context.Background()

configureLogger(ctx, opt.logLevel)

if opt.version {
fmt.Fprintln(w, commands.Version)
return nil
Expand Down Expand Up @@ -362,7 +390,7 @@ func run(r io.Reader, w io.Writer, opt *option) error {
// filtering of annotations dividing them in two groups:
// - This pull request (10)
// - All (50)
log.Printf("reviewdog: [bitbucket-code-report] supports only with filter.ModeNoFilter for now")
slog.WarnContext(ctx, "reviewdog: [bitbucket-code-report] supports only with filter.ModeNoFilter for now")

Check warning on line 393 in cmd/reviewdog/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/reviewdog/main.go#L393

Added line #L393 was not covered by tests
}
opt.filterMode = filter.ModeNoFilter
ds = &reviewdog.EmptyDiff{}
Expand All @@ -372,7 +400,7 @@ func run(r io.Reader, w io.Writer, opt *option) error {
return err
}
if !isPR {
fmt.Fprintln(os.Stderr, "reviewdog: this is not PullRequest build.")
slog.ErrorContext(ctx, "reviewdog: this is not PullRequest build.")

Check warning on line 403 in cmd/reviewdog/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/reviewdog/main.go#L403

Added line #L403 was not covered by tests
return nil
}
cs = reviewdog.MultiCommentService(gs, cs)
Expand Down

0 comments on commit d69c2c9

Please sign in to comment.