From 11dc650b699ca16b3299265eb53a9f8ae9e4ca67 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Sun, 16 Jun 2019 00:11:43 -0700 Subject: [PATCH] Factor stream logging into one function --- internal/utils/connlogger.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/internal/utils/connlogger.go b/internal/utils/connlogger.go index 271023a4e..c6aa7ee91 100644 --- a/internal/utils/connlogger.go +++ b/internal/utils/connlogger.go @@ -28,6 +28,10 @@ func connLogger(c net.Conn, l log.Logger) log.Logger { "connRemoteAddr", c.RemoteMultiaddr()) } +func streamLogger(s net.Stream, l log.Logger) log.Logger { + return connLogger(s.Conn(), l).New("streamProtocolID", s.Protocol()) +} + func (cl ConnLogger) Listen(n net.Network, ma ma.Multiaddr) { WithCaller(netLogger(n, cl.l)).Debug("listener starting", "addr", ma) } @@ -45,21 +49,11 @@ func (cl ConnLogger) Disconnected(n net.Network, c net.Conn) { } func (cl ConnLogger) OpenedStream(n net.Network, s net.Stream) { - conn := s.Conn() - WithCaller(netLogger(n, cl.l)).Debug("stream opened", - "localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), - "remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), - "protocol", s.Protocol(), - ) + WithCaller(streamLogger(s, netLogger(n, cl.l))).Debug("stream opened") } func (cl ConnLogger) ClosedStream(n net.Network, s net.Stream) { - conn := s.Conn() - WithCaller(netLogger(n, cl.l)).Debug("stream closed", - "localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), - "remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), - "protocol", s.Protocol(), - ) + WithCaller(streamLogger(s, netLogger(n, cl.l))).Debug("stream closed") } // NewConnLogger returns a new connection logger that uses the given