Move connection logger to internal/utils

Suggested-by: Leo Chen <leo@harmony.one>
pull/424/head
Eugene Kim 6 years ago
parent 970d3b2f08
commit 02abf88079
  1. 47
      cmd/harmony.go
  2. 57
      internal/utils/connlogger.go

@ -11,7 +11,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
net "github.com/libp2p/go-libp2p-net"
peerstore "github.com/libp2p/go-libp2p-peerstore" peerstore "github.com/libp2p/go-libp2p-peerstore"
multiaddr "github.com/multiformats/go-multiaddr" multiaddr "github.com/multiformats/go-multiaddr"
@ -79,50 +78,6 @@ func loggingInit(logFolder, role, ip, port string, onlyLogTps bool) {
log.Root().SetHandler(h) log.Root().SetHandler(h)
} }
type connLogger struct{}
func (connLogger) Listen(net net.Network, ma multiaddr.Multiaddr) {
log.Debug("[CONNECTIONS] Listener starting", "net", net, "addr", ma)
}
func (connLogger) ListenClose(net net.Network, ma multiaddr.Multiaddr) {
log.Debug("[CONNECTIONS] Listener closing", "net", net, "addr", ma)
}
func (connLogger) Connected(net net.Network, conn net.Conn) {
log.Debug("[CONNECTIONS] Connected", "net", net,
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
)
}
func (connLogger) Disconnected(net net.Network, conn net.Conn) {
log.Debug("[CONNECTIONS] Disconnected", "net", net,
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
)
}
func (connLogger) OpenedStream(net net.Network, stream net.Stream) {
conn := stream.Conn()
log.Debug("[CONNECTIONS] Stream opened", "net", net,
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
"protocol", stream.Protocol(),
)
}
func (connLogger) ClosedStream(net net.Network, stream net.Stream) {
conn := stream.Conn()
log.Debug("[CONNECTIONS] Stream closed", "net", net,
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
"protocol", stream.Protocol(),
)
}
var theConnLogger connLogger
func main() { func main() {
// TODO: use http://getmyipaddress.org/ or http://www.get-myip.com/ to retrieve my IP address // TODO: use http://getmyipaddress.org/ or http://www.get-myip.com/ to retrieve my IP address
ip := flag.String("ip", "127.0.0.1", "IP of the node") ip := flag.String("ip", "127.0.0.1", "IP of the node")
@ -261,7 +216,7 @@ func main() {
host, err := p2pimpl.NewHost(&selfPeer, nodePriKey) host, err := p2pimpl.NewHost(&selfPeer, nodePriKey)
if *logConn { if *logConn {
host.GetP2PHost().Network().Notify(theConnLogger) host.GetP2PHost().Network().Notify(utils.ConnLogger)
} }
if err != nil { if err != nil {
panic("unable to new host in harmony") panic("unable to new host in harmony")

@ -0,0 +1,57 @@
package utils
import (
"github.com/ethereum/go-ethereum/log"
net "github.com/libp2p/go-libp2p-net"
ma "github.com/multiformats/go-multiaddr"
)
type connLogger struct{}
func (connLogger) Listen(net net.Network, ma ma.Multiaddr) {
log.Debug("[CONNECTIONS] Listener starting", "net", net, "addr", ma)
}
func (connLogger) ListenClose(net net.Network, ma ma.Multiaddr) {
log.Debug("[CONNECTIONS] Listener closing", "net", net, "addr", ma)
}
func (connLogger) Connected(net net.Network, conn net.Conn) {
log.Debug("[CONNECTIONS] Connected", "net", net,
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
)
}
func (connLogger) Disconnected(net net.Network, conn net.Conn) {
log.Debug("[CONNECTIONS] Disconnected", "net", net,
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
)
}
func (connLogger) OpenedStream(net net.Network, stream net.Stream) {
conn := stream.Conn()
log.Debug("[CONNECTIONS] Stream opened", "net", net,
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
"protocol", stream.Protocol(),
)
}
func (connLogger) ClosedStream(net net.Network, stream net.Stream) {
conn := stream.Conn()
log.Debug("[CONNECTIONS] Stream closed", "net", net,
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
"protocol", stream.Protocol(),
)
}
// ConnLogger is a LibP2P connection logger.
// Add on a LibP2P host by calling:
//
// host.Network().Notify(utils.ConnLogger)
//
// It logs all listener/connection/stream open/close activities at debug level.
var ConnLogger connLogger
Loading…
Cancel
Save