Skip to content

Commit b3e7fcb

Browse files
committed
feat: add log
1 parent 65d7b38 commit b3e7fcb

18 files changed

+200
-60
lines changed

Diff for: pkg/logutils/logutils.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ const (
3636

3737
// Printers.
3838
const (
39-
DebugKeyTabPrinter = "tab_printer"
40-
DebugKeyTextPrinter = "text_printer"
39+
DebugKeyCheckstylePrinter = "checkstyle_printer"
40+
DebugKeyCodeClimatePrinter = "codeclimate_printer"
41+
DebugKeySarifPrinter = "sarif_printer"
42+
DebugKeyTabPrinter = "tab_printer"
43+
DebugKeyTeamCityPrinter = "teamcity_printer"
44+
DebugKeyTextPrinter = "text_printer"
4145
)
4246

4347
// Processors.

Diff for: pkg/printers/checkstyle.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/go-xmlfmt/xmlfmt"
1010
"golang.org/x/exp/maps"
1111

12+
"github.com/golangci/golangci-lint/pkg/logutils"
1213
"github.com/golangci/golangci-lint/pkg/result"
1314
)
1415

@@ -21,10 +22,11 @@ type Checkstyle struct {
2122
sanitizer severitySanitizer
2223
}
2324

24-
func NewCheckstyle(w io.Writer) *Checkstyle {
25+
func NewCheckstyle(log logutils.Log, w io.Writer) *Checkstyle {
2526
return &Checkstyle{
2627
w: w,
2728
sanitizer: severitySanitizer{
29+
log: log.Child(logutils.DebugKeyCheckstylePrinter),
2830
// https://checkstyle.org/config.html#Severity
2931
// https://checkstyle.org/property_types.html#SeverityLevel
3032
allowedSeverities: []string{"ignore", "info", "warning", defaultCheckstyleSeverity},

Diff for: pkg/printers/checkstyle_test.go

+55-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
1111

12+
"github.com/golangci/golangci-lint/pkg/logutils"
1213
"github.com/golangci/golangci-lint/pkg/result"
1314
)
1415

@@ -41,15 +42,67 @@ func TestCheckstyle_Print(t *testing.T) {
4142
Column: 9,
4243
},
4344
},
45+
{
46+
FromLinter: "linter-c",
47+
Severity: "",
48+
Text: "without severity",
49+
SourceLines: []string{
50+
"func foo() {",
51+
"\tfmt.Println(\"bar\")",
52+
"}",
53+
},
54+
Pos: token.Position{
55+
Filename: "path/to/filec.go",
56+
Offset: 5,
57+
Line: 300,
58+
Column: 10,
59+
},
60+
},
61+
{
62+
FromLinter: "linter-d",
63+
Severity: "foo",
64+
Text: "unknown severity",
65+
SourceLines: []string{
66+
"func foo() {",
67+
"\tfmt.Println(\"bar\")",
68+
"}",
69+
},
70+
Pos: token.Position{
71+
Filename: "path/to/filed.go",
72+
Offset: 5,
73+
Line: 300,
74+
Column: 11,
75+
},
76+
},
4477
}
4578

4679
buf := new(bytes.Buffer)
47-
printer := NewCheckstyle(buf)
80+
81+
log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
82+
log.SetLevel(logutils.LogLevelDebug)
83+
84+
printer := NewCheckstyle(log, buf)
4885

4986
err := printer.Print(issues)
5087
require.NoError(t, err)
5188

52-
expected := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<checkstyle version=\"5.0\">\n <file name=\"path/to/filea.go\">\n <error column=\"4\" line=\"10\" message=\"some issue\" severity=\"warning\" source=\"linter-a\"></error>\n </file>\n <file name=\"path/to/fileb.go\">\n <error column=\"9\" line=\"300\" message=\"another issue\" severity=\"error\" source=\"linter-b\"></error>\n </file>\n</checkstyle>\n"
89+
expected := `<?xml version="1.0" encoding="UTF-8"?>
90+
91+
<checkstyle version="5.0">
92+
<file name="path/to/filea.go">
93+
<error column="4" line="10" message="some issue" severity="warning" source="linter-a"></error>
94+
</file>
95+
<file name="path/to/fileb.go">
96+
<error column="9" line="300" message="another issue" severity="error" source="linter-b"></error>
97+
</file>
98+
<file name="path/to/filec.go">
99+
<error column="10" line="300" message="without severity" severity="error" source="linter-c"></error>
100+
</file>
101+
<file name="path/to/filed.go">
102+
<error column="11" line="300" message="unknown severity" severity="error" source="linter-d"></error>
103+
</file>
104+
</checkstyle>
105+
`
53106

54107
assert.Equal(t, expected, strings.ReplaceAll(buf.String(), "\r", ""))
55108
}

