Skip to content

Commit

Permalink
chore: replace hclog with zap
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiebens committed Mar 19, 2023
1 parent 3577b8b commit 4ba540c
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 341 deletions.
24 changes: 12 additions & 12 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"errors"
"fmt"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/hashicorp/go-hclog"
"github.com/jsiebens/ionscale/internal/database/migration"
"github.com/jsiebens/ionscale/internal/util"
"go.uber.org/zap"
"tailscale.com/types/key"
"time"

Expand All @@ -23,7 +23,7 @@ type dbLock interface {
UnlockErr(error) error
}

func OpenDB(config *config.Database, logger hclog.Logger) (domain.Repository, error) {
func OpenDB(config *config.Database, logger *zap.Logger) (domain.Repository, error) {
db, lock, err := createDB(config, logger)
if err != nil {
return nil, err
Expand Down Expand Up @@ -54,9 +54,9 @@ func OpenDB(config *config.Database, logger hclog.Logger) (domain.Repository, er
return repository, nil
}

func createDB(config *config.Database, logger hclog.Logger) (*gorm.DB, dbLock, error) {
func createDB(config *config.Database, logger *zap.Logger) (*gorm.DB, dbLock, error) {
gormConfig := &gorm.Config{
Logger: &GormLoggerAdapter{logger: logger.Named("db")},
Logger: &GormLoggerAdapter{logger: logger.Sugar()},
}

switch config.Type {
Expand Down Expand Up @@ -134,42 +134,42 @@ func createJSONWebKeySet(ctx context.Context, repository domain.Repository) erro
}

type GormLoggerAdapter struct {
logger hclog.Logger
logger *zap.SugaredLogger
}

func (g *GormLoggerAdapter) LogMode(level logger.LogLevel) logger.Interface {
return g
}

func (g *GormLoggerAdapter) Info(ctx context.Context, s string, i ...interface{}) {
g.logger.Info(s, i)
g.logger.Infow(s, i)
}

func (g *GormLoggerAdapter) Warn(ctx context.Context, s string, i ...interface{}) {
g.logger.Warn(s, i)
g.logger.Warnw(s, i)
}

func (g *GormLoggerAdapter) Error(ctx context.Context, s string, i ...interface{}) {
g.logger.Error(s, i)
}

func (g *GormLoggerAdapter) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
if g.logger.IsTrace() {
if g.logger.Level().Enabled(zap.DebugLevel) {
elapsed := time.Since(begin)
switch {
case err != nil && !errors.Is(err, gorm.ErrRecordNotFound):
sql, rows := fc()
if rows == -1 {
g.logger.Trace("Error executing query", "sql", sql, "start_time", begin.Format(time.RFC3339), "duration", elapsed, "err", err)
g.logger.Debugw("Error executing query", "sql", sql, "start_time", begin.Format(time.RFC3339), "duration", elapsed, "err", err)
} else {
g.logger.Trace("Error executing query", "sql", sql, "start_time", begin.Format(time.RFC3339), "duration", elapsed, "rows", rows, "err", err)
g.logger.Debugw("Error executing query", "sql", sql, "start_time", begin.Format(time.RFC3339), "duration", elapsed, "rows", rows, "err", err)
}
default:
sql, rows := fc()
if rows == -1 {
g.logger.Trace("Statement executed", "sql", sql, "start_time", begin.Format(time.RFC3339), "duration", elapsed)
g.logger.Debugw("Statement executed", "sql", sql, "start_time", begin.Format(time.RFC3339), "duration", elapsed)
} else {
g.logger.Trace("Statement executed", "sql", sql, "start_time", begin.Format(time.RFC3339), "duration", elapsed, "rows", rows)
g.logger.Debugw("Statement executed", "sql", sql, "start_time", begin.Format(time.RFC3339), "duration", elapsed, "rows", rows)
}
}
}
Expand Down
42 changes: 0 additions & 42 deletions internal/errors/errors.go

This file was deleted.

Loading

0 comments on commit 4ba540c

Please sign in to comment.