log.go 2.18 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
	"github.com/ipfs/go-ipfs/thirdparty/eventlog"
9
	u "github.com/ipfs/go-ipfs/util"
10 11
)

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

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

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

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

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

		if err := u.SetLogLevel(subsystem, level); err != nil {
58 59
			res.SetError(err, cmds.ErrNormal)
			return
60 61
		}

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

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

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