Skip to content

Commit

Permalink
🧑‍💻 Add zerolog.Logger as direct dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dumindu committed Nov 19, 2023
1 parent 6413f86 commit 76b838d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 282 deletions.
6 changes: 3 additions & 3 deletions api/requestlog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (
"net/http"
"time"

"myapp/util/logger"
"github.com/rs/zerolog"
)

type Handler struct {
handler http.Handler
logger *logger.Logger
logger *zerolog.Logger
}

func NewHandler(h http.HandlerFunc, l *logger.Logger) *Handler {
func NewHandler(h http.HandlerFunc, l *zerolog.Logger) *Handler {
return &Handler{
handler: h,
logger: l,
Expand Down
6 changes: 3 additions & 3 deletions api/resource/book/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/rs/zerolog"
"gorm.io/gorm"

e "myapp/api/resource/common/err"
"myapp/util/logger"
validatorUtil "myapp/util/validator"
)

type API struct {
logger *logger.Logger
logger *zerolog.Logger
validator *validator.Validate
repository *Repository
}

func New(logger *logger.Logger, validator *validator.Validate, db *gorm.DB) *API {
func New(logger *zerolog.Logger, validator *validator.Validate, db *gorm.DB) *API {
return &API{
logger: logger,
validator: validator,
Expand Down
4 changes: 2 additions & 2 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package router
import (
"github.com/go-chi/chi/v5"
"github.com/go-playground/validator/v10"
"github.com/rs/zerolog"
"gorm.io/gorm"

"myapp/api/requestlog"
"myapp/api/resource/book"
"myapp/api/resource/health"
"myapp/api/router/middleware"
"myapp/util/logger"
)

func New(l *logger.Logger, v *validator.Validate, db *gorm.DB) *chi.Mux {
func New(l *zerolog.Logger, v *validator.Validate, db *gorm.DB) *chi.Mux {
r := chi.NewRouter()

r.Get("/livez", health.Read)
Expand Down
128 changes: 2 additions & 126 deletions util/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package logger

import (
"context"
"fmt"
"io"
"os"

"github.com/rs/zerolog"
)

type Logger struct {
logger *zerolog.Logger
}

func New(isDebug bool) *Logger {
func New(isDebug bool) *zerolog.Logger {
logLevel := zerolog.InfoLevel
if isDebug {
logLevel = zerolog.TraceLevel
Expand All @@ -22,122 +15,5 @@ func New(isDebug bool) *Logger {
zerolog.SetGlobalLevel(logLevel)
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()

return &Logger{logger: &logger}
}

// Output duplicates the global logger and sets w as its output.
func (l *Logger) Output(w io.Writer) zerolog.Logger {
return l.logger.Output(w)
}

// With creates a child logger with the field added to its context.
func (l *Logger) With() zerolog.Context {
return l.logger.With()
}

// Level creates a child logger with the minimum accepted level set to level.
func (l *Logger) Level(level zerolog.Level) zerolog.Logger {
return l.logger.Level(level)
}

// Sample returns a logger with the s sampler.
func (l *Logger) Sample(s zerolog.Sampler) zerolog.Logger {
return l.logger.Sample(s)
}

// Hook returns a logger with the h Hook.
func (l *Logger) Hook(h zerolog.Hook) zerolog.Logger {
return l.logger.Hook(h)
}

// Err starts a new message with error level with err as a field if not nil or
// with info level if err is nil.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Err(err error) *zerolog.Event {
return l.logger.Err(err)
}

// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Trace() *zerolog.Event {
return l.logger.Trace()
}

// Debug starts a new message with debug level.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Debug() *zerolog.Event {
return l.logger.Debug()
}

// Info starts a new message with info level.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Info() *zerolog.Event {
return l.logger.Info()
}

// Warn starts a new message with warn level.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Warn() *zerolog.Event {
return l.logger.Warn()
}

// Error starts a new message with error level.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Error() *zerolog.Event {
return l.logger.Error()
}

// Fatal starts a new message with fatal level. The os.Exit(1) function
// is called by the Msg method.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Fatal() *zerolog.Event {
return l.logger.Fatal()
}

// Panic starts a new message with panic level. The message is also sent
// to the panic function.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Panic() *zerolog.Event {
return l.logger.Panic()
}

// WithLevel starts a new message with level.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) WithLevel(level zerolog.Level) *zerolog.Event {
return l.logger.WithLevel(level)
}

// Log starts a new message with no level. Setting zerolog.GlobalLevel to
// zerolog.Disabled will still disable events produced by this method.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Log() *zerolog.Event {
return l.logger.Log()
}

// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Print(v ...interface{}) {
l.logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...))
}

// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Printf(format string, v ...interface{}) {
l.logger.Debug().CallerSkipFrame(1).Msgf(format, v...)
}

// Ctx returns the Logger associated with the ctx. If no logger
// is associated, a disabled logger is returned.
func (l *Logger) Ctx(ctx context.Context) *Logger {
return &Logger{logger: zerolog.Ctx(ctx)}
return &logger
}
148 changes: 0 additions & 148 deletions util/logger/logger_test.go

This file was deleted.

0 comments on commit 76b838d

Please sign in to comment.