Diff for: pkg/printers/codeclimate.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"io"
66

7+
"github.com/golangci/golangci-lint/pkg/logutils"
78
"github.com/golangci/golangci-lint/pkg/result"
89
)
910

@@ -16,10 +17,11 @@ type CodeClimate struct {
1617
sanitizer severitySanitizer
1718
}
1819

19-
func NewCodeClimate(w io.Writer) *CodeClimate {
20+
func NewCodeClimate(log logutils.Log, w io.Writer) *CodeClimate {
2021
return &CodeClimate{
2122
w: w,
2223
sanitizer: severitySanitizer{
24+
log: log.Child(logutils.DebugKeyCodeClimatePrinter),
2325
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
2426
allowedSeverities: []string{"info", "minor", "major", defaultCodeClimateSeverity, "blocker"},
2527
defaultSeverity: defaultCodeClimateSeverity,

Diff for: pkg/printers/codeclimate_test.go

+30-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
1010

11+
"github.com/golangci/golangci-lint/pkg/logutils"
1112
"github.com/golangci/golangci-lint/pkg/result"
1213
)
1314

1415
func TestCodeClimate_Print(t *testing.T) {
1516
issues := []result.Issue{
1617
{
1718
FromLinter: "linter-a",
18-
Severity: "warning",
19+
Severity: "minor",
1920
Text: "some issue",
2021
Pos: token.Position{
2122
Filename: "path/to/filea.go",
@@ -42,28 +43,49 @@ func TestCodeClimate_Print(t *testing.T) {
4243
},
4344
{
4445
FromLinter: "linter-c",
45-
Text: "issue c",
46+
Severity: "",
47+
Text: "without severity",
4648
SourceLines: []string{
4749
"func foo() {",
48-
"\tfmt.Println(\"ccc\")",
50+
"\tfmt.Println(\"bar\")",
4951
"}",
5052
},
5153
Pos: token.Position{
5254
Filename: "path/to/filec.go",
53-
Offset: 6,
54-
Line: 200,
55-
Column: 2,
55+
Offset: 5,
56+
Line: 300,
57+
Column: 10,
58+
},
59+
},
60+
{
61+
FromLinter: "linter-d",
62+
Severity: "foo",
63+
Text: "unknown severity",
64+
SourceLines: []string{
65+
"func foo() {",
66+
"\tfmt.Println(\"bar\")",
67+
"}",
68+
},
69+
Pos: token.Position{
70+
Filename: "path/to/filed.go",
71+
Offset: 5,
72+
Line: 300,
73+
Column: 11,
5674
},
5775
},
5876
}
5977

6078
buf := new(bytes.Buffer)
61-
printer := NewCodeClimate(buf)
79+
80+
log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
81+
log.SetLevel(logutils.LogLevelDebug)
82+
83+
printer := NewCodeClimate(log, buf)
6284

6385
err := printer.Print(issues)
6486
require.NoError(t, err)
6587

66-
expected := `[{"description":"linter-a: some issue","check_name":"linter-a","severity":"critical","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","check_name":"linter-b","severity":"major","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}},{"description":"linter-c: issue c","check_name":"linter-c","severity":"critical","fingerprint":"BEE6E9FBB6BFA4B7DB9FB036697FB036","location":{"path":"path/to/filec.go","lines":{"begin":200}}}]
88+
expected := `[{"description":"linter-a: some issue","check_name":"linter-a","severity":"minor","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","check_name":"linter-b","severity":"major","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}},{"description":"linter-c: without severity","check_name":"linter-c","severity":"critical","fingerprint":"84F89700554F16CCEB6C0BB481B44AD2","location":{"path":"path/to/filec.go","lines":{"begin":300}}},{"description":"linter-d: unknown severity","check_name":"linter-d","severity":"critical","fingerprint":"340BB02E7B583B9727D73765CB64F56F","location":{"path":"path/to/filed.go","lines":{"begin":300}}}]
6789
`
6890

6991
assert.Equal(t, expected, buf.String())

Diff for: pkg/printers/json.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type JSON struct {
1414
w io.Writer
1515
}
1616

17-
func NewJSON(rd *report.Data, w io.Writer) *JSON {
17+
func NewJSON(w io.Writer, rd *report.Data) *JSON {
1818
return &JSON{
1919
rd: rd,
2020
w: w,

Diff for: pkg/printers/json_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestJSON_Print(t *testing.T) {
4444

4545
buf := new(bytes.Buffer)
4646

47-
printer := NewJSON(nil, buf)
47+
printer := NewJSON(buf, nil)
4848

4949
err := printer.Print(issues)
5050
require.NoError(t, err)

Diff for: pkg/printers/junitxml.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type JunitXML struct {
2121
w io.Writer
2222
}
2323

24-
func NewJunitXML(extended bool, w io.Writer) *JunitXML {
24+
func NewJunitXML(w io.Writer, extended bool) *JunitXML {
2525
return &JunitXML{
2626
extended: extended,
2727
w: w,

Diff for: pkg/printers/junitxml_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Details: func foo() {
105105
t.Parallel()
106106

107107
buf := new(bytes.Buffer)
108-
printer := NewJunitXML(test.extended, buf)
108+
printer := NewJunitXML(buf, test.extended)
109109

110110
err := printer.Print(issues)
111111
require.NoError(t, err)

Diff for: pkg/printers/printer.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,25 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
115115

116116
switch format {
117117
case config.OutFormatJSON:
118-
p = NewJSON(c.reportData, w)
118+
p = NewJSON(w, c.reportData)
119119
case config.OutFormatLineNumber, config.OutFormatColoredLineNumber:
120-
p = NewText(c.cfg.PrintIssuedLine,
121-
format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName,
122-
c.log.Child(logutils.DebugKeyTextPrinter), w)
120+
p = NewText(c.log, w, c.cfg.PrintLinterName, c.cfg.PrintIssuedLine, format == config.OutFormatColoredLineNumber)
123121
case config.OutFormatTab, config.OutFormatColoredTab:
124-
p = NewTab(c.cfg.PrintLinterName,
125-
format == config.OutFormatColoredTab,
126-
c.log.Child(logutils.DebugKeyTabPrinter), w)
122+
p = NewTab(c.log, w, c.cfg.PrintLinterName, format == config.OutFormatColoredTab)
127123
case config.OutFormatCheckstyle:
128-
p = NewCheckstyle(w)
124+
p = NewCheckstyle(c.log, w)
129125
case config.OutFormatCodeClimate:
130-
p = NewCodeClimate(w)
126+
p = NewCodeClimate(c.log, w)
131127
case config.OutFormatHTML:
132128
p = NewHTML(w)
133129
case config.OutFormatJunitXML, config.OutFormatJunitXMLExtended:
134-
p = NewJunitXML(format == config.OutFormatJunitXMLExtended, w)
130+
p = NewJunitXML(w, format == config.OutFormatJunitXMLExtended)
135131
case config.OutFormatGithubActions:
136132
p = NewGitHubAction(w)
137133
case config.OutFormatTeamCity:
138-
p = NewTeamCity(w)
134+
p = NewTeamCity(c.log, w)
139135
case config.OutFormatSarif:
140-
p = NewSarif(w)
136+
p = NewSarif(c.log, w)
141137
default:
142138
return nil, fmt.Errorf("unknown output format %q", format)
143139
}
@@ -146,6 +142,8 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
146142
}
147143

148144
type severitySanitizer struct {
145+
log logutils.Log
146+
149147
allowedSeverities []string
150148
defaultSeverity string
151149
}
@@ -155,5 +153,7 @@ func (s *severitySanitizer) Clean(severity string) string {
155153
return severity
156154
}
157155

156+
s.log.Infof("severity '%s' is not inside %v, fallback to '%s'", severity, s.allowedSeverities, s.defaultSeverity)
157+
158158
return s.defaultSeverity
159159
}

Diff for: pkg/printers/sarif.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"io"
66

7+
"github.com/golangci/golangci-lint/pkg/logutils"
78
"github.com/golangci/golangci-lint/pkg/result"
89
)
910

@@ -22,10 +23,11 @@ type Sarif struct {
2223
sanitizer severitySanitizer
2324
}
2425

25-
func NewSarif(w io.Writer) *Sarif {
26+
func NewSarif(log logutils.Log, w io.Writer) *Sarif {
2627
return &Sarif{
2728
w: w,
2829
sanitizer: severitySanitizer{
30+
log: log.Child(logutils.DebugKeySarifPrinter),
2931
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790898
3032
allowedSeverities: []string{"none", "note", "warning", defaultSarifSeverity},
3133
defaultSeverity: defaultSarifSeverity,

0 commit comments

Comments
 (0)