Adding logger interface
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
parent
3a72e93cc8
commit
4a21980e44
9
backend/internal/logger/logger.go
Normal file
9
backend/internal/logger/logger.go
Normal file
@ -0,0 +1,9 @@
|
||||
package logger
|
||||
|
||||
type Logger interface {
|
||||
Debug(msg string, keysAndValues ...interface{})
|
||||
Info(msg string, keysAndValues ...interface{})
|
||||
Warn(msg string, keysAndValues ...interface{})
|
||||
Error(msg string, keysAndValues ...interface{})
|
||||
Fatal(msg string, keysAndValues ...interface{})
|
||||
}
|
||||
1
backend/internal/logger/note.txt
Normal file
1
backend/internal/logger/note.txt
Normal file
@ -0,0 +1 @@
|
||||
logger taken from https://dwarvesf.hashnode.dev/go-1-21-release-slog-with-benchmarks-zerolog-and-zap#heading-implementing-the-logger-with-zerolog-and-zap
|
||||
38
backend/internal/logger/slog.go
Normal file
38
backend/internal/logger/slog.go
Normal file
@ -0,0 +1,38 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
type SlogLogger struct {
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func NewSlogLogger() Logger {
|
||||
sl := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
|
||||
return &SlogLogger{log: sl}
|
||||
}
|
||||
|
||||
func (zl *SlogLogger) Debug(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Debug(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (zl *SlogLogger) Info(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Info(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (zl *SlogLogger) Warn(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Warn(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (zl *SlogLogger) Error(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Error(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (zl *SlogLogger) Fatal(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Error(msg, keysAndValues...)
|
||||
log.Fatal(msg)
|
||||
}
|
||||
35
backend/internal/logger/zap.go
Normal file
35
backend/internal/logger/zap.go
Normal file
@ -0,0 +1,35 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ZapLogger struct {
|
||||
log *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func NewZapLogger() Logger {
|
||||
logger, _ := zap.NewProduction()
|
||||
sugar := logger.Sugar()
|
||||
return &ZapLogger{log: sugar}
|
||||
}
|
||||
|
||||
func (zl *ZapLogger) Debug(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Debugw(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (zl *ZapLogger) Info(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Infow(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (zl *ZapLogger) Warn(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Warnw(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (zl *ZapLogger) Error(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Errorw(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (zl *ZapLogger) Fatal(msg string, keysAndValues ...interface{}) {
|
||||
zl.log.Fatalw(msg, keysAndValues...)
|
||||
}
|
||||
36
backend/internal/logger/zerolog.go
Normal file
36
backend/internal/logger/zerolog.go
Normal file
@ -0,0 +1,36 @@
|
||||
// package logger
|
||||
|
||||
// import (
|
||||
// "os"
|
||||
|
||||
// "github.com/rs/zerolog"
|
||||
// )
|
||||
|
||||
// type ZeroLogger struct {
|
||||
// log zerolog.Logger
|
||||
// }
|
||||
|
||||
// func NewZeroLogger() Logger {
|
||||
// zl := zerolog.New(os.Stdout).With().Timestamp().Logger()
|
||||
// return &ZeroLogger{log: zl}
|
||||
// }
|
||||
|
||||
// func (zl *ZeroLogger) Debug(msg string, keysAndValues ...interface{}) {
|
||||
// zl.log.Debug().Fields(keysAndValues).Msg(msg)
|
||||
// }
|
||||
|
||||
// func (zl *ZeroLogger) Info(msg string, keysAndValues ...interface{}) {
|
||||
// zl.log.Info().Fields(keysAndValues).Msg(msg)
|
||||
// }
|
||||
|
||||
// func (zl *ZeroLogger) Warn(msg string, keysAndValues ...interface{}) {
|
||||
// zl.log.Warn().Fields(keysAndValues).Msg(msg)
|
||||
// }
|
||||
|
||||
// func (zl *ZeroLogger) Error(msg string, keysAndValues ...interface{}) {
|
||||
// zl.log.Error().Fields(keysAndValues).Msg(msg)
|
||||
// }
|
||||
|
||||
// func (zl *ZeroLogger) Fatal(msg string, keysAndValues ...interface{}) {
|
||||
// zl.log.Fatal().Fields(keysAndValues).Msg(msg)
|
||||
// }
|
||||
Loading…
Reference in New Issue
Block a user