Skip to content

Commit 8127c87

Browse files
committed
cmd/vulnreport: move all logging logic to one file
Move all logic for vulnreport logging into a new inner package, log, which will allow us to more easily change the logging internals (e.g., if we want to use slog in the future). Change-Id: I8287fc186451e6dc2e846ed071bd3c6fefdad359 Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/559600 Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 2c6db71 commit 8127c87

13 files changed

+137
-79
lines changed

cmd/vulnreport/create.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strconv"
1313
"strings"
1414

15+
"golang.org/x/vulndb/cmd/vulnreport/log"
1516
"golang.org/x/vulndb/internal/cveclient"
1617
"golang.org/x/vulndb/internal/cveschema5"
1718
"golang.org/x/vulndb/internal/derrors"
@@ -51,11 +52,11 @@ func create(ctx context.Context, issueNumber int, cfg *createCfg) (err error) {
5152
return err
5253
}
5354

54-
outlog.Println(filename)
55+
log.Out(filename)
5556

5657
xrefs := xref(filename, r, cfg.existingByFile)
5758
if len(xrefs) != 0 {
58-
infolog.Printf("found cross-references:\n%s", xrefs)
59+
log.Infof("found cross-references:\n%s", xrefs)
5960
}
6061

6162
return nil
@@ -75,21 +76,21 @@ func createExcluded(ctx context.Context, cfg *createCfg) (err error) {
7576
if err != nil {
7677
return err
7778
}
78-
infolog.Printf("found %d issues with label %s\n", len(tempIssues), label)
79+
log.Infof("found %d issues with label %s\n", len(tempIssues), label)
7980
isses = append(isses, tempIssues...)
8081
}
8182

