8000 chore: use go-log/v2 by gammazero · Pull Request #10801 · ipfs/kubo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: use go-log/v2 #10801

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions cmd/ipfs/kubo/pinmfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"os"
"time"

"github.com/libp2p/go-libp2p/core/host"
peer "github.com/libp2p/go-libp2p/core/peer"

pinclient "github.com/ipfs/boxo/pinning/remote/client"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"

config "github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core"
"github.com/libp2p/go-libp2p/core/host"
peer "github.com/libp2p/go-libp2p/core/peer"
)

// mfslog is the logger for remote mfs pinning.
Expand Down
10 changes: 5 additions & 5 deletions cmd/ipfs/kubo/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import (
"time"

"github.com/blang/semver/v4"
"github.com/google/uuid"
u "github.com/ipfs/boxo/util"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/go-ipfs-cmds/cli"
cmdhttp "github.com/ipfs/go-ipfs-cmds/http"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
ipfs "github.com/ipfs/kubo"
"github.com/ipfs/kubo/client/rpc/auth"
"github.com/ipfs/kubo/cmd/ipfs/util"
Expand Down Expand Up @@ -89,6 +88,7 @@ func printErr(err error) int {
return 1
}

/*
func newUUID(key string) logging.Metadata {
ids := "#UUID-ERROR#"
if id, err := uuid.NewRandom(); err == nil {
Expand All @@ -97,7 +97,8 @@ func newUUID(key string) logging.Metadata {
return logging.Metadata{
key: ids,
}
}
}
*/

func BuildDefaultEnv(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
return BuildEnv(nil)(ctx, req)
Expand Down Expand Up @@ -157,8 +158,7 @@ func BuildEnv(pl PluginPreloader) func(ctx context.Context, req *cmds.Request) (
// - output the response
// - if anything fails, print error, maybe with help.
func Start(buildEnv func(ctx context.Context, req *cmds.Request) (cmds.Environment, error)) (exitCode int) {
ctx := logging.ContextWithLoggable(context.Background(), newUUID("session"))

ctx := context.Background()
tp, err := tracing.NewTracerProvider(ctx)
if err != nil {
return printErr(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/util/ulimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"syscall"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
)

var log = logging.Logger("ulimit")
Expand Down
2 changes: 1 addition & 1 deletion commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
loader "github.com/ipfs/kubo/plugin/loader"

cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
config "github.com/ipfs/kubo/config"
coreiface "github.com/ipfs/kubo/core/coreiface"
options "github.com/ipfs/kubo/core/coreiface/options"
Expand Down
2 changes: 1 addition & 1 deletion core/commands/cmdenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ipfs/kubo/core"

cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
coreiface "github.com/ipfs/kubo/core/coreiface"
options "github.com/ipfs/kubo/core/coreiface/options"
)
Expand Down
2 changes: 1 addition & 1 deletion core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
cidenc "github.com/ipfs/go-cidutil/cidenc"
cmds "github.com/ipfs/go-ipfs-cmds"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
iface "github.com/ipfs/kubo/core/coreiface"
mh "github.com/multiformats/go-multihash"
)
Expand Down
33 changes: 23 additions & 10 deletions core/commands/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"io"

cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
lwriter "github.com/ipfs/go-log/writer"
logging "github.com/ipfs/go-log/v2"
)

// Golang os.Args overrides * and replaces the character argument with
Expand Down Expand Up @@ -104,25 +103,39 @@ subsystems of a running daemon.
Type: stringList{},
}

const logLevelOption = "log-level"

var logTailCmd = &cmds.Command{
Status: cmds.Experimental,
Helptext: cmds.HelpText{
Tagline: "Read the event log.",
ShortDescription: `
Outputs event log messages (not other log messages) as they are generated.

Currently broken. Follow https://github.com/ipfs/kubo/issues/9245 for updates.
`,
},

Options: []cmds.Option{
// FIXME: The PipeReader doesn't support per-subsystem log levels like
// https://github.com/ipfs/go-log#golog_log_level, just a global one.
cmds.StringOption(logLevelOption, "Log level to listen to.").WithDefault(""),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := req.Context
r, w := io.Pipe()
var pipeReader *logging.PipeReader
logLevelString, _ := req.Options[logLevelOption].(string)
if logLevelString != "" {
logLevel, err := logging.LevelFromString(logLevelString)
if err != nil {
return fmt.Errorf("setting log level %s: %w", logLevelString, err)
}
pipeReader = logging.NewPipeReader(logging.PipeLevel(logLevel))
} else {
pipeReader = logging.NewPipeReader()
}

go func() {
defer w.Close()
<-ctx.Done()
<-req.Context.Done()
pipeReader.Close()
}()
lwriter.WriterGroup.AddWriter(w)
return res.Emit(r)
return res.Emit(pipeReader)
},
}
2 changes: 1 addition & 1 deletion core/commands/name/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
options "github.com/ipfs/kubo/core/coreiface/options"
)
Expand Down
2 changes: 1 addition & 1 deletion core/commands/pin/remotepin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
pinclient "github.com/ipfs/boxo/pinning/remote/client"
cid "github.com/ipfs/go-cid"
cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
config "github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core/commands/cmdenv"
"github.com/ipfs/kubo/core/commands/cmdutils"
Expand Down
2 changes: 1 addition & 1 deletion core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ipfs/kubo/core/commands/pin"

cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
)

var log = logging.Logger("core/commands")
Expand Down
2 changes: 1 addition & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
pathresolver "github.com/ipfs/boxo/path/resolver"
provider "github.com/ipfs/boxo/provider"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
goprocess "github.com/jbenet/goprocess"
ddht "github.com/libp2p/go-libp2p-kad-dht/dual"
pubsub "github.com/libp2p/go-libp2p-pubsub"
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/corehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"net/http"
"time"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
core "github.com/ipfs/kubo/core"
"github.com/jbenet/goprocess"
periodicproc "github.com/jbenet/goprocess/periodic"
Expand Down
95 changes: 53 additions & 42 deletions core/corehttp/logs.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
package corehttp

import (
"io"
"fmt"
"net"
"net/http"

lwriter "github.com/ipfs/go-log/writer"
logging "github.com/ipfs/go-log/v2"
core "github.com/ipfs/kubo/core"
)

type writeErrNotifier struct {
w io.Writer
errs chan error
}

func newWriteErrNotifier(w io.Writer) (io.WriteCloser, <-chan error) {
ch := make(chan error, 1)
return &writeErrNotifier{
w: w,
errs: ch,
}, ch
}

func (w *writeErrNotifier) Write(b []byte) (int, error) {
n, err := w.w.Write(b)
if err != nil {
select {
case w.errs <- err:
default:
}
}
if f, ok := w.w.(http.Flusher); ok {
f.Flush()
}
return n, err
}

func (w *writeErrNotifier) Close() error {
select {
case w.errs <- io.EOF:
default:
}
return nil
}

func LogOption() ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
wnf, errs := newWriteErrNotifier(w)
lwriter.WriterGroup.AddWriter(wnf)
log.Event(n.Context(), "log API client connected") //nolint deprecated
<-errs
// The log data comes from an io.Reader, and we need to constantly
// read from it and then write to the HTTP response.
pipeReader := logging.NewPipeReader()
done := make(chan struct{})

// Close the pipe reader if the request context is canceled. This
// is necessary to avoiding blocking on reading from the pipe
// reader when the client terminates the request.
go func() {
select {
case <-r.Context().Done(): // Client canceled request
case <-n.Context().Done(): // Node shutdown
case <-done: // log reader goroutine exitex
}
pipeReader.Close()
}()

errs := make(chan error, 1)

go func() {
defer close(errs)
defer close(done)
buf := make([]byte, 4096)
for {
// Read log data into limited size buffer and send buffer to client.
size, err := pipeReader.Read(buf)
if err != nil {
errs <- fmt.Errorf("error reading log event: %s", err)
return
}
_, err = w.Write(buf[:size])
if err != nil {
// Failed to write to client, probably disconnected.
return
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
if r.Context().Err() != nil {
return
}
if n.Context().Err() != nil {
return
}
}
}()
err := <-errs
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
return mux, nil
}
Expand Down
2 changes: 1 addition & 1 deletion core/corerepo/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/ipfs/boxo/mfs"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
)

var log = logging.Logger("corerepo")
Expand Down
2 changes: 1 addition & 1 deletion core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
pin "github.com/ipfs/boxo/pinning/pinner"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
coreiface "github.com/ipfs/kubo/core/coreiface"

"github.com/ipfs/kubo/tracing"
Expand Down
2 changes: 1 addition & 1 deletion core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
offline "github.com/ipfs/boxo/exchange/offline"
uio "github.com/ipfs/boxo/ipld/unixfs/io"
util "github.com/ipfs/boxo/util"
"github.com/ipfs/go-log"
"github.com/ipfs/go-log/v2"
"github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core/node/libp2p"
"github.com/ipfs/kubo/p2p"
Expand Down
2 changes: 1 addition & 1 deletion core/node/libp2p/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"time"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
version "github.com/ipfs/kubo"
"github.com/ipfs/kubo/config"
p2pforge "github.com/ipshipyard/p2p-forge/client"
Expand Down
2 changes: 1 addition & 1 deletion core/node/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
version "github.com/ipfs/kubo"
config "github.com/ipfs/kubo/config"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down
Loading
Loading
0