From d74a02c1e093e6880296d98422591b35f3406729 Mon Sep 17 00:00:00 2001 From: Paul Hachmang Date: Tue, 1 Dec 2026 22:30:00 +0100 Subject: [PATCH] deployyy logs filters by source with --source and --except-source --- cli/README.md | 8 ++++---- cli/cli_test.go | 12 ++++++------ cli/logs.go | 22 +++++++++++----------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/cli/README.md b/cli/README.md index fd8caf82..2a1e321c 100644 --- a/cli/README.md +++ b/cli/README.md @@ -56,9 +56,9 @@ ```bash deployyy logs acme/shop/magento/main --level error,critical --since 24h - deployyy logs acme/shop --service magento-web --search SQLSTATE -f + deployyy logs acme/shop --source magento-web --search SQLSTATE -f deployyy logs acme/shop --grep 'SQLSTATE\[(23000|40001)\]' --grep deadlock -i --grep-v 'GET /health' - deployyy logs acme/shop --except-level debug,info --except-service database + deployyy logs acme/shop --except-level debug,info --except-source database ``` Each line holds the time, the level, the environment below the scope, the source and the text, oldest first. A JSON line prints its message as the text, and its other keys as `name=value`. The CLI prints each page of 1,000 lines when the page arrives. `-f` prints each new line until Ctrl-C. @@ -66,9 +66,9 @@ | Option | Use | | --- | --- | | `--level error,warning` | The levels: `debug`, `info`, `warning`, `error`, `critical` and `unknown`. Without the option, each level. | - | `--service magento-web` | A source, such as a process or a service. Repeat the option for more sources. | + | `--source magento-web` | A source, such as a process or a service. Repeat the option for more sources. | | `--except-level debug,info` | The levels to leave out. | - | `--except-service database` | A source to leave out. Repeat the option for more sources. | + | `--except-source database` | A source to leave out. Repeat the option for more sources. | | `--search SQLSTATE` | The text that each line holds. | | `--grep 'SQLSTATE\[23000\]'` | An [RE2](https://github.com/google/re2/wiki/Syntax) regular expression. Each line matches at least one `--grep`. | | `--grep-v 'GET /health'` | An RE2 regular expression of the lines to leave out. Each line matches no `--grep-v`. | diff --git a/cli/cli_test.go b/cli/cli_test.go index e3efbaff..3d28aca7 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -694,7 +694,7 @@ func TestLogsPrintsEachPageThenFollows(t *testing.T) { t.Setenv("HOME", t.TempDir()) t.Setenv("DEPLOYYY_TOKEN", "token") line := func(at, level, text string) string { - return fmt.Sprintf(`{"at":%q,"level":%q,"service":"magento-web","text":%q,"environment":{"branch":"main","app":{"slug":"magento","project":{"slug":"shop"}}}}`, at, level, text) + return fmt.Sprintf(`{"at":%q,"level":%q,"source":"magento-web","text":%q,"environment":{"branch":"main","app":{"slug":"magento","project":{"slug":"shop"}}}}`, at, level, text) } var filters []any subscriptions := 0 @@ -733,7 +733,7 @@ func TestLogsPrintsEachPageThenFollows(t *testing.T) { defer server.Close() t.Setenv("DEPLOYYY_API", server.URL) - output, err := run(t, "", "logs", "acme", "--level", "error,warn", "--level", "critical", "--since", "2h", "--search", "SQLSTATE", "--service", "magento-web", "-f") + output, err := run(t, "", "logs", "acme", "--level", "error,warn", "--level", "critical", "--since", "2h", "--search", "SQLSTATE", "--source", "magento-web", "-f") want := "10:01 CRITICAL shop/magento/main magento-web first\n" + "10:02 WARNING shop/magento/main magento-web second\n" + "10:03 ERROR shop/magento/main magento-web third\n" + @@ -743,7 +743,7 @@ func TestLogsPrintsEachPageThenFollows(t *testing.T) { t.Fatalf("logs: %v, output %q", err, output) } filter, _ := json.Marshal(filters[0]) - if string(filter) != `{"lastSeconds":7200,"levels":["ERROR","WARNING","CRITICAL"],"search":"SQLSTATE","services":["magento-web"]}` { + if string(filter) != `{"lastSeconds":7200,"levels":["ERROR","WARNING","CRITICAL"],"search":"SQLSTATE","sources":["magento-web"]}` { t.Errorf("filter %s", filter) } if subscriptions != 2 { @@ -760,8 +760,8 @@ func TestLogsPrintsEachPageThenFollows(t *testing.T) { } filters = nil - _, err = run(t, "", "logs", "acme", "--grep", "SQLSTATE", "--grep", "(?i)deadlock", "--grep-v", "GET /health", "-i", "--except-level", "debug,warn", "--except-service", "database", "--except-service", "redis", "--level", "error", "--service", "magento-web") - if filter, _ := json.Marshal(filters[0]); err != nil || string(filter) != `{"exceptLevels":["DEBUG","WARNING"],"exceptPatterns":["(?i)GET /health"],"exceptServices":["database","redis"],"levels":["ERROR"],"patterns":["(?i)SQLSTATE","(?i)deadlock"],"services":["magento-web"]}` { + _, err = run(t, "", "logs", "acme", "--grep", "SQLSTATE", "--grep", "(?i)deadlock", "--grep-v", "GET /health", "-i", "--except-level", "debug,warn", "--except-source", "database", "--except-source", "redis", "--level", "error", "--source", "magento-web") + if filter, _ := json.Marshal(filters[0]); err != nil || string(filter) != `{"exceptLevels":["DEBUG","WARNING"],"exceptPatterns":["(?i)GET /health"],"exceptSources":["database","redis"],"levels":["ERROR"],"patterns":["(?i)SQLSTATE","(?i)deadlock"],"sources":["magento-web"]}` { t.Errorf("pattern filter: %v, %s", err, filter) } filters = nil @@ -793,7 +793,7 @@ func TestLogsPrintsTheFirstPageBeforeTheNextPageArrives(t *testing.T) { var request struct{ Variables map[string]any } json.NewDecoder(r.Body).Decode(&request) node := func(text string) string { - return fmt.Sprintf(`{"at":"10:00","level":"INFO","service":"web","text":%q,"environment":null}`, text) + return fmt.Sprintf(`{"at":"10:00","level":"INFO","source":"web","text":%q,"environment":null}`, text) } if request.Variables["before"] == nil { fmt.Fprintf(w, `{"data":{"scope":{"id":"gid://deployyy/Environment/main","allLogs":{"liveCursor":"live","edges":[{"cursor":"c1","node":%s}],"pageInfo":{"hasPreviousPage":true,"startCursor":"c1"}}}}}`, node("oldest")) diff --git a/cli/logs.go b/cli/logs.go index 734d8671..ee2b3403 100644 --- a/cli/logs.go +++ b/cli/logs.go @@ -29,12 +29,12 @@ var logScopes = []struct{ field, arguments, variables string }{ {"environment", "team: $team, project: $project, app: $app, branch: $branch", "$team: String!, $project: String!, $app: String!, $branch: String!"}, } -const logLineFields = `at level service text fields { name value } environment { branch app { slug project { slug } } }` +const logLineFields = `at level source text fields { name value } environment { branch app { slug project { slug } } }` type logLine struct { - At, Level, Service, Text string - Fields []struct{ Name, Value string } - Environment *struct { + At, Level, Source, Text string + Fields []struct{ Name, Value string } + Environment *struct { Branch string App struct { Slug string @@ -50,16 +50,16 @@ type logEdge struct { func logsCommand() *cobra.Command { var follow, asJSON, ignoreCase bool - var levels, exceptLevels, services, exceptServices, patterns, exceptPatterns []string + var levels, exceptLevels, sources, exceptSources, patterns, exceptPatterns []string var search, since string command := &cobra.Command{ Use: "logs [/[/[/]]]", Short: "Print the log lines of an environment, or of each environment below a team, a project or an app", Long: "Prints the log lines, oldest first: the time, the level, the environment below the scope, the source, the text and the fields of a JSON line as name=value. Without --since, the lines of the last hour. With --follow, the command prints each new line until Ctrl-C.", Example: ` deployyy logs acme/shop/magento/main --level error,critical - deployyy logs acme/shop --since 24h --search "SQLSTATE" --service magento-web + deployyy logs acme/shop --since 24h --search "SQLSTATE" --source magento-web deployyy logs acme/shop --grep 'SQLSTATE\[(23000|40001)\]' --grep 'Deadlock' -i - deployyy logs acme/shop --except-level debug,info --except-service database --grep-v 'GET /health' + deployyy logs acme/shop --except-level debug,info --except-source database --grep-v 'GET /health' deployyy logs acme/shop/magento/main -f --json`, Args: cobra.ExactArgs(1), RunE: func(command *cobra.Command, args []string) error { @@ -98,7 +98,7 @@ func logsCommand() *cobra.Command { filter[field] = picked } } - for field, values := range map[string][]string{"services": services, "exceptServices": exceptServices} { + for field, values := range map[string][]string{"sources": sources, "exceptSources": exceptSources} { if len(values) > 0 { filter[field] = values } @@ -131,9 +131,9 @@ func logsCommand() *cobra.Command { } command.Flags().BoolVarP(&follow, "follow", "f", false, "print each new line until Ctrl-C") command.Flags().StringSliceVar(&levels, "level", nil, "the levels to print, such as error,warning; without the option, each level") - command.Flags().StringArrayVar(&services, "service", nil, "a source to print, such as magento-web or database; repeat the option for more sources") + command.Flags().StringArrayVar(&sources, "source", nil, "a source to print, such as magento-web or database; repeat the option for more sources") command.Flags().StringSliceVar(&exceptLevels, "except-level", nil, "the levels to leave out, such as debug,info") - command.Flags().StringArrayVar(&exceptServices, "except-service", nil, "a source to leave out, such as database; repeat the option for more sources") + command.Flags().StringArrayVar(&exceptSources, "except-source", nil, "a source to leave out, such as database; repeat the option for more sources") command.Flags().StringVar(&search, "search", "", "the text that each line holds") command.Flags().StringArrayVar(&patterns, "grep", nil, "an RE2 regular expression of the lines to print; repeat the option for more expressions, and a line matches one of them") command.Flags().StringArrayVar(&exceptPatterns, "grep-v", nil, "an RE2 regular expression of the lines to leave out; repeat the option for more expressions") @@ -287,7 +287,7 @@ func logPrinter(depth int, asJSON, colour bool) func(json.RawMessage) error { if line.Environment != nil && depth < 4 { parts = append(parts, strings.Join([]string{line.Environment.App.Project.Slug, line.Environment.App.Slug, line.Environment.Branch}[depth-1:], "/")) } - parts = append(parts, line.Service, line.Text) + parts = append(parts, line.Source, line.Text) for _, field := range line.Fields { value := field.Value if strings.ContainsAny(value, " \t\n\"=") {