Skip to content

Commit

Permalink
go/logging: libp2p log adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Mar 8, 2022
1 parent 6743e38 commit 8dea1a1
Show file tree
Hide file tree
Showing 5 changed files with 549 additions and 26 deletions.
10 changes: 0 additions & 10 deletions .buildkite/code.pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ steps:
# runs only on non-SGX agents.
OASIS_EXCLUDE_E2E: e2e/runtime/trust-root,e2e/runtime/txsource-multi-short
TEST_BASE_DIR: /tmp
# libp2p logging.
IPFS_LOGGING: debug
retry:
<<: *retry_agent_failure
plugins:
Expand All @@ -251,8 +249,6 @@ steps:
env:
OASIS_E2E_COVERAGE: enable
TEST_BASE_DIR: /tmp
# libp2p logging.
IPFS_LOGGING: debug
agents:
buildkite_agent_class: ephemeral # XXX: Use a dedicated tag instead.
retry:
Expand Down Expand Up @@ -287,8 +283,6 @@ steps:
OASIS_UNSAFE_ALLOW_DEBUG_ENCLAVES: "1"
OASIS_E2E_COVERAGE: enable
TEST_BASE_DIR: /tmp
# libp2p logging.
IPFS_LOGGING: debug
agents:
queue: intel-sgx
retry:
Expand Down Expand Up @@ -320,8 +314,6 @@ steps:
OASIS_E2E_COVERAGE: enable
OASIS_EXCLUDE_E2E: e2e/runtime/txsource-multi,e2e/runtime/txsource-multi-short
TEST_BASE_DIR: /tmp
# libp2p logging.
IPFS_LOGGING: debug
agents:
queue: intel-sgx
retry:
Expand All @@ -347,8 +339,6 @@ steps:
env:
OASIS_E2E_COVERAGE: enable
TEST_BASE_DIR: /tmp
# libp2p logging.
IPFS_LOGGING: debug
agents:
queue: intel-sgx
retry:
Expand Down
2 changes: 0 additions & 2 deletions .buildkite/longtests.pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ steps:
- .buildkite/scripts/daily_txsource.sh --e2e/runtime.epoch.interval=${epochtime_inverval}
env:
TEST_BASE_DIR: /var/tmp/longtests
# libp2p logging.
IPFS_LOGGING: debug
agents:
daily: true
# NOTE: we actually don't want to retry, but this is the only way that we
Expand Down
33 changes: 19 additions & 14 deletions go/common/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ package logging
import (
"fmt"
"io"
"io/ioutil"
goLog "log"
"sort"
"strings"
"sync"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
ipfsLog "github.com/ipfs/go-log/v2"
"github.com/spf13/pflag"

goLogging "github.com/whyrusleeping/go-logging"
)

var (
Expand Down Expand Up @@ -228,14 +225,18 @@ func Initialize(w io.Writer, format Format, defaultLvl Level, moduleLvls map[str
return fmt.Errorf("logging: already initialized")
}

var syncW io.Writer
if w != nil {
syncW = log.NewSyncWriter(w)
}

var logger log.Logger = backend.baseLogger
if w != nil {
w = log.NewSyncWriter(w)
switch format {
case FmtLogfmt:
logger = log.NewLogfmtLogger(w)
logger = log.NewLogfmtLogger(syncW)
case FmtJSON:
logger = log.NewJSONLogger(w)
logger = log.NewJSONLogger(syncW)
default:
return fmt.Errorf("logging: unsupported log format: %v", format)
}
Expand All @@ -262,13 +263,17 @@ func Initialize(w io.Writer, format Format, defaultLvl Level, moduleLvls map[str
backend.earlyLoggers = nil

// libp2p/IPFS uses yet another logging library, that appears to be a
// wrapper around go-logging. Because it's quality IPFS code, it's
// configured via env vars, from the package `init()`.
//
// Till we can write a nice wrapper around it, to make it do what we
// want, reach into the underlying library and squelch it.
goLogging.Reset()
_ = goLogging.SetBackend(goLogging.NewLogBackend(ioutil.Discard, "libp2p", goLog.LstdFlags))
// wrapper around zap.
// Create a zap core wrapper.
// Note: We cannot use GetLoggerEx here, since we already hold the lock.
ipfsLogger := newZapCore(logger, "libp2p", 7)
backend.setupLogLevelLocked(ipfsLogger.logger)
// Update the ipfs core logger.
ipfsLog.SetPrimaryCore(ipfsLogger)

// Enable all logs on the ipfs level.
// zapCore will filter logs based on the configured logging level of the oasis node.
ipfsLog.SetDebugLogging()

return nil
}
Expand Down
Loading

0 comments on commit 8dea1a1

Please sign in to comment.