github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/commands/debuglog.go (about) 1 // Copyright 2013, 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "fmt" 8 "io" 9 10 "github.com/juju/cmd" 11 "github.com/juju/loggo" 12 "launchpad.net/gnuflag" 13 14 "github.com/juju/juju/api" 15 "github.com/juju/juju/cmd/envcmd" 16 ) 17 18 type DebugLogCommand struct { 19 envcmd.EnvCommandBase 20 21 level string 22 params api.DebugLogParams 23 } 24 25 var DefaultLogLocation = "/var/log/juju/all-machines.log" 26 27 // defaultLineCount is the default number of lines to 28 // display, from the end of the consolidated log. 29 const defaultLineCount = 10 30 31 const debuglogDoc = ` 32 Stream the consolidated debug log file. This file contains the log messages 33 from all nodes in the environment. 34 ` 35 36 func (c *DebugLogCommand) Info() *cmd.Info { 37 return &cmd.Info{ 38 Name: "debug-log", 39 Purpose: "display the consolidated log file", 40 Doc: debuglogDoc, 41 } 42 } 43 44 func (c *DebugLogCommand) SetFlags(f *gnuflag.FlagSet) { 45 f.Var(cmd.NewAppendStringsValue(&c.params.IncludeEntity), "i", "only show log messages for these entities") 46 f.Var(cmd.NewAppendStringsValue(&c.params.IncludeEntity), "include", "only show log messages for these entities") 47 f.Var(cmd.NewAppendStringsValue(&c.params.ExcludeEntity), "x", "do not show log messages for these entities") 48 f.Var(cmd.NewAppendStringsValue(&c.params.ExcludeEntity), "exclude", "do not show log messages for these entities") 49 f.Var(cmd.NewAppendStringsValue(&c.params.IncludeModule), "include-module", "only show log messages for these logging modules") 50 f.Var(cmd.NewAppendStringsValue(&c.params.ExcludeModule), "exclude-module", "do not show log messages for these logging modules") 51 52 f.StringVar(&c.level, "l", "", "log level to show, one of [TRACE, DEBUG, INFO, WARNING, ERROR]") 53 f.StringVar(&c.level, "level", "", "") 54 55 f.UintVar(&c.params.Backlog, "n", defaultLineCount, "go back this many lines from the end before starting to filter") 56 f.UintVar(&c.params.Backlog, "lines", defaultLineCount, "") 57 f.UintVar(&c.params.Limit, "limit", 0, "show at most this many lines") 58 f.BoolVar(&c.params.Replay, "replay", false, "start filtering from the start") 59 } 60 61 func (c *DebugLogCommand) Init(args []string) error { 62 if c.level != "" { 63 level, ok := loggo.ParseLevel(c.level) 64 if !ok || level < loggo.TRACE || level > loggo.ERROR { 65 return fmt.Errorf("level value %q is not one of %q, %q, %q, %q, %q", 66 c.level, loggo.TRACE, loggo.DEBUG, loggo.INFO, loggo.WARNING, loggo.ERROR) 67 } 68 c.params.Level = level 69 } 70 return cmd.CheckEmpty(args) 71 } 72 73 type DebugLogAPI interface { 74 WatchDebugLog(params api.DebugLogParams) (io.ReadCloser, error) 75 Close() error 76 } 77 78 var getDebugLogAPI = func(c *DebugLogCommand) (DebugLogAPI, error) { 79 return c.NewAPIClient() 80 } 81 82 // Run retrieves the debug log via the API. 83 func (c *DebugLogCommand) Run(ctx *cmd.Context) (err error) { 84 client, err := getDebugLogAPI(c) 85 if err != nil { 86 return err 87 } 88 defer client.Close() 89 debugLog, err := client.WatchDebugLog(c.params) 90 if err != nil { 91 return err 92 } 93 defer debugLog.Close() 94 _, err = io.Copy(ctx.Stdout, debugLog) 95 return err 96 } 97 98 var runSSHCommand = func(sshCmd *SSHCommand, ctx *cmd.Context) error { 99 return sshCmd.Run(ctx) 100 }