Skip to content

Commit

Permalink
fix: use logger only for github messages
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Zipitria <[email protected]>
  • Loading branch information
fzipi committed Mar 2, 2024
1 parent 9ea579c commit 8891875
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/flag_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (o *outputType) String() string {
func (o *outputType) Set(value string) error {
switch value {
case string(gitHub):
logger = loggerConfig.SetGithubOutput()
logger = loggerConfig.SetGithubOutput(os.Stdout)
fallthrough
case string(text):
rootValues.output = outputType(value)
Expand Down
9 changes: 4 additions & 5 deletions cmd/regex_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func performCompare(processAll bool, ctx *processors.Context) error {
if err != nil && len(chainOffsetString) > 0 {
return errors.New("failed to match chain offset. Value must not be larger than 255")
}
regex := runAssemble(filePath, ctx)
err = processRegexForCompare(id, uint8(chainOffset), regex, ctx)
rx := runAssemble(filePath, ctx)
err = processRegexForCompare(id, uint8(chainOffset), rx, ctx)
if err != nil && errors.Is(err, &ComparisonError{}) {
failed = true
return nil
Expand All @@ -148,9 +148,8 @@ func performCompare(processAll bool, ctx *processors.Context) error {
if err != nil {
logger.Fatal().Err(err).Msg("Failed to compare expressions")
}
if failed && rootValues.output == gitHub {
fmt.Println("::error::All rules need to be up to date.",
"Please run `crs-toolchain regex update --all`")
if failed {
logger.Error().Msg("All rules need to be up to date. Please run `crs-toolchain regex update --all`")
return &ComparisonError{}
}
} else {
Expand Down
17 changes: 2 additions & 15 deletions cmd/regex_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

const (
regexAssemblyStandardHeader = "##! Please refer to the documentation at\n##! https://coreruleset.org/docs/development/regex_assembly/.\n"
showCharsAround = 20
)

// formatCmd represents the generate command
Expand Down Expand Up @@ -149,18 +148,14 @@ func processAll(ctxt *processors.Context, checkOnly bool) error {
return err
}
if failed {
if rootValues.output == gitHub {
fmt.Println("::error::All assembly files need to be properly formatted.",
"Please run `crs-toolchain regex format --all`")
}
logger.Error().Msg("All assembly files need to be properly formatted. Please run `crs-toolchain regex format --all`")
return &UnformattedFileError{}
}
return nil
}

func processFile(filePath string, ctxt *processors.Context, checkOnly bool) error {
var processFileError error
message := ""
filename := path.Base(filePath)
logger.Info().Msgf("Processing %s", filename)
file, err := os.Open(filePath)
Expand Down Expand Up @@ -213,8 +208,7 @@ func processFile(filePath string, ctxt *processors.Context, checkOnly bool) erro
}
equalContent := bytes.Equal(currentContents, newContents)
if !equalContent || foundUppercase {
message = formatMessage("File not properly formatted")
fmt.Println(message)
logger.Error().Msg("File not properly formatted")
processFileError = &UnformattedFileError{filePath: filePath}
}
} else {
Expand Down Expand Up @@ -281,13 +275,6 @@ func processLine(line []byte, indent int) ([]byte, int, error) {
return trimmedLine, nextIndent, nil
}

func formatMessage(message string) string {
if rootValues.output == gitHub {
message = "::warning::" + message
}
return message
}

func formatEndOfFile(lines []string) []string {
eof := len(lines) - 1
if eof < 0 {
Expand Down
35 changes: 17 additions & 18 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package logger

import (
"fmt"
"io"
"os"

"github.com/rs/zerolog"
Expand All @@ -13,31 +14,23 @@ import (

const DefaultLogLevel zerolog.Level = zerolog.InfoLevel

var consoleOutput = zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "03:04:05"}

func init() {
log.Logger = log.Output(consoleOutput).With().Caller().Logger()
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "03:04:05"}).With().Caller().Logger()
zerolog.SetGlobalLevel(DefaultLogLevel)
}

// SetGithubOutput changes the standard logging format to be compatible with GitHub's.
// See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-creating-an-annotation-for-an-error
// Levels on github are:
// - debug, notice, error, warning
// Another possibility is to add the following strings between the level and the message:
// file={name},line={line},endLine={endLine},title={title}
func SetGithubOutput() zerolog.Logger {
// the following formatlevel loosely translates from posix levels to github levels
consoleOutput.FormatLevel = func(i interface{}) string {
func SetGithubOutput(w io.Writer) zerolog.Logger {
ghOutput := zerolog.ConsoleWriter{Out: w, TimeFormat: "03:04:05"}
ghOutput.FormatLevel = func(i interface{}) string {
var l string
if ll, ok := i.(string); ok {
switch ll {
case zerolog.LevelTraceValue, zerolog.LevelDebugValue:
l = "debug"
case zerolog.LevelInfoValue:
l = "notice "
l = "notice"
case zerolog.LevelWarnValue:
l = "warn "
l = "warn"
case zerolog.LevelErrorValue, zerolog.LevelFatalValue, zerolog.LevelPanicValue:
l = "error "
default:
Expand All @@ -50,9 +43,15 @@ func SetGithubOutput() zerolog.Logger {
}
return fmt.Sprintf("::%s", l)
}
consoleOutput.FormatMessage = func(i interface{}) string {
return fmt.Sprintf("::%s", i)
ghOutput.FormatMessage = func(i interface{}) string {
return fmt.Sprintf("::%s\n", i)
}
ghOutput.PartsExclude = []string{zerolog.TimestampFieldName, zerolog.CallerFieldName}
ghOutput.PartsOrder = []string{
zerolog.LevelFieldName,
zerolog.MessageFieldName,
}
consoleOutput.PartsExclude = []string{zerolog.TimestampFieldName}
return zerolog.New(consoleOutput).With().Logger()
ghOutput.NoColor = true

return log.Output(ghOutput).With().Caller().Logger()
}
124 changes: 124 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2024 OWASP ModSecurity Core Rule Set Project
// SPDX-License-Identifier: Apache-2.0

package logger

import (
"bytes"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"
)

type loggerTestSuite struct {
suite.Suite
out *bytes.Buffer
logger zerolog.Logger
}

func TestRunLoggerTestSuite(t *testing.T) {
suite.Run(t, new(loggerTestSuite))
}

var testJsonBase = []struct {
name string
text string
logType zerolog.Level
want string
}{
{
name: "JsonBaseOutput",
text: "hello",
logType: zerolog.InfoLevel,
want: "message\":\"hello\"",
},
}

var testConsoleBase = []struct {
name string
text string
logType zerolog.Level
want string
}{
{
name: "BaseConsoleOutput",
text: "hello",
logType: zerolog.InfoLevel,
want: "INF hello component=parser-test",
},
}

var testGithub = []struct {
name string
text string
logType zerolog.Level
want string
}{
{
name: "TestGithubInfoOutput",
text: "this is an info message",
logType: zerolog.InfoLevel,
want: "::notice ::this is an info message",
},
{
name: "TestGithubWarningOutput",
text: "this is a warning message",
logType: zerolog.WarnLevel,
want: "::warn ::this is a warning message",
},
{
name: "TestGithubTraceOutput",
text: "this is a trace message that will show as debug",
logType: zerolog.TraceLevel,
want: "::debug ::this is a trace message that will show as debug",
},
{
name: "TestGithubDebugOutput",
text: "this is a debug message",
logType: zerolog.DebugLevel,
want: "::debug ::this is a debug message",
},
}

func (s *loggerTestSuite) SetupTest() {
// reset logger
s.out = &bytes.Buffer{}
s.logger = zerolog.New(s.out).With().Str("component", "parser-test").Logger()
zerolog.SetGlobalLevel(zerolog.TraceLevel)
}

func (s *loggerTestSuite) TestJsonOutput() {
for _, t := range testJsonBase {
s.Run(t.name, func() {
s.logger.WithLevel(t.logType).Msg(t.text)
s.Contains(s.out.String(), t.want)
s.out.Reset()
})
}
}

func (s *loggerTestSuite) TestConsoleOutput() {
s.logger = s.logger.Output(zerolog.ConsoleWriter{Out: s.out, NoColor: true, TimeFormat: "03:04:05"})
for _, t := range testConsoleBase {
s.Run(t.name, func() {
s.logger.WithLevel(t.logType).Msg(t.text)
s.Contains(s.out.String(), t.want)
s.out.Reset()
})
}
}

//s.log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "03:04:05"}).With().Caller().Logger()

func (s *loggerTestSuite) TestSetGithubOutput() {
// send logs to buffer
logger := SetGithubOutput(s.out)
for _, t := range testGithub {
s.Run(t.name, func() {
logger.WithLevel(t.logType).Msg(t.text)
s.Contains(s.out.String(), t.want)
s.out.Reset()
})
}
}

0 comments on commit 8891875

Please sign in to comment.