diff --git a/plugin/loader/preload.go b/plugin/loader/preload.go index 4c6209a11..4be60625f 100644 --- a/plugin/loader/preload.go +++ b/plugin/loader/preload.go @@ -5,7 +5,7 @@ import ( pluginflatfs "github.com/ipfs/go-ipfs/plugin/plugins/flatfs" pluginipldgit "github.com/ipfs/go-ipfs/plugin/plugins/git" pluginlevelds "github.com/ipfs/go-ipfs/plugin/plugins/levelds" - pluginpeeridlog "github.com/ipfs/go-ipfs/plugin/plugins/peeridlog" + pluginpeerlog "github.com/ipfs/go-ipfs/plugin/plugins/peerlog" ) // DO NOT EDIT THIS FILE @@ -17,5 +17,5 @@ func init() { Preload(pluginbadgerds.Plugins...) Preload(pluginflatfs.Plugins...) Preload(pluginlevelds.Plugins...) - Preload(pluginpeeridlog.Plugins...) + Preload(pluginpeerlog.Plugins...) } diff --git a/plugin/loader/preload_list b/plugin/loader/preload_list index d92a4a612..38349cc43 100644 --- a/plugin/loader/preload_list +++ b/plugin/loader/preload_list @@ -8,4 +8,4 @@ ipldgit github.com/ipfs/go-ipfs/plugin/plugins/git * badgerds github.com/ipfs/go-ipfs/plugin/plugins/badgerds * flatfs github.com/ipfs/go-ipfs/plugin/plugins/flatfs * levelds github.com/ipfs/go-ipfs/plugin/plugins/levelds * -peeridlog github.com/ipfs/go-ipfs/plugin/plugins/peeridlog * +peerlog github.com/ipfs/go-ipfs/plugin/plugins/peerlog * diff --git a/plugin/plugins/peeridlog/peeridlog.go b/plugin/plugins/peeridlog/peeridlog.go deleted file mode 100644 index 2117303af..000000000 --- a/plugin/plugins/peeridlog/peeridlog.go +++ /dev/null @@ -1,44 +0,0 @@ -package peeridlog - -import ( - "fmt" - - core "github.com/ipfs/go-ipfs/core" - plugin "github.com/ipfs/go-ipfs/plugin" -) - -// Plugins is exported list of plugins that will be loaded -var Plugins = []plugin.Plugin{ - &peerIDLogPlugin{}, -} - -// Log all the PeerIDs we connect to. -type peerIDLogPlugin struct{} - -var _ plugin.PluginDaemonInternal = (*peerIDLogPlugin)(nil) - -// Name returns the plugin's name, satisfying the plugin.Plugin interface. -func (*peerIDLogPlugin) Name() string { - return "peeridlog" -} - -// Version returns the plugin's version, satisfying the plugin.Plugin interface. -func (*peerIDLogPlugin) Version() string { - return "0.1.0" -} - -// Init initializes plugin, satisfying the plugin.Plugin interface. Put any -// initialization logic here. -func (*peerIDLogPlugin) Init(*plugin.Environment) error { - return nil -} - -func (*peerIDLogPlugin) Start(*core.IpfsNode) error { - fmt.Println("peerIDLogPlugin HELLO!") - return nil -} - -func (*peerIDLogPlugin) Close() error { - fmt.Println("peerIDLogPlugin GOODBYE!") - return nil -} diff --git a/plugin/plugins/peerlog/peerlog.go b/plugin/plugins/peerlog/peerlog.go new file mode 100644 index 000000000..b5b78c086 --- /dev/null +++ b/plugin/plugins/peerlog/peerlog.go @@ -0,0 +1,67 @@ +package peerlog + +import ( + "fmt" + + core "github.com/ipfs/go-ipfs/core" + plugin "github.com/ipfs/go-ipfs/plugin" + logging "github.com/ipfs/go-log" + network "github.com/libp2p/go-libp2p-core/network" +) + +var log = logging.Logger("plugin/peerlog") + +// Log all the PeerIDs we see +// +// Usage: +// GOLOG_FILE=~/peer.log IPFS_LOGGING_FMT=json ipfs daemon +// Output: +// {"level":"info","ts":"2020-02-10T13:54:26.639Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:51","msg":"connected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"} +// {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"disconnected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"} +// +type peerLogPlugin struct{} + +var _ plugin.PluginDaemonInternal = (*peerLogPlugin)(nil) + +// Plugins is exported list of plugins that will be loaded +var Plugins = []plugin.Plugin{ + &peerLogPlugin{}, +} + +// Name returns the plugin's name, satisfying the plugin.Plugin interface. +func (*peerLogPlugin) Name() string { + return "peerlog" +} + +// Version returns the plugin's version, satisfying the plugin.Plugin interface. +func (*peerLogPlugin) Version() string { + return "0.1.0" +} + +// Init initializes plugin +func (*peerLogPlugin) Init(*plugin.Environment) error { + fmt.Println("peerLogPlugin enabled - PeerIDs will be logged") + return nil +} + +func (*peerLogPlugin) Start(node *core.IpfsNode) error { + // Ensure logs from this plugin get printed regardless of global IPFS_LOGGING value + logging.SetLogLevel("plugin/peerlog", "info") + var notifee network.NotifyBundle + notifee.ConnectedF = func(net network.Network, conn network.Conn) { + log.Infow("connected", + "peer", conn.RemotePeer().Pretty(), + ) + } + notifee.DisconnectedF = func(net network.Network, conn network.Conn) { + log.Infow("disconnected", + "peer", conn.RemotePeer().Pretty(), + ) + } + node.PeerHost.Network().Notify(¬ifee) + return nil +} + +func (*peerLogPlugin) Close() error { + return nil +}