log.go 2.22 KB
Newer Older
1 2 3 4
package commands

import (
	"fmt"
5
	"io"
6

7
	cmds "github.com/ipfs/go-ipfs/commands"
Jeromy's avatar
Jeromy committed
8
	logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
9 10
)

11 12 13 14 15 16
// 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
17
var LogCmd = &cmds.Command{
18 19 20 21 22 23 24 25 26 27
	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,
28
		"tail":  logTailCmd,
29 30 31 32
	},
}

var logLevelCmd = &cmds.Command{
33 34 35
	Helptext: cmds.HelpText{
		Tagline: "Change the logging level",
		ShortDescription: `
36
'ipfs log level' is a utility command used to change the logging
37 38
output of a running daemon.
`,
39
	},
40

41
	Arguments: []cmds.Argument{
42 43 44
		// 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)),
rht's avatar
rht committed
45
		cmds.StringArg("level", true, false, "one of: debug, info, warning, error, fatal, panic"),
46
	},
47
	Run: func(req cmds.Request, res cmds.Response) {
48

49
		args := req.Arguments()
50
		subsystem, level := args[0], args[1]
51 52 53 54 55

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

Jeromy's avatar
Jeromy committed
56
		if err := logging.SetLogLevel(subsystem, level); err != nil {
57 58
			res.SetError(err, cmds.ErrNormal)
			return
59 60
		}

61
		s := fmt.Sprintf("Changed log level of '%s' to '%s'\n", subsystem, level)
62
		log.Info(s)
63
		res.SetOutput(&MessageOutput{s})
64
	},
65 66
	Marshalers: cmds.MarshalerMap{
		cmds.Text: MessageTextMarshaler,
67
	},
68
	Type: MessageOutput{},
69
}
70

71
var logTailCmd = &cmds.Command{
72 73 74
	Helptext: cmds.HelpText{
		Tagline: "Read the logs",
		ShortDescription: `
75
'ipfs log tail' is a utility command used to read log output as it is written.
76 77 78
`,
	},

79
	Run: func(req cmds.Request, res cmds.Response) {
80
		ctx := req.Context()
Jeromy's avatar
Jeromy committed
81
		r, w := io.Pipe()
82
		go func() {
83 84
			defer w.Close()
			<-ctx.Done()
85
		}()
86
		logging.WriterGroup.AddWriter(w)
Jeromy's avatar
Jeromy committed
87
		res.SetOutput(r)
88 89
	},
}