Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add peerlog plugin #6887

Merged
merged 5 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugin/loader/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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"
pluginpeerlog "github.com/ipfs/go-ipfs/plugin/plugins/peerlog"
)

// DO NOT EDIT THIS FILE
Expand All @@ -16,4 +17,5 @@ func init() {
Preload(pluginbadgerds.Plugins...)
Preload(pluginflatfs.Plugins...)
Preload(pluginlevelds.Plugins...)
Preload(pluginpeerlog.Plugins...)
}
1 change: 1 addition & 0 deletions plugin/loader/preload_list
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +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 *
peerlog github.com/ipfs/go-ipfs/plugin/plugins/peerlog *
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
67 changes: 67 additions & 0 deletions plugin/plugins/peerlog/peerlog.go
Original file line number Diff line number Diff line change
@@ -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")
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
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(&notifee)
return nil
}

func (*peerLogPlugin) Close() error {
return nil
}