Skip to content

Commit

Permalink
cleanup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Mar 8, 2024
1 parent 33f5df3 commit 460785b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
8 changes: 4 additions & 4 deletions pool/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,21 @@ func (ch *Connection) shutdownErr() error {
return ch.ctx.Err()
}

func (ch *Connection) cLog() logging.Logger {
func (ch *Connection) clog() logging.Logger {
return ch.log.WithFields(map[string]any{
"connection": ch.name,
"address": ch.addr,
})
}

func (ch *Connection) info(a ...any) {
ch.cLog().Info(a...)
ch.clog().Info(a...)
}

func (ch *Connection) warn(err error, a ...any) {
ch.cLog().WithField("error", err.Error()).Warn(a...)
ch.clog().WithError(err).Warn(a...)
}

func (ch *Connection) debug(a ...any) {
ch.cLog().Debug(a...)
ch.clog().Debug(a...)
}
29 changes: 19 additions & 10 deletions pool/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,14 @@ func (s *Session) close() (err error) {
return nil
}

s.debug("canceling consumers...")
for consumer := range s.consumers {
// ignore error, as at this point we cannot do anything about the error
// tell server to cancel consumer deliveries.
cerr := s.channel.Cancel(consumer, false)
err = errors.Join(err, cerr)
if len(s.consumers) > 0 {
s.debug("canceling consumers...")
for consumer := range s.consumers {
// ignore error, as at this point we cannot do anything about the error
// tell server to cancel consumer deliveries.
cerr := s.channel.Cancel(consumer, false)
err = errors.Join(err, cerr)
}
}

if s.error() != nil {
Expand Down Expand Up @@ -1482,20 +1484,27 @@ func (s *Session) shutdownErr() error {
return s.ctx.Err()
}

func (s *Session) slog() logging.Logger {
return s.log.WithFields(logging.Fields{
"connection": s.conn.Name(),
"session": s.name,
})
}

func (s *Session) info(a ...any) {
s.log.WithField("connection", s.conn.Name()).WithField("session", s.Name()).Info(a...)
s.slog().Info(a...)
}

func (s *Session) warn(err error, a ...any) {
s.log.WithField("connection", s.conn.Name()).WithField("session", s.Name()).WithField("error", err.Error()).Warn(a...)
s.slog().WithError(err).Warn(a...)
}

func (s *Session) warnf(err error, format string, a ...any) {
s.log.WithField("connection", s.conn.Name()).WithField("session", s.Name()).WithField("error", err.Error()).Warnf(format, a...)
s.slog().WithError(err).Warnf(format, a...)
}

func (s *Session) debug(a ...any) {
s.log.WithField("connection", s.conn.Name()).WithField("session", s.Name()).Debug(a...)
s.slog().Debug(a...)
}

// flush should be called when you return the session back to the pool
Expand Down

0 comments on commit 460785b

Please sign in to comment.