From eda71e3ffe4738511fe7958ebad3a1652ec652e1 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Jan 2021 01:43:55 -0500 Subject: [PATCH] feat: mirror log tailing support for logs:vector-logs command --- docs/deployment/logs.md | 2 +- plugins/common/log.go | 14 +++++++++++--- plugins/logs/src/commands/commands.go | 2 +- plugins/logs/src/subcommands/subcommands.go | 4 +++- plugins/logs/subcommands.go | 4 ++-- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/docs/deployment/logs.md b/docs/deployment/logs.md index 938a1029d..2e242c689 100644 --- a/docs/deployment/logs.md +++ b/docs/deployment/logs.md @@ -5,7 +5,7 @@ logs [-h] [-t] [-n num] [-q] [-p process] # Display recent log output logs:failed [--all|] # Shows the last failed deploy logs logs:report [] [] # Displays a logs report for one or more apps logs:set [--global|] # Set or clear a logs property for an app -logs:vector-logs # Tail the logs of the vector container +logs:vector-logs [--num num] [--tail] # Display vector log output logs:vector-start # Start the vector logging container logs:vector-stop # Stop the vector logging container ``` diff --git a/plugins/common/log.go b/plugins/common/log.go index 1a64ca241..6b9e053e2 100644 --- a/plugins/common/log.go +++ b/plugins/common/log.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "os" + "strconv" "strings" ) @@ -95,8 +96,15 @@ func LogVerboseQuietContainerLogs(containerID string) { } // LogVerboseQuietContainerLogsTail is the verbose log formatter for container logs with tail mode enabled -func LogVerboseQuietContainerLogsTail(containerID string) { - sc := NewShellCmdWithArgs(DockerBin(), "container", "logs", containerID, "--follow", "--tail", "10") +func LogVerboseQuietContainerLogsTail(containerID string, lines int, tail bool) { + args := []string{"container", "logs", containerID} + if lines > 0 { + args = append(args, "--tail", strconv.Itoa(lines)) + } + if tail { + args = append(args, "--follow") + } + sc := NewShellCmdWithArgs(DockerBin(), args...) stdout, _ := sc.Command.StdoutPipe() sc.Command.Start() @@ -104,7 +112,7 @@ func LogVerboseQuietContainerLogsTail(containerID string) { scanner.Split(bufio.ScanLines) for scanner.Scan() { m := scanner.Text() - fmt.Println(m) + LogVerboseQuiet(m) } sc.Command.Wait() } diff --git a/plugins/logs/src/commands/commands.go b/plugins/logs/src/commands/commands.go index c9c4e4fd3..c4fcc3a93 100644 --- a/plugins/logs/src/commands/commands.go +++ b/plugins/logs/src/commands/commands.go @@ -26,7 +26,7 @@ Additional commands:` logs:failed [--all|], Shows the last failed deploy logs logs:report [] [], Displays a logs report for one or more apps logs:set [--global|] , Set or clear a logs property for an app - logs:vector-logs, Tail the logs of the vector container + logs:vector-logs [--num num] [--tail], Display vector log output logs:vector-start, Start the vector logging container logs:vector-stop, Stop the vector logging container ` diff --git a/plugins/logs/src/subcommands/subcommands.go b/plugins/logs/src/subcommands/subcommands.go index d94322bc0..875dc2d67 100644 --- a/plugins/logs/src/subcommands/subcommands.go +++ b/plugins/logs/src/subcommands/subcommands.go @@ -47,8 +47,10 @@ func main() { err = logs.CommandSet(appName, property, value) case "vector-logs": args := flag.NewFlagSet("logs:vector-logs", flag.ExitOnError) + num := args.Int("num", 100, "the number of lines to display") + tail := args.Bool("tail", false, "continually stream logs") args.Parse(os.Args[2:]) - err = logs.CommandVectorLogs() + err = logs.CommandVectorLogs(*num, *tail) case "vector-start": args := flag.NewFlagSet("logs:vector-start", flag.ExitOnError) vectorImage := args.String("vector-image", logs.VectorImage, "--vector-image: the name of the docker image to run for vector") diff --git a/plugins/logs/subcommands.go b/plugins/logs/subcommands.go index 483d1baeb..25e1191ef 100644 --- a/plugins/logs/subcommands.go +++ b/plugins/logs/subcommands.go @@ -85,7 +85,7 @@ func CommandSet(appName string, property string, value string) error { } // CommandVectorLogs tails the log output for the vector container -func CommandVectorLogs() error { +func CommandVectorLogs(lines int, tail bool) error { if !common.ContainerExists(vectorContainerName) { return errors.New("Vector container does not exist") } @@ -95,7 +95,7 @@ func CommandVectorLogs() error { } common.LogInfo1Quiet("Tailing vector container logs") - common.LogVerboseQuietContainerLogsTail(vectorContainerName) + common.LogVerboseQuietContainerLogsTail(vectorContainerName, lines, tail) return nil }