Skip to content

Commit

Permalink
chore: added Log method and optimizations (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-wz authored Mar 10, 2024
1 parent 64b869a commit 3995fa9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
24 changes: 10 additions & 14 deletions internal/infrastructure/logger/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@ import (
"github.com/rs/zerolog"
)

// Note: add time format types supported by zerolog as needed
const (
DefaultTimeFormat TimeFormat = "2006/01/02 15:04:05"
)

// ZeroLog is a logger type with zerolog.Logger support
type ZeroLog struct {
logger *zerolog.Logger
}

type TimeFormat string

func (tf TimeFormat) String() string {
return string(tf)
}

func NewZeroLog(timeFormat TimeFormat) *ZeroLog {
// NewZeroLog returns an implemented ZeroLog instance.
func NewZeroLog() ZeroLog {
writer := zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
w.Out = os.Stderr
w.TimeFormat = timeFormat.String()
w.TimeFormat = "2006/01/02 15:04:05"
})

zerolog.SetGlobalLevel(zerolog.DebugLevel)
zl := zerolog.New(writer).With().Timestamp().Logger()

return &ZeroLog{
return ZeroLog{
logger: &zl,
}
}

// Log returns the pointer of the zerolog.Logger object
func (l ZeroLog) Log() *zerolog.Logger {
return l.logger
}
5 changes: 3 additions & 2 deletions internal/infrastructure/logger/zerolog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func TestNewZeroLog(t *testing.T) {
zl := NewZeroLog(DefaultTimeFormat)
assert.NotNil(t, zl)
zl := NewZeroLog()
assert.NotEqual(t, zl, ZeroLog{})
zl.Log().Info().Msg("NewZeroLog tested successfully")
}

0 comments on commit 3995fa9

Please sign in to comment.