1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-03 02:56:09 +08:00
Files
kubo/core/commands/log.go
Jeromy 8f0623255d replace imports with absolute path instead of using symlink
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
2015-10-03 14:30:50 -07:00

89 lines
2.2 KiB
Go

package commands
import (
"fmt"
"io"
cmds "github.com/ipfs/go-ipfs/commands"
logging "github.com/ipfs/go-ipfs/vendor/QmXJkcEXB6C9h6Ytb6rrUTFU56Ro62zxgrbxTT3dgjQGA8/go-log"
)
// Golang os.Args overrides * and replaces the character argument with
// an array which includes every file in the user's CWD. As a
// workaround, we use 'all' instead. The util library still uses * so
// we convert it at this step.
var logAllKeyword = "all"
var LogCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Interact with the daemon log output",
ShortDescription: `
'ipfs log' contains utility commands to affect or read the logging
output of a running daemon.
`,
},
Subcommands: map[string]*cmds.Command{
"level": logLevelCmd,
"tail": logTailCmd,
},
}
var logLevelCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Change the logging level",
ShortDescription: `
'ipfs log level' is a utility command used to change the logging
output of a running daemon.
`,
},
Arguments: []cmds.Argument{
// TODO use a different keyword for 'all' because all can theoretically
// clash with a subsystem name
cmds.StringArg("subsystem", true, false, fmt.Sprintf("the subsystem logging identifier. Use '%s' for all subsystems.", logAllKeyword)),
cmds.StringArg("level", true, false, "one of: debug, info, warning, error, fatal, panic"),
},
Run: func(req cmds.Request, res cmds.Response) {
args := req.Arguments()
subsystem, level := args[0], args[1]
if subsystem == logAllKeyword {
subsystem = "*"
}
if err := logging.SetLogLevel(subsystem, level); err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
s := fmt.Sprintf("Changed log level of '%s' to '%s'\n", subsystem, level)
log.Info(s)
res.SetOutput(&MessageOutput{s})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: MessageTextMarshaler,
},
Type: MessageOutput{},
}
var logTailCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Read the logs",
ShortDescription: `
'ipfs log tail' is a utility command used to read log output as it is written.
`,
},
Run: func(req cmds.Request, res cmds.Response) {
r, w := io.Pipe()
logging.WriterGroup.AddWriter(w)
go func() {
<-req.Context().Done()
w.Close()
}()
res.SetOutput(r)
},
}