Skip to content

Commit

Permalink
feat: add log
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 30, 2025
1 parent 65d7b38 commit 847731b
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 60 deletions.
8 changes: 6 additions & 2 deletions pkg/logutils/logutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ const (

// Printers.
const (
DebugKeyTabPrinter = "tab_printer"
DebugKeyTextPrinter = "text_printer"
DebugKeyCheckstylePrinter = "checkstyle_printer"
DebugKeyCodeClimatePrinter = "codeclimate_printer"
DebugKeySarifPrinter = "sarif_printer"
DebugKeyTabPrinter = "tab_printer"
DebugKeyTeamCityPrinter = "teamcity_printer"
DebugKeyTextPrinter = "text_printer"
)

// Processors.
Expand Down
4 changes: 3 additions & 1 deletion pkg/printers/checkstyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-xmlfmt/xmlfmt"
"golang.org/x/exp/maps"

"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)

Expand All @@ -21,10 +22,11 @@ type Checkstyle struct {
sanitizer severitySanitizer
}

func NewCheckstyle(w io.Writer) *Checkstyle {
func NewCheckstyle(log logutils.Log, w io.Writer) *Checkstyle {
return &Checkstyle{
w: w,
sanitizer: severitySanitizer{
log: log.Child(logutils.DebugKeyCheckstylePrinter),
// https://checkstyle.org/config.html#Severity
// https://checkstyle.org/property_types.html#SeverityLevel
allowedSeverities: []string{"ignore", "info", "warning", defaultCheckstyleSeverity},
Expand Down
57 changes: 55 additions & 2 deletions pkg/printers/checkstyle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)

Expand Down Expand Up @@ -41,15 +42,67 @@ func TestCheckstyle_Print(t *testing.T) {
Column: 9,
},
},
{
FromLinter: "linter-c",
Severity: "",
Text: "without severity",
SourceLines: []string{
"func foo() {",
"\tfmt.Println(\"bar\")",
"}",
},
Pos: token.Position{
Filename: "path/to/filec.go",
Offset: 5,
Line: 300,
Column: 10,
},
},
{
FromLinter: "linter-d",
Severity: "foo",
Text: "unknown severity",
SourceLines: []string{
"func foo() {",
"\tfmt.Println(\"bar\")",
"}",
},
Pos: token.Position{
Filename: "path/to/filed.go",
Offset: 5,
Line: 300,
Column: 11,
},
},
}

buf := new(bytes.Buffer)
printer := NewCheckstyle(buf)

log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
log.SetLevel(logutils.LogLevelDebug)

printer := NewCheckstyle(log, buf)

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

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"
expected := `<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="5.0">
<file name="path/to/filea.go">
<error column="4" line="10" message="some issue" severity="warning" source="linter-a"></error>
</file>
<file name="path/to/fileb.go">
<error column="9" line="300" message="another issue" severity="error" source="linter-b"></error>
</file>
<file name="path/to/filec.go">
<error column="10" line="300" message="without severity" severity="error" source="linter-c"></error>
</file>
<file name="path/to/filed.go">
<error column="11" line="300" message="unknown severity" severity="error" source="linter-d"></error>
</file>
</checkstyle>
`

assert.Equal(t, expected, strings.ReplaceAll(buf.String(), "\r", ""))
}
4 changes: 3 additions & 1 deletion pkg/printers/codeclimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io"

"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)

Expand All @@ -16,10 +17,11 @@ type CodeClimate struct {
sanitizer severitySanitizer
}

func NewCodeClimate(w io.Writer) *CodeClimate {
func NewCodeClimate(log logutils.Log, w io.Writer) *CodeClimate {
return &CodeClimate{
w: w,
sanitizer: severitySanitizer{
log: log.Child(logutils.DebugKeyCodeClimatePrinter),
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
allowedSeverities: []string{"info", "minor", "major", defaultCodeClimateSeverity, "blocker"},
defaultSeverity: defaultCodeClimateSeverity,
Expand Down
38 changes: 30 additions & 8 deletions pkg/printers/codeclimate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)

func TestCodeClimate_Print(t *testing.T) {
issues := []result.Issue{
{
FromLinter: "linter-a",
Severity: "warning",
Severity: "minor",
Text: "some issue",
Pos: token.Position{
Filename: "path/to/filea.go",
Expand All @@ -42,28 +43,49 @@ func TestCodeClimate_Print(t *testing.T) {
},
{
FromLinter: "linter-c",
Text: "issue c",
Severity: "",
Text: "without severity",
SourceLines: []string{
"func foo() {",
"\tfmt.Println(\"ccc\")",
"\tfmt.Println(\"bar\")",
"}",
},
Pos: token.Position{
Filename: "path/to/filec.go",
Offset: 6,
Line: 200,
Column: 2,
Offset: 5,
Line: 300,
Column: 10,
},
},
{
FromLinter: "linter-d",
Severity: "foo",
Text: "unknown severity",
SourceLines: []string{
"func foo() {",
"\tfmt.Println(\"bar\")",
"}",
},
Pos: token.Position{
Filename: "path/to/filed.go",
Offset: 5,
Line: 300,
Column: 11,
},
},
}

buf := new(bytes.Buffer)
printer := NewCodeClimate(buf)

log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
log.SetLevel(logutils.LogLevelDebug)

printer := NewCodeClimate(log, buf)

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

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}}}]
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}}}]
`

assert.Equal(t, expected, buf.String())
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type JSON struct {
w io.Writer
}

func NewJSON(rd *report.Data, w io.Writer) *JSON {
func NewJSON(w io.Writer, rd *report.Data) *JSON {
return &JSON{
rd: rd,
w: w,
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestJSON_Print(t *testing.T) {

buf := new(bytes.Buffer)

printer := NewJSON(nil, buf)
printer := NewJSON(buf, nil)

err := printer.Print(issues)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type JunitXML struct {
w io.Writer
}

func NewJunitXML(extended bool, w io.Writer) *JunitXML {
func NewJunitXML(w io.Writer, extended bool) *JunitXML {
return &JunitXML{
extended: extended,
w: w,
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/junitxml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Details: func foo() {
t.Parallel()

buf := new(bytes.Buffer)
printer := NewJunitXML(test.extended, buf)
printer := NewJunitXML(buf, test.extended)

err := printer.Print(issues)
require.NoError(t, err)
Expand Down
24 changes: 12 additions & 12 deletions pkg/printers/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,25 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error

switch format {
case config.OutFormatJSON:
p = NewJSON(c.reportData, w)
p = NewJSON(w, c.reportData)
case config.OutFormatLineNumber, config.OutFormatColoredLineNumber:
p = NewText(c.cfg.PrintIssuedLine,
format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName,
c.log.Child(logutils.DebugKeyTextPrinter), w)
p = NewText(c.log, w, c.cfg.PrintLinterName, c.cfg.PrintIssuedLine, format == config.OutFormatColoredLineNumber)
case config.OutFormatTab, config.OutFormatColoredTab:
p = NewTab(c.cfg.PrintLinterName,
format == config.OutFormatColoredTab,
c.log.Child(logutils.DebugKeyTabPrinter), w)
p = NewTab(c.log, w, c.cfg.PrintLinterName, format == config.OutFormatColoredTab)
case config.OutFormatCheckstyle:
p = NewCheckstyle(w)
p = NewCheckstyle(c.log, w)
case config.OutFormatCodeClimate:
p = NewCodeClimate(w)
p = NewCodeClimate(c.log, w)
case config.OutFormatHTML:
p = NewHTML(w)
case config.OutFormatJunitXML, config.OutFormatJunitXMLExtended:
p = NewJunitXML(format == config.OutFormatJunitXMLExtended, w)
p = NewJunitXML(w, format == config.OutFormatJunitXMLExtended)
case config.OutFormatGithubActions:
p = NewGitHubAction(w)
case config.OutFormatTeamCity:
p = NewTeamCity(w)
p = NewTeamCity(c.log, w)
case config.OutFormatSarif:
p = NewSarif(w)
p = NewSarif(c.log, w)
default:
return nil, fmt.Errorf("unknown output format %q", format)
}
Expand All @@ -146,6 +142,8 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
}

type severitySanitizer struct {
log logutils.Log

allowedSeverities []string
defaultSeverity string
}
Expand All @@ -155,5 +153,7 @@ func (s *severitySanitizer) Clean(severity string) string {
return severity
}

s.log.Infof("severity '%s' is not inside %v, fallback to %s", severity, s.allowedSeverities, s.defaultSeverity)

return s.defaultSeverity
}
4 changes: 3 additions & 1 deletion pkg/printers/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io"

"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)

Expand All @@ -22,10 +23,11 @@ type Sarif struct {
sanitizer severitySanitizer
}

func NewSarif(w io.Writer) *Sarif {
func NewSarif(log logutils.Log, w io.Writer) *Sarif {
return &Sarif{
w: w,
sanitizer: severitySanitizer{
log: log.Child(logutils.DebugKeySarifPrinter),
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790898
allowedSeverities: []string{"none", "note", "warning", defaultSarifSeverity},
defaultSeverity: defaultSarifSeverity,
Expand Down
Loading

0 comments on commit 847731b

Please sign in to comment.