8283
var created []string
8384
for _, iss := range isses {
8485
// Don't create a report for an issue that already has a report.
8586
if _, ok := cfg.existingByIssue[iss.Number]; ok {
86-
infolog.Printf("skipped issue %d which already has a report\n", iss.Number)
87+
log.Infof("skipped issue %d which already has a report\n", iss.Number)
8788
continue
8889
}
8990

9091
r, err := createReport(ctx, cfg, iss)
9192
if err != nil {
92-
errlog.Printf("skipped issue %d: %v\n", iss.Number, err)
93+
log.Errf("skipped issue %d: %v\n", iss.Number, err)
9394
continue
9495
}
9596

@@ -103,11 +104,11 @@ func createExcluded(ctx context.Context, cfg *createCfg) (err error) {
103104

104105
skipped := len(isses) - len(created)
105106
if skipped > 0 {
106-
infolog.Printf("skipped %d issue(s)\n", skipped)
107+
log.Infof("skipped %d issue(s)\n", skipped)
107108
}
108109

109110
if len(created) == 0 {
110-
infolog.Printf("no files to commit, exiting")
111+
log.Infof("no files to commit, exiting")
111112
return nil
112113
}
113114

@@ -219,13 +220,13 @@ func createReport(ctx context.Context, cfg *createCfg, iss *issues.Issue) (r *re
219220

220221
aliases := allAliases(ctx, parsed.aliases, cfg.ghsaClient)
221222
if alias, ok := pickBestAlias(aliases, *preferCVE); ok {
222-
infolog.Printf("creating report %s based on %s (picked from [%s])", parsed.id, alias, strings.Join(aliases, ", "))
223+
log.Infof("creating report %s based on %s (picked from [%s])", parsed.id, alias, strings.Join(aliases, ", "))
223224
r, err = reportFromAlias(ctx, parsed.id, parsed.modulePath, alias, cfg)
224225
if err != nil {
225226
return nil, err
226227
}
227228
} else {
228-
infolog.Printf("no alias found, creating basic report for %s", parsed.id)
229+
log.Infof("no alias found, creating basic report for %s", parsed.id)
229230
r = &report.Report{
230231
ID: parsed.id,
231232
Modules: []*report.Module{
@@ -258,11 +259,11 @@ func createReport(ctx context.Context, cfg *createCfg, iss *issues.Issue) (r *re
258259
if cfg.aiClient != nil {
259260
suggestions, err := suggest(ctx, cfg.aiClient, r, 1)
260261
if err != nil {
261-
warnlog.Printf("failed to get AI-generated suggestions for %s: %v\n", r.ID, err)
262+
log.Warnf("failed to get AI-generated suggestions for %s: %v\n", r.ID, err)
262263
} else if len(suggestions) == 0 {
263-
warnlog.Printf("failed to get AI-generated suggestions for %s (none generated)\n", r.ID)
264+
log.Warnf("failed to get AI-generated suggestions for %s (none generated)\n", r.ID)
264265
} else {
265-
infolog.Printf("applying AI-generated suggestion for %s", r.ID)
266+
log.Infof("applying AI-generated suggestion for %s", r.ID)
266267
applySuggestion(r, suggestions[0])
267268
}
268269
}
@@ -313,7 +314,7 @@ func parseGithubIssue(iss *issues.Issue, pc *proxy.Client, allowClosed bool) (*p
313314
}
314315

315316
if len(parsed.aliases) == 0 {
316-
infolog.Printf("%q has no CVE or GHSA IDs\n", iss.Title)
317+
log.Infof("%q has no CVE or GHSA IDs\n", iss.Title)
317318
}
318319

319320
return parsed, nil
@@ -373,13 +374,13 @@ func reportFromAlias(ctx context.Context, id, modulePath, alias string, cfg *cre
373374
if err != nil {
374375
// If a CVE is not found, it is most likely a CVE we reserved but haven't
375376
// published yet.
376-
infolog.Printf("no published record found for %s, creating basic report", alias)
377+
log.Infof("no published record found for %s, creating basic report", alias)
377378
return basicReport(id, modulePath), nil
378379
}
379380
return report.CVE5ToReport(cve, id, modulePath, cfg.proxyClient), nil
380381
}
381382

382-
infolog.Printf("alias %s is not a CVE or GHSA, creating basic report", alias)
383+
log.Infof("alias %s is not a CVE or GHSA, creating basic report", alias)
383384
return basicReport(id, modulePath), nil
384385
}
385386

cmd/vulnreport/cve.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
88
"context"
99

10+
"golang.org/x/vulndb/cmd/vulnreport/log"
1011
"golang.org/x/vulndb/internal/database"
1112
"golang.org/x/vulndb/internal/derrors"
1213
"golang.org/x/vulndb/internal/report"
@@ -22,7 +23,7 @@ func cveCmd(_ context.Context, filename string) (err error) {
2223
if err := writeCVE(r); err != nil {
2324
return err
2425
}
25-
outlog.Println(r.CVEFilename())
26+
log.Out(r.CVEFilename())
2627
}
2728
return nil
2829
}

cmd/vulnreport/find_aliases.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010

1111
"golang.org/x/exp/slices"
12+
"golang.org/x/vulndb/cmd/vulnreport/log"
1213
"golang.org/x/vulndb/internal/cveschema5"
1314
"golang.org/x/vulndb/internal/ghsa"
1415
"golang.org/x/vulndb/internal/report"
@@ -68,7 +69,7 @@ func aliasesBFS(ctx context.Context, knownAliases []string,
6869
all = append(all, alias)
6970
aliases, err := aliasesFor(ctx, alias)
7071
if err != nil {
71-
errlog.Printf(err.Error())
72+
log.Err(err)
7273
continue
7374
}
7475
queue = append(queue, aliases...)

cmd/vulnreport/fix.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/google/go-cmp/cmp"
1616
"golang.org/x/exp/slices"
17+
"golang.org/x/vulndb/cmd/vulnreport/log"
1718
"golang.org/x/vulndb/internal/derrors"
1819
"golang.org/x/vulndb/internal/ghsa"
1920
"golang.org/x/vulndb/internal/osvutils"
@@ -30,7 +31,7 @@ var (
3031

3132
func fix(ctx context.Context, filename string, ghsaClient *ghsa.Client, pc *proxy.Client, force bool) (err error) {
3233
defer derrors.Wrap(&err, "fix(%q)", filename)
33-
infolog.Printf("fix %s\n", filename)
34+
log.Infof("fix %s\n", filename)
3435

3536
r, err := report.Read(filename)
3637
if err != nil {
@@ -44,27 +45,27 @@ func fix(ctx context.Context, filename string, ghsaClient *ghsa.Client, pc *prox
4445
// report even if a fatal error occurs somewhere.
4546
defer func() {
4647
if err := r.Write(filename); err != nil {
47-
errlog.Println(err)
48+
log.Err(err)
4849
}
4950
}()
5051

5152
if lints := r.Lint(pc); force || len(lints) > 0 {
5253
r.Fix(pc)
5354
}
5455
if lints := r.Lint(pc); len(lints) > 0 {
55-
warnlog.Printf("%s still has lint errors after fix:\n\t- %s", filename, strings.Join(lints, "\n\t- "))
56+
log.Warnf("%s still has lint errors after fix:\n\t- %s", filename, strings.Join(lints, "\n\t- "))
5657
}
5758

5859
if !*skipSymbols {
59-
infolog.Printf("%s: checking packages and symbols (use -skip-symbols to skip this)", r.ID)
60+
log.Infof("%s: checking packages and symbols (use -skip-symbols to skip this)", r.ID)
6061
if err := checkReportSymbols(r); err != nil {
6162
return err
6263
}
6364
}
6465
if !*skipAlias {
65-
infolog.Printf("%s: checking for missing GHSAs and CVEs (use -skip-alias to skip this)", r.ID)
66+
log.Infof("%s: checking for missing GHSAs and CVEs (use -skip-alias to skip this)", r.ID)
6667
if added := addMissingAliases(ctx, r, ghsaClient); added > 0 {
67-
infolog.Printf("%s: added %d missing aliases", r.ID, added)
68+
log.Infof("%s: added %d missing aliases", r.ID, added)
6869
}
6970
}
7071

@@ -85,7 +86,7 @@ func fix(ctx context.Context, filename string, ghsaClient *ghsa.Client, pc *prox
8586

8687
func checkReportSymbols(r *report.Report) error {
8788
if r.IsExcluded() {
88-
infolog.Printf("%s is excluded, skipping symbol checks\n", r.ID)
89+
log.Infof("%s is excluded, skipping symbol checks\n", r.ID)
8990
return nil
9091
}
9192
for _, m := range r.Modules {
@@ -100,28 +101,28 @@ func checkReportSymbols(r *report.Report) error {
100101
return err
101102
}
102103
if ver == "" || !affected {
103-
warnlog.Printf("%s: current Go version %q is not in a vulnerable range, skipping symbol checks for module %s\n", r.ID, gover, m.Module)
104+
log.Warnf("%s: current Go version %q is not in a vulnerable range, skipping symbol checks for module %s\n", r.ID, gover, m.Module)
104105
continue
105106
}
106107
if ver != m.VulnerableAt {
107-
warnlog.Printf("%s: current Go version %q does not match vulnerable_at version (%s) for module %s\n", r.ID, ver, m.VulnerableAt, m.Module)
108+
log.Warnf("%s: current Go version %q does not match vulnerable_at version (%s) for module %s\n", r.ID, ver, m.VulnerableAt, m.Module)
108109
}
109110
}
110111

111112
for _, p := range m.Packages {
112113
if p.SkipFix != "" {
113-
infolog.Printf("%s: skipping symbol checks for package %s (reason: %q)\n", r.ID, p.Package, p.SkipFix)
114+
log.Infof("%s: skipping symbol checks for package %s (reason: %q)\n", r.ID, p.Package, p.SkipFix)
114115
continue
115116
}
116-
syms, err := symbols.Exported(m, p, errlog)
117+
syms, err := symbols.Exported(m, p, log.Errf, log.Err)
117118
if err != nil {
118119
return fmt.Errorf("package %s: %w", p.Package, err)
119120
}
120121
// Remove any derived symbols that were marked as excluded by a human.
121122
syms = removeExcluded(syms, p.ExcludedSymbols)
122123
if !cmp.Equal(syms, p.DerivedSymbols) {
123124
p.DerivedSymbols = syms
124-
infolog.Printf("%s: updated derived symbols for package %s\n", r.ID, p.Package)
125+
log.Infof("%s: updated derived symbols for package %s\n", r.ID, p.Package)
125126
}
126127
}
127128
}
@@ -136,7 +137,7 @@ func removeExcluded(syms, excluded []string) []string {
136137
var newSyms []string
137138
for _, d := range syms {
138139
if slices.Contains(excluded, d) {
139-
infolog.Printf("removed excluded symbol %s\n", d)
140+
log.Infof("removed excluded symbol %s\n", d)
140141
continue
141142
}
142143
newSyms = append(newSyms, d)

cmd/vulnreport/lint.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ package main
77
import (
88
"context"
99

10+
"golang.org/x/vulndb/cmd/vulnreport/log"
1011
"golang.org/x/vulndb/internal/derrors"
1112
"golang.org/x/vulndb/internal/proxy"
1213
"golang.org/x/vulndb/internal/report"
1314
)
1415

1516
func lint(_ context.Context, filename string, pc *proxy.Client) (err error) {
1617
defer derrors.Wrap(&err, "lint(%q)", filename)
17-
infolog.Printf("lint %s\n", filename)
18+
log.Infof("lint %s\n", filename)
1819

1920
_, err = report.ReadAndLint(filename, pc)
2021
return err

cmd/vulnreport/log/log.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package log
6+
7+
import (
8+
"io"
9+
"os"
10+
11+
"log"
12+
)
13+
14+
var (
15+
infolog *log.Logger
16+
outlog *log.Logger
17+
warnlog *log.Logger
18+
errlog *log.Logger
19+
)
20+
21+
func Init(quiet bool) {
22+
if quiet {
23+
infolog = log.New(io.Discard, "", 0)
24+
} else {
25+
infolog = log.New(os.Stderr, "info: ", 0)
26+
}
27+
outlog = log.New(os.Stdout, "", 0)
28+
warnlog = log.New(os.Stderr, "WARNING: ", 0)
29+
errlog = log.New(os.Stderr, "ERROR: ", 0)
30+
}
31+
32+
func Infof(format string, v ...any) {
33+
infolog.Printf(format, v...)
34+
}
35+
36+
func Outf(format string, v ...any) {
37+
outlog.Printf(format, v...)
38+
}
39+
40+
func Warnf(format string, v ...any) {
41+
warnlog.Printf(format, v...)
42+
}
43+
44+
func Errf(format string, v ...any) {
45+
errlog.Printf(format, v...)
46+
}
47+
48+
func Info(v ...any) {
49+
infolog.Println(v...)
50+
}
51+
52+
func Out(v ...any) {
53+
outlog.Println(v...)
54+
}
55+
56+
func Warn(v ...any) {
57+
warnlog.Println(v...)
58+
}
59+
60+
func Err(v ...any) {
61+
errlog.Println(v...)
62+
}

0 commit comments

Comments
 (0)