diff --git a/api/logs.go b/api/logs.go index 2519f9cd3..e9addb5d1 100644 --- a/api/logs.go +++ b/api/logs.go @@ -14,7 +14,7 @@ func LogsURL(app string) (*http.Response, error) { return req.Do() } -func Logs(logsURL string, n int) (*http.Response, error) { +func Logs(logsURL string, n int, filter string) (*http.Response, error) { u, err := url.Parse(logsURL) if err != nil { return nil, errgo.Mask(err) @@ -25,8 +25,9 @@ func Logs(logsURL string, n int) (*http.Response, error) { URL: u.Scheme + "://" + u.Host, Endpoint: u.Path, Params: map[string]interface{}{ - "token": u.Query().Get("token"), - "n": n, + "token": u.Query().Get("token"), + "n": n, + "filter": filter, }, } return req.Do() diff --git a/apps/logs.go b/apps/logs.go index 89935062c..3ff81feea 100644 --- a/apps/logs.go +++ b/apps/logs.go @@ -29,7 +29,7 @@ type LogsRes struct { App *api.App `json:"app"` } -func Logs(appName string, stream bool, n int) error { +func Logs(appName string, stream bool, n int, filter string) error { res, err := api.LogsURL(appName) if err != nil { return errgo.Mask(err, errgo.Any) @@ -52,20 +52,20 @@ func Logs(appName string, stream bool, n int) error { return errgo.Mask(err, errgo.Any) } - if err = dumpLogs(logsRes.LogsURL, n); err != nil { + if err = dumpLogs(logsRes.LogsURL, n, filter); err != nil { return errgo.Mask(err, errgo.Any) } if stream { - if err = streamLogs(logsRes.LogsURL); err != nil { + if err = streamLogs(logsRes.LogsURL, filter); err != nil { return errgo.Mask(err, errgo.Any) } } return nil } -func dumpLogs(logsURL string, n int) error { - res, err := api.Logs(logsURL, n) +func dumpLogs(logsURL string, n int, filter string) error { + res, err := api.Logs(logsURL, n, filter) if err != nil { return errgo.Mask(err, errgo.Any) } @@ -84,7 +84,7 @@ func dumpLogs(logsURL string, n int) error { return nil } -func streamLogs(logsRawURL string) error { +func streamLogs(logsRawURL string, filter string) error { var ( err error buffer [2048]byte @@ -101,7 +101,12 @@ func streamLogs(logsRawURL string) error { logsURL.Scheme = "ws" } - conn, err := websocket.Dial(logsURL.String()+"&stream=true", "", "http://scalingo-cli.local/"+config.Version) + logsURLString := fmt.Sprintf("%s&stream=true", logsURL.String()) + if filter != "" { + logsURLString = fmt.Sprintf("%s&filter=%s", logsURLString, filter) + } + + conn, err := websocket.Dial(logsURLString, "", "http://scalingo-cli.local/"+config.Version) if err != nil { return errgo.Mask(err, errgo.Any) } diff --git a/cmd/logs.go b/cmd/logs.go index 4d4336944..a43e60053 100644 --- a/cmd/logs.go +++ b/cmd/logs.go @@ -14,16 +14,18 @@ var ( Usage: "Get the logs of your applications", Description: `Get the logs of your applications Example: - Get 100 lines: 'scalingo --app my-app logs -n 100' - Real-Time logs: 'scalingo --app my-app logs -f'`, + Get 100 lines: 'scalingo --app my-app logs -n 100' + Real-Time logs: 'scalingo --app my-app logs -f' + Get lines with filter: 'scalingo --app my-app logs -n 100 -F web-1|worker-2'`, Flags: []cli.Flag{appFlag, cli.IntFlag{Name: "lines, n", Value: 20, Usage: "Number of log lines to dump", EnvVar: ""}, cli.BoolFlag{Name: "follow, f", Usage: "Stream logs of app, (as \"tail -f\")", EnvVar: ""}, + cli.StringFlag{Name: "filter, F", Usage: "Filter containers logs that will be displayed", EnvVar: ""}, }, Action: func(c *cli.Context) { currentApp := appdetect.CurrentApp(c) - if len(c.Args()) == 0 || len(c.Args()) == 2 && c.Int("n") != 0 { - if err := apps.Logs(currentApp, c.Bool("f"), c.Int("n")); err != nil { + if len(c.Args()) == 0 { + if err := apps.Logs(currentApp, c.Bool("f"), c.Int("n"), c.String("F")); err != nil { errorQuit(err) } } else {