log.go 1.41 KB
Newer Older
1 2 3 4 5 6 7 8 9
package commands

import (
	"fmt"

	cmds "github.com/jbenet/go-ipfs/commands"
	u "github.com/jbenet/go-ipfs/util"
)

10 11 12 13 14 15
// 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"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
var LogCmd = &cmds.Command{
17 18 19 20
	Helptext: cmds.HelpText{
		Tagline: "Change the logging level",
		ShortDescription: `
'ipfs log' is a utility command used to change the logging
21 22
output of a running daemon.
`,
23
	},
24

25
	Arguments: []cmds.Argument{
26 27 28
		// 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)),
29
		cmds.StringArg("level", true, false, "one of: debug, info, notice, warning, error, critical"),
30
	},
31
	Run: func(req cmds.Request) (interface{}, error) {
32

33
		args := req.Arguments()
34
		subsystem, level := args[0], args[1]
35 36 37 38 39 40

		if subsystem == logAllKeyword {
			subsystem = "*"
		}

		if err := u.SetLogLevel(subsystem, level); err != nil {
41
			return nil, err
42 43
		}

44
		s := fmt.Sprintf("Changed log level of '%s' to '%s'", subsystem, level)
45
		log.Info(s)
46
		return &MessageOutput{s}, nil
47
	},
48 49
	Marshalers: cmds.MarshalerMap{
		cmds.Text: MessageTextMarshaler,
50
	},
51
	Type: MessageOutput{},
52
}