From 40ea6a5080c583ddaefdef840a296cc64297e566 Mon Sep 17 00:00:00 2001 From: cthuang Date: Tue, 11 Oct 2022 16:48:36 +0100 Subject: [PATCH] TUN-6829: Allow user of datagramsession to control logging level of errors --- datagramsession/session.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/datagramsession/session.go b/datagramsession/session.go index 94562673..7e0131da 100644 --- a/datagramsession/session.go +++ b/datagramsession/session.go @@ -24,6 +24,13 @@ func SessionIdleErr(timeout time.Duration) error { type transportSender func(session *packet.Session) error +// ErrVithVariableSeverity are errors that have variable severity +type ErrVithVariableSeverity interface { + error + // LogLevel return the severity of this error + LogLevel() zerolog.Level +} + // Session is a bidirectional pipe of datagrams between transport and dstConn // Destination can be a connection with origin or with eyeball // When the destination is origin: @@ -53,7 +60,11 @@ func (s *Session) Serve(ctx context.Context, closeAfterIdle time.Duration) (clos if errors.Is(err, net.ErrClosed) { s.log.Debug().Msg("Destination connection closed") } else { - s.log.Error().Err(err).Msg("Failed to send session payload from destination to transport") + level := zerolog.ErrorLevel + if variableErr, ok := err.(ErrVithVariableSeverity); ok { + level = variableErr.LogLevel() + } + s.log.WithLevel(level).Err(err).Msg("Failed to send session payload from destination to transport") } if closeSession { s.closeChan <- err