Skip to content

Commit

Permalink
chore: add tests for gh logger output
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Zipitria <[email protected]>
  • Loading branch information
fzipi committed Feb 27, 2024
1 parent a7e1f2c commit c343cf2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
17 changes: 11 additions & 6 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/rs/zerolog/log"
)

const DefaultLogLevel zerolog.Level = zerolog.InfoLevel
const DefaultLogLevel zerolog.Level = zerolog.TraceLevel

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

Expand All @@ -26,9 +26,10 @@ func init() {
// - 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 {
func SetGithubOutput(w *zerolog.ConsoleWriter) zerolog.Logger {
w.NoColor = true
// the following formatlevel loosely translates from posix levels to github levels
consoleOutput.FormatLevel = func(i interface{}) string {
w.FormatLevel = func(i interface{}) string {
var l string
if ll, ok := i.(string); ok {
switch ll {
Expand All @@ -50,9 +51,13 @@ func SetGithubOutput() zerolog.Logger {
}
return fmt.Sprintf("::%s", l)
}
consoleOutput.FormatMessage = func(i interface{}) string {
w.FormatMessage = func(i interface{}) string {
if i == nil {
return ""
}
return fmt.Sprintf("::%s", i)
}
consoleOutput.PartsExclude = []string{zerolog.TimestampFieldName}
return zerolog.New(consoleOutput).With().Logger()
w.PartsExclude = []string{zerolog.TimestampFieldName, zerolog.CallerFieldName}
w.PartsOrder = []string{zerolog.LevelFieldName, zerolog.MessageFieldName}
return zerolog.New(w).With().Logger()
}
46 changes: 46 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package logger

import (
"bytes"
"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"
"testing"
)

type loggerTestSuite struct {
suite.Suite
zl zerolog.Logger
buf *bytes.Buffer
}

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

func (s *loggerTestSuite) SetupSuite() {
s.buf = &bytes.Buffer{}
s.zl = zerolog.New(zerolog.ConsoleWriter{Out: s.buf}).With().Timestamp().Logger()
}

// TestLogger_Normal tests the normal logger output
func (s *loggerTestSuite) TestLogger_Normal() {
s.zl.Info().Msg("This is a test message")
s.Contains(s.buf.String(), "\x1b[32mINF\x1b[0m This is a test message\n")
s.Equal(52, s.buf.Len())
s.buf.Reset()
}

// TestLogger_GitHub tests the GitHub logger output
func (s *loggerTestSuite) TestLogger_GitHub() {
githubLogger := SetGithubOutput(&zerolog.ConsoleWriter{Out: s.buf})
githubLogger.Info().Msg("This is an info message")
msg, err := s.buf.ReadString('\n')
s.NoError(err)
s.Equal("::notice ::This is an info message\n", msg)
s.Len(msg, 36)

githubLogger.Debug().Msg("This is a debug message")
msg, err = s.buf.ReadString('\n')
s.NoError(err)
s.Equal("::debug::This is a debug message\n", msg)
}

0 comments on commit c343cf2

Please sign in to comment.