Changing the logging of the libp2p stack

It appears that the Golang libp2p libs are logging to stdout/stderr on its own even though I haven’t configured a logger.

I use the logrus logger. GitHub - sirupsen/logrus: Structured, pluggable logging for Go.

Is it possible to hook my own logging into the libp2p stack somehow?

For example, is there a way to register a callback or provide an interface that will be called for logging?

@leighmcculloch we’ve ran into some quirks that sent us spinning looking for answers, and turning on logging was also needed. From what I’ve managed to reverse engineer from the logger setup in the github.com/ipfs/go-log library that is used in the libp2p projects, the relevant code section is in the setup.go file:

func init() {
	SetupLogging(configFromEnv())
}

// Logging environment variables
const (
	// IPFS_* prefixed env vars kept for backwards compatibility
	// for this release. They will not be available in the next
	// release.
	//
	// GOLOG_* env vars take precedences over IPFS_* env vars.
	envIPFSLogging    = "IPFS_LOGGING"
	envIPFSLoggingFmt = "IPFS_LOGGING_FMT"

	envLogging    = "GOLOG_LOG_LEVEL"
	envLoggingFmt = "GOLOG_LOG_FMT"

	envLoggingFile = "GOLOG_FILE" // /path/to/file
	envLoggingURL  = "GOLOG_URL"  // url that will be processed by sink in the zap

	envLoggingOutput = "GOLOG_OUTPUT"     // possible values: stdout|stderr|file combine multiple values with '+'
	envLoggingLabels = "GOLOG_LOG_LABELS" // comma-separated key-value pairs, i.e. "app=example_app,dc=sjc-1"
)

We’ve had success setting the environment variables to the following values:

GOLOG_LOG_LEVEL=ERROR
GOLOG_OUTPUT=stdout

Dig deeper and you’ll find the other supported logging levels in the uber zap library:

// LevelFromString parses a string-based level and returns the corresponding
// LogLevel.
//
// Supported strings are: DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL, and
// their lower-case forms.
//
// The returned LogLevel must be discarded if error is not nil.
func LevelFromString(level string) (LogLevel, error) {
	lvl := zapcore.InfoLevel // zero value
	err := lvl.Set(level)
	return LogLevel(lvl), err
}

Enjoy

1 Like

Most of the libp2p libraries use go-log. Have a look at the API there.
Does that solve your problem?