From 8c6cd46bb797dc593f56ccdab12ae46eaf3e0b8a Mon Sep 17 00:00:00 2001 From: Shu Shen Date: Thu, 21 Jan 2021 22:52:52 -0800 Subject: [PATCH 1/3] feat: logs tail --- internal/cli/logs.go | 54 ++++++++++++++++++++++++++++++++++++++++ internal/cli/root.go | 1 + internal/display/logs.go | 20 +++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 internal/cli/logs.go create mode 100644 internal/display/logs.go diff --git a/internal/cli/logs.go b/internal/cli/logs.go new file mode 100644 index 000000000..1958ec470 --- /dev/null +++ b/internal/cli/logs.go @@ -0,0 +1,54 @@ +package cli + +import ( + "github.com/spf13/cobra" + "time" + + "gopkg.in/auth0.v5/management" +) + +func logsCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "logs", + Short: "manage resources for logs.", + } + + cmd.SetUsageTemplate(resourceUsageTemplate()) + cmd.AddCommand(tailLogsCmd(cli)) + + return cmd +} + +func tailLogsCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "tail", + Short: "Tail your log events as they are happening", + Long: `$ auth0 logs tail +Tail your logs as they are happening. +`, + RunE: func(cmd *cobra.Command, args []string) error { + list, err := cli.api.Log.List(management.Parameter("sort", "date:-1")) + if err != nil { + return err + } + var fromLogId = "" + for { + if len(list) > 0 { + fromLogId = *list[len(list)-1].LogID + } + list, err := cli.api.Log.List( + management.Parameter("from", fromLogId), + management.Parameter("take", "100"), + ) + if err != nil { + return err + } + + cli.renderer.LogList(list) + time.Sleep(1 * time.Second) + } + }, + } + + return cmd +} diff --git a/internal/cli/root.go b/internal/cli/root.go index 465536adb..62496f639 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -45,6 +45,7 @@ func Execute() { "verbose", false, "Enable verbose mode.") rootCmd.AddCommand(clientsCmd(cli)) + rootCmd.AddCommand(logsCmd(cli)) // TODO(cyx): backport this later on using latest auth0/v5. // rootCmd.AddCommand(actionsCmd(cli)) diff --git a/internal/display/logs.go b/internal/display/logs.go new file mode 100644 index 000000000..4f4ac90c1 --- /dev/null +++ b/internal/display/logs.go @@ -0,0 +1,20 @@ +package display + +import ( + "fmt" + "time" + + "github.com/auth0/auth0-cli/internal/ansi" + "gopkg.in/auth0.v5/management" +) + +func (r *Renderer) LogList(logs []*management.Log) { + for _, c := range logs { + // TODO: Info/Warn/Error based on type + r.Infof(fmt.Sprintf("%s\t%s\t%s", + ansi.Faint(c.GetDate().Format(time.RFC3339)), + ansi.Faint(c.TypeName()), + ansi.Faint(*c.ClientName), + )) + } +} From e7a6d9e908f0600128590608dce038e5c8a23525 Mon Sep 17 00:00:00 2001 From: Shu Shen Date: Fri, 22 Jan 2021 08:00:22 -0800 Subject: [PATCH 2/3] Update logs display Co-authored-by: Patrick Carey --- internal/cli/logs.go | 3 +-- internal/display/logs.go | 55 ++++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/internal/cli/logs.go b/internal/cli/logs.go index 1958ec470..d6285e0b6 100644 --- a/internal/cli/logs.go +++ b/internal/cli/logs.go @@ -2,9 +2,8 @@ package cli import ( "github.com/spf13/cobra" - "time" - "gopkg.in/auth0.v5/management" + "time" ) func logsCmd(cli *cli) *cobra.Command { diff --git a/internal/display/logs.go b/internal/display/logs.go index 4f4ac90c1..38edf64b5 100644 --- a/internal/display/logs.go +++ b/internal/display/logs.go @@ -2,19 +2,58 @@ package display import ( "fmt" + "strings" "time" - "github.com/auth0/auth0-cli/internal/ansi" + "github.com/logrusorgru/aurora" "gopkg.in/auth0.v5/management" ) func (r *Renderer) LogList(logs []*management.Log) { - for _, c := range logs { - // TODO: Info/Warn/Error based on type - r.Infof(fmt.Sprintf("%s\t%s\t%s", - ansi.Faint(c.GetDate().Format(time.RFC3339)), - ansi.Faint(c.TypeName()), - ansi.Faint(*c.ClientName), - )) + for _, l := range logs { + // colorize the event type field based on whether it's a success or failure + var logType aurora.Value + if t := l.GetType(); strings.HasPrefix(t, "s") { + logType = aurora.Green(t) + } else if strings.HasPrefix(t, "f") { + logType = aurora.BrightRed(t) + } else { + logType = aurora.Reset(t) + } + + fmt.Fprintf( + r.Writer, + "[%s] (%s) client_name=%q client_id=%q", + l.Date.Format(time.RFC3339), + logType, + l.GetClientName(), + l.GetClientID(), + ) + + // if userAgent is present in the log, then add it to the output + reqMap, _ := l.Details["request"].(map[string]interface{}) + userAgent, _ := reqMap["userAgent"].(string) + if userAgent != "" { + fmt.Fprintf( + r.Writer, + " user_agent=%q", + userAgent, + ) + } + + // if an error is present in the log, add it to the output + errMap, _ := l.Details["error"].(map[string]interface{}) + errMsg, _ := errMap["message"].(string) + errType, _ := errMap["type"].(string) + if errType != "" || errMsg != "" { + fmt.Fprintf( + r.Writer, + " error_type=%q error_message=%q", + errType, + errMsg, + ) + } + + fmt.Fprint(r.Writer, "\n") } } From 71b076a5d42a674df143c612607b4227362ce7ea Mon Sep 17 00:00:00 2001 From: Shu Shen Date: Fri, 22 Jan 2021 08:34:04 -0800 Subject: [PATCH 3/3] fix: logs polling logic --- internal/cli/logs.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/cli/logs.go b/internal/cli/logs.go index d6285e0b6..9f88cb099 100644 --- a/internal/cli/logs.go +++ b/internal/cli/logs.go @@ -26,16 +26,17 @@ func tailLogsCmd(cli *cli) *cobra.Command { Tail your logs as they are happening. `, RunE: func(cmd *cobra.Command, args []string) error { - list, err := cli.api.Log.List(management.Parameter("sort", "date:-1")) + list, err := cli.api.Log.List(management.Parameter("sort", "date:1")) if err != nil { return err } - var fromLogId = "" + fromLogId := "" for { if len(list) > 0 { - fromLogId = *list[len(list)-1].LogID + cli.renderer.LogList(list) + fromLogId = list[len(list)-1].GetLogID() } - list, err := cli.api.Log.List( + list, err = cli.api.Log.List( management.Parameter("from", fromLogId), management.Parameter("take", "100"), ) @@ -43,8 +44,10 @@ Tail your logs as they are happening. return err } - cli.renderer.LogList(list) - time.Sleep(1 * time.Second) + if len(list) < 90 { + // Not a lot is happening, sleep on it + time.Sleep(1 * time.Second) + } } }, }