Skip to content

Commit

Permalink
feat: add noop logger
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoclair committed Jan 10, 2024
1 parent 13d99f4 commit 7cfb0d6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion domain/service/service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newServiceTestMock(t *testing.T) (m allMocks, svc *service, ctrl *gomock.Co
require.NoError(t, err)

ctrl = gomock.NewController(t)
log := logger.New(*cfg)
log := logger.NewNoop()
dm := mocks.NewMockDataManager(ctrl)
accountRepo := mocks.NewMockAccountRepo(ctrl)
cm := mocks.NewMockCacheManager(ctrl)
Expand Down
2 changes: 1 addition & 1 deletion infra/data/mysql/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestMain(m *testing.M) {
log.Fatal("cannot get config: ", err)
}

log := logger.New(*cfg)
log := logger.NewNoop()
cfg.DB.MySQL.DBName = cfg.DB.MySQL.DBName + "_test"

ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion infra/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Logger interface {
Errorw(ctx context.Context, msg string, keyAndValues ...any)
Fatal(ctx context.Context, msg string)
Fatalf(ctx context.Context, msg string, args ...any)
Fatalfw(ctx context.Context, msg string, keyAndValues ...any)
Fatalw(ctx context.Context, msg string, keyAndValues ...any)
Print(args ...any)
}

Expand Down
26 changes: 26 additions & 0 deletions infra/logger/noop_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package logger

import "context"

func NewNoop() Logger {
return &NoopLogger{}
}

type NoopLogger struct{}

func (l *NoopLogger) Info(ctx context.Context, msg string) {}
func (l *NoopLogger) Infof(ctx context.Context, msg string, args ...any) {}
func (l *NoopLogger) Infow(ctx context.Context, msg string, keyAndValues ...any) {}
func (l *NoopLogger) Debug(ctx context.Context, msg string) {}
func (l *NoopLogger) Debugf(ctx context.Context, msg string, args ...any) {}
func (l *NoopLogger) Debugw(ctx context.Context, msg string, keyAndValues ...any) {}
func (l *NoopLogger) Warn(ctx context.Context, msg string) {}
func (l *NoopLogger) Warnf(ctx context.Context, msg string, args ...any) {}
func (l *NoopLogger) Warnw(ctx context.Context, msg string, keyAndValues ...any) {}
func (l *NoopLogger) Error(ctx context.Context, msg string) {}
func (l *NoopLogger) Errorf(ctx context.Context, msg string, args ...any) {}
func (l *NoopLogger) Errorw(ctx context.Context, msg string, keyAndValues ...any) {}
func (l *NoopLogger) Fatal(ctx context.Context, msg string) {}
func (l *NoopLogger) Fatalf(ctx context.Context, msg string, args ...any) {}
func (l *NoopLogger) Fatalw(ctx context.Context, msg string, keyAndValues ...any) {}
func (l *NoopLogger) Print(args ...any) {}
2 changes: 1 addition & 1 deletion infra/logger/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (l *SlogLogger) Fatalf(ctx context.Context, msg string, args ...any) {
os.Exit(1)
}

func (l *SlogLogger) Fatalfw(ctx context.Context, msg string, keyAndValues ...any) {
func (l *SlogLogger) Fatalw(ctx context.Context, msg string, keyAndValues ...any) {
l.Logger.Log(ctx, LevelFatalCode, msg, append(l.withSession(ctx), keyAndValues...)...)
os.Exit(1)
}
Expand Down

0 comments on commit 7cfb0d6

Please sign in to comment.