Skip to content

Commit

Permalink
chore: debug level for query log (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
max107 authored Sep 10, 2023
1 parent 838a3bc commit 63de2b1
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions extra/bunzerolog/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"errors"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/uptrace/bun"
)

Expand All @@ -21,19 +22,22 @@ func (h *QueryHook) BeforeQuery(ctx context.Context, _ *bun.QueryEvent) context.

// AfterQuery after query zerolog hook.
func (h *QueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
var logEvent *zerolog.Event

// do not log sql.ErrNoRows as real error
l := zerolog.Ctx(ctx)
if errors.Is(event.Err, sql.ErrNoRows) {
logEvent = l.Warn().Err(event.Err)
} else {
logEvent = l.Err(event.Err)
}

logEvent.
l := log.Ctx(ctx).With().
Str("query", event.Query).
Str("operation", event.Operation()).
Str("duration", time.Since(event.StartTime).String()).
Msg("query")
Logger()

if event.Err != nil {
// do not log sql.ErrNoRows as real error
if errors.Is(event.Err, sql.ErrNoRows) {
l.Warn().Err(event.Err).Send()
return
}

l.Err(event.Err).Send()
return
}

l.Debug().Send()
}

0 comments on commit 63de2b1

Please sign in to comment.