From 76b838d9c0c5666d3b4ea5ed148d554ce0ada15e Mon Sep 17 00:00:00 2001 From: Dumindu Madunuwan Date: Sun, 19 Nov 2023 16:14:22 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Add=20zerol?= =?UTF-8?q?og.Logger=20as=20direct=20dependency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/requestlog/handler.go | 6 +- api/resource/book/handler.go | 6 +- api/router/router.go | 4 +- util/logger/logger.go | 128 +----------------------------- util/logger/logger_test.go | 148 ----------------------------------- 5 files changed, 10 insertions(+), 282 deletions(-) delete mode 100644 util/logger/logger_test.go diff --git a/api/requestlog/handler.go b/api/requestlog/handler.go index 925e0b5..57a7592 100644 --- a/api/requestlog/handler.go +++ b/api/requestlog/handler.go @@ -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, diff --git a/api/resource/book/handler.go b/api/resource/book/handler.go index 777c042..61a1305 100644 --- a/api/resource/book/handler.go +++ b/api/resource/book/handler.go @@ -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, diff --git a/api/router/router.go b/api/router/router.go index 2a3fdf6..6a7a623 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -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) diff --git a/util/logger/logger.go b/util/logger/logger.go index 82d67cd..7315447 100644 --- a/util/logger/logger.go +++ b/util/logger/logger.go @@ -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 @@ -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 } diff --git a/util/logger/logger_test.go b/util/logger/logger_test.go deleted file mode 100644 index 76a5a6e..0000000 --- a/util/logger/logger_test.go +++ /dev/null @@ -1,148 +0,0 @@ -//go:build !binary_log -// +build !binary_log - -package logger_test - -import ( - "errors" - "flag" - "time" - - "github.com/rs/zerolog" - - "myapp/util/logger" -) - -// setup would normally be an init() function, however, there seems -// to be something awry with the testing framework when we set the -// global Logger from an init() -func setup() *logger.Logger { - // UNIX Time is faster and smaller than most timestamps - // If you set zerolog.TimeFieldFormat to an empty string, - // logs will write with UNIX time - zerolog.TimeFieldFormat = "" - // In order to always output a static time to stdout for these - // examples to pass, we need to override zerolog.TimestampFunc - // and log.Logger globals -- you would not normally need to do this - zerolog.TimestampFunc = func() time.Time { - return time.Date(2008, 1, 8, 17, 5, 5, 0, time.UTC) - } - - return logger.New(true) -} - -// Simple logging example using the Print function in the log package -// Note that both Print and Printf are at the debug log level by default -func ExampleLogger_Print() { - l := setup() - l.Print("hello world") - - // Output: {"level":"debug","time":1199811905,"message":"hello world"} -} - -// Simple logging example using the Printf function in the log package -func ExampleLogger_Printf() { - l := setup() - l.Printf("hello %s", "world") - - // Output: {"level":"debug","time":1199811905,"message":"hello world"} -} - -// Example of a log with no particular "level" -func ExampleLogger_Log() { - l := setup() - l.Log().Msg("hello world") - - // Output: {"time":1199811905,"message":"hello world"} -} - -// Example of a conditional level based on the presence of an error. -func ExampleLogger_Err() { - l := setup() - err := errors.New("some error") - l.Err(err).Msg("hello world") - l.Err(nil).Msg("hello world") - - // Output: {"level":"error","error":"some error","time":1199811905,"message":"hello world"} - // {"level":"info","time":1199811905,"message":"hello world"} -} - -// Example of a log at a particular "level" (in this case, "trace") -func ExampleLogger_Trace() { - l := setup() - l.Trace().Msg("hello world") - - // Output: {"level":"trace","time":1199811905,"message":"hello world"} -} - -// Example of a log at a particular "level" (in this case, "debug") -func ExampleLogger_Debug() { - l := setup() - l.Debug().Msg("hello world") - - // Output: {"level":"debug","time":1199811905,"message":"hello world"} -} - -// Example of a log at a particular "level" (in this case, "info") -func ExampleLogger_Info() { - l := setup() - l.Info().Msg("hello world") - - // Output: {"level":"info","time":1199811905,"message":"hello world"} -} - -// Example of a log at a particular "level" (in this case, "warn") -func ExampleLogger_Warn() { - l := setup() - l.Warn().Msg("hello world") - - // Output: {"level":"warn","time":1199811905,"message":"hello world"} -} - -// Example of a log at a particular "level" (in this case, "error") -func ExampleLogger_Error() { - l := setup() - l.Error().Msg("hello world") - - // Output: {"level":"error","time":1199811905,"message":"hello world"} -} - -// Example of a log at a particular "level" (in this case, "fatal") -func ExampleLogger_Fatal() { - l := setup() - err := errors.New("A repo man spends his life getting into tense situations") - service := "myservice" - - l.Fatal(). - Err(err). - Str("service", service). - Msgf("Cannot start %s", service) - - // Outputs: {"level":"fatal","time":1199811905,"error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"} -} - -// This example uses command-line flags to demonstrate various outputs -// depending on the chosen log level. -func Example() { - l := setup() - debug := flag.Bool("debug", false, "sets log level to debug") - - flag.Parse() - - // Default level for this example is info, unless debug flag is present - zerolog.SetGlobalLevel(zerolog.InfoLevel) - if *debug { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } - - l.Debug().Msg("This message appears only when log level set to Debug") - l.Info().Msg("This message appears when log level set to Debug or Info") - - if e := l.Debug(); e.Enabled() { - // Compute log output only if enabled. - value := "bar" - e.Str("foo", value).Msg("some debug message") - } - - // Output: {"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"} -}