Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[A0CLI-5] feat: logs tail #6

Merged
merged 3 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions internal/cli/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cli

import (
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
"time"
)

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
}
fromLogId := ""
for {
if len(list) > 0 {
cli.renderer.LogList(list)
fromLogId = list[len(list)-1].GetLogID()
}
list, err = cli.api.Log.List(
management.Parameter("from", fromLogId),
management.Parameter("take", "100"),
)
if err != nil {
return err
}

if len(list) < 90 {
// Not a lot is happening, sleep on it
time.Sleep(1 * time.Second)
}
}
},
}

return cmd
}
1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
59 changes: 59 additions & 0 deletions internal/display/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package display

import (
"fmt"
"strings"
"time"

"github.com/logrusorgru/aurora"
"gopkg.in/auth0.v5/management"
)

func (r *Renderer) LogList(logs []*management.Log) {
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")
}